KNX christmas cleaning #2 - remove old migrations (#60026)

* Remove old migrations from KNX integration

* Remove cover migration
This commit is contained in:
Marvin Wichmann 2021-11-20 16:34:59 +01:00 committed by GitHub
parent 6d4b74f8f2
commit 769661adc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 2 additions and 145 deletions

View file

@ -6,7 +6,6 @@ from typing import Any
from xknx import XKNX from xknx import XKNX
from xknx.devices import Climate as XknxClimate, ClimateMode as XknxClimateMode from xknx.devices import Climate as XknxClimate, ClimateMode as XknxClimateMode
from xknx.dpt.dpt_hvac_mode import HVACControllerMode, HVACOperationMode from xknx.dpt.dpt_hvac_mode import HVACControllerMode, HVACOperationMode
from xknx.telegram.address import parse_device_group_address
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate import ClimateEntity
@ -24,8 +23,7 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -56,50 +54,9 @@ async def async_setup_entry(
SupportedPlatforms.CLIMATE.value SupportedPlatforms.CLIMATE.value
] ]
_async_migrate_unique_id(hass, config)
async_add_entities(KNXClimate(xknx, entity_config) for entity_config in config) async_add_entities(KNXClimate(xknx, entity_config) for entity_config in config)
@callback
def _async_migrate_unique_id(
hass: HomeAssistant, platform_config: list[ConfigType]
) -> None:
"""Change unique_ids used in 2021.4 to include target_temperature GA."""
entity_registry = er.async_get(hass)
for entity_config in platform_config:
# normalize group address strings - ga_temperature_state was the old uid
ga_temperature_state = parse_device_group_address(
entity_config[ClimateSchema.CONF_TEMPERATURE_ADDRESS][0]
)
old_uid = str(ga_temperature_state)
entity_id = entity_registry.async_get_entity_id("climate", DOMAIN, old_uid)
if entity_id is None:
continue
ga_target_temperature_state = parse_device_group_address(
entity_config[ClimateSchema.CONF_TARGET_TEMPERATURE_STATE_ADDRESS][0]
)
target_temp = entity_config.get(ClimateSchema.CONF_TARGET_TEMPERATURE_ADDRESS)
ga_target_temperature = (
parse_device_group_address(target_temp[0])
if target_temp is not None
else None
)
setpoint_shift = entity_config.get(ClimateSchema.CONF_SETPOINT_SHIFT_ADDRESS)
ga_setpoint_shift = (
parse_device_group_address(setpoint_shift[0])
if setpoint_shift is not None
else None
)
new_uid = (
f"{ga_temperature_state}_"
f"{ga_target_temperature_state}_"
f"{ga_target_temperature}_"
f"{ga_setpoint_shift}"
)
entity_registry.async_update_entity(entity_id, new_unique_id=new_uid)
def _create_climate(xknx: XKNX, config: ConfigType) -> XknxClimate: def _create_climate(xknx: XKNX, config: ConfigType) -> XknxClimate:
"""Return a KNX Climate device to be used within XKNX.""" """Return a KNX Climate device to be used within XKNX."""
climate_mode = XknxClimateMode( climate_mode = XknxClimateMode(

View file

@ -7,7 +7,6 @@ from typing import Any
from xknx import XKNX from xknx import XKNX
from xknx.devices import Cover as XknxCover, Device as XknxDevice from xknx.devices import Cover as XknxCover, Device as XknxDevice
from xknx.telegram.address import parse_device_group_address
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.cover import ( from homeassistant.components.cover import (
@ -26,7 +25,6 @@ from homeassistant.components.cover import (
) )
from homeassistant.const import CONF_DEVICE_CLASS, CONF_ENTITY_CATEGORY, CONF_NAME from homeassistant.const import CONF_DEVICE_CLASS, CONF_ENTITY_CATEGORY, CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_utc_time_change from homeassistant.helpers.event import async_track_utc_time_change
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -47,37 +45,9 @@ async def async_setup_entry(
SupportedPlatforms.COVER.value SupportedPlatforms.COVER.value
] ]
_async_migrate_unique_id(hass, config)
async_add_entities(KNXCover(xknx, entity_config) for entity_config in config) async_add_entities(KNXCover(xknx, entity_config) for entity_config in config)
@callback
def _async_migrate_unique_id(
hass: HomeAssistant, platform_config: list[ConfigType]
) -> None:
"""Change unique_ids used in 2021.4 to include position_target GA."""
entity_registry = er.async_get(hass)
for entity_config in platform_config:
# normalize group address strings - ga_updown was the old uid but is optional
updown_addresses = entity_config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS)
if updown_addresses is None:
continue
ga_updown = parse_device_group_address(updown_addresses[0])
old_uid = str(ga_updown)
entity_id = entity_registry.async_get_entity_id("cover", DOMAIN, old_uid)
if entity_id is None:
continue
position_target_addresses = entity_config.get(CoverSchema.CONF_POSITION_ADDRESS)
ga_position_target = (
parse_device_group_address(position_target_addresses[0])
if position_target_addresses is not None
else None
)
new_uid = f"{ga_updown}_{ga_position_target}"
entity_registry.async_update_entity(entity_id, new_unique_id=new_uid)
class KNXCover(KnxEntity, CoverEntity): class KNXCover(KnxEntity, CoverEntity):
"""Representation of a KNX cover.""" """Representation of a KNX cover."""

View file

@ -5,7 +5,6 @@ from typing import Any, Tuple, cast
from xknx import XKNX from xknx import XKNX
from xknx.devices.light import Light as XknxLight, XYYColor from xknx.devices.light import Light as XknxLight, XYYColor
from xknx.telegram.address import parse_device_group_address
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.light import ( from homeassistant.components.light import (
@ -25,8 +24,7 @@ from homeassistant.components.light import (
LightEntity, LightEntity,
) )
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
@ -53,77 +51,9 @@ async def async_setup_entry(
SupportedPlatforms.LIGHT.value SupportedPlatforms.LIGHT.value
] ]
_async_migrate_unique_id(hass, config)
async_add_entities(KNXLight(xknx, entity_config) for entity_config in config) async_add_entities(KNXLight(xknx, entity_config) for entity_config in config)
@callback
def _async_migrate_unique_id(
hass: HomeAssistant, platform_config: list[ConfigType]
) -> None:
"""Change unique_ids used in 2021.4 to exchange individual color switch address for brightness address."""
entity_registry = er.async_get(hass)
for entity_config in platform_config:
individual_colors_config = entity_config.get(LightSchema.CONF_INDIVIDUAL_COLORS)
if individual_colors_config is None:
continue
try:
ga_red_switch = individual_colors_config[LightSchema.CONF_RED][KNX_ADDRESS][
0
]
ga_green_switch = individual_colors_config[LightSchema.CONF_GREEN][
KNX_ADDRESS
][0]
ga_blue_switch = individual_colors_config[LightSchema.CONF_BLUE][
KNX_ADDRESS
][0]
except KeyError:
continue
# normalize group address strings
ga_red_switch = parse_device_group_address(ga_red_switch)
ga_green_switch = parse_device_group_address(ga_green_switch)
ga_blue_switch = parse_device_group_address(ga_blue_switch)
# white config is optional so it has to be checked for `None` extra
white_config = individual_colors_config.get(LightSchema.CONF_WHITE)
white_switch = (
white_config.get(KNX_ADDRESS) if white_config is not None else None
)
ga_white_switch = (
parse_device_group_address(white_switch[0])
if white_switch is not None
else None
)
old_uid = (
f"{ga_red_switch}_"
f"{ga_green_switch}_"
f"{ga_blue_switch}_"
f"{ga_white_switch}"
)
entity_id = entity_registry.async_get_entity_id("light", DOMAIN, old_uid)
if entity_id is None:
continue
ga_red_brightness = parse_device_group_address(
individual_colors_config[LightSchema.CONF_RED][
LightSchema.CONF_BRIGHTNESS_ADDRESS
][0]
)
ga_green_brightness = parse_device_group_address(
individual_colors_config[LightSchema.CONF_GREEN][
LightSchema.CONF_BRIGHTNESS_ADDRESS
][0]
)
ga_blue_brightness = parse_device_group_address(
individual_colors_config[LightSchema.CONF_BLUE][
LightSchema.CONF_BRIGHTNESS_ADDRESS
][0]
)
new_uid = f"{ga_red_brightness}_{ga_green_brightness}_{ga_blue_brightness}"
entity_registry.async_update_entity(entity_id, new_unique_id=new_uid)
def _create_light(xknx: XKNX, config: ConfigType) -> XknxLight: def _create_light(xknx: XKNX, config: ConfigType) -> XknxLight:
"""Return a KNX Light device to be used within XKNX.""" """Return a KNX Light device to be used within XKNX."""