Skip to content

Commit

Permalink
KNX christmas cleaning #2 - remove old migrations (home-assistant#60026)
Browse files Browse the repository at this point in the history
* Remove old migrations from KNX integration

* Remove cover migration
  • Loading branch information
marvin-w authored Nov 20, 2021
1 parent 6d4b74f commit 769661a
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 145 deletions.
45 changes: 1 addition & 44 deletions homeassistant/components/knx/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from xknx import XKNX
from xknx.devices import Climate as XknxClimate, ClimateMode as XknxClimateMode
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.components.climate import ClimateEntity
Expand All @@ -24,8 +23,7 @@
CONF_NAME,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType

Expand Down Expand Up @@ -56,50 +54,9 @@ async def async_setup_entry(
SupportedPlatforms.CLIMATE.value
]

_async_migrate_unique_id(hass, 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:
"""Return a KNX Climate device to be used within XKNX."""
climate_mode = XknxClimateMode(
Expand Down
30 changes: 0 additions & 30 deletions homeassistant/components/knx/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from xknx import XKNX
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.components.cover import (
Expand All @@ -26,7 +25,6 @@
)
from homeassistant.const import CONF_DEVICE_CLASS, CONF_ENTITY_CATEGORY, CONF_NAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_utc_time_change
from homeassistant.helpers.typing import ConfigType
Expand All @@ -47,37 +45,9 @@ async def async_setup_entry(
SupportedPlatforms.COVER.value
]

_async_migrate_unique_id(hass, 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):
"""Representation of a KNX cover."""

Expand Down
72 changes: 1 addition & 71 deletions homeassistant/components/knx/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from xknx import XKNX
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.components.light import (
Expand All @@ -25,8 +24,7 @@
LightEntity,
)
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
import homeassistant.util.color as color_util
Expand All @@ -53,77 +51,9 @@ async def async_setup_entry(
SupportedPlatforms.LIGHT.value
]

_async_migrate_unique_id(hass, 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:
"""Return a KNX Light device to be used within XKNX."""

Expand Down

0 comments on commit 769661a

Please sign in to comment.