From 5e243da47013c8c9c49d88fa861c65ac8d59f44c Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 21 Apr 2023 20:38:45 +0200 Subject: [PATCH] Clean up gateway logic from Plugwise (#91769) --- homeassistant/components/plugwise/__init__.py | 83 +++++++++++++++--- homeassistant/components/plugwise/const.py | 2 +- homeassistant/components/plugwise/gateway.py | 85 ------------------- 3 files changed, 73 insertions(+), 97 deletions(-) delete mode 100644 homeassistant/components/plugwise/gateway.py diff --git a/homeassistant/components/plugwise/__init__.py b/homeassistant/components/plugwise/__init__.py index 89349e1f0b5..bfae7772b93 100644 --- a/homeassistant/components/plugwise/__init__.py +++ b/homeassistant/components/plugwise/__init__.py @@ -1,23 +1,84 @@ """Plugwise platform for Home Assistant Core.""" +from __future__ import annotations + +from typing import Any from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST -from homeassistant.core import HomeAssistant +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import device_registry as dr, entity_registry as er -from .gateway import async_setup_entry_gw, async_unload_entry_gw +from .const import DOMAIN, LOGGER, PLATFORMS +from .coordinator import PlugwiseDataUpdateCoordinator async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Plugwise components from a config entry.""" - if entry.data.get(CONF_HOST): - return await async_setup_entry_gw(hass, entry) - # PLACEHOLDER USB entry setup - return False + await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) + + coordinator = PlugwiseDataUpdateCoordinator(hass, entry) + await coordinator.async_config_entry_first_refresh() + migrate_sensor_entities(hass, coordinator) + + hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator + + device_registry = dr.async_get(hass) + device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, str(coordinator.api.gateway_id))}, + manufacturer="Plugwise", + model=coordinator.api.smile_model, + name=coordinator.api.smile_name, + sw_version=coordinator.api.smile_version[0], + ) + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload the Plugwise components.""" - if entry.data.get(CONF_HOST): - return await async_unload_entry_gw(hass, entry) - # PLACEHOLDER USB entry setup - return False + if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): + hass.data[DOMAIN].pop(entry.entry_id) + return unload_ok + + +@callback +def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None: + """Migrate Plugwise entity entries. + + - Migrates unique ID from old relay switches to the new unique ID + """ + if entry.domain == Platform.SWITCH and entry.unique_id.endswith("-plug"): + return {"new_unique_id": entry.unique_id.replace("-plug", "-relay")} + + # No migration needed + return None + + +def migrate_sensor_entities( + hass: HomeAssistant, + coordinator: PlugwiseDataUpdateCoordinator, +) -> None: + """Migrate Sensors if needed.""" + ent_reg = er.async_get(hass) + + # Migrating opentherm_outdoor_temperature + # to opentherm_outdoor_air_temperature sensor + for device_id, device in coordinator.data.devices.items(): + if device.get("dev_class") != "heater_central": + continue + + old_unique_id = f"{device_id}-outdoor_temperature" + if entity_id := ent_reg.async_get_entity_id( + Platform.SENSOR, DOMAIN, old_unique_id + ): + new_unique_id = f"{device_id}-outdoor_air_temperature" + LOGGER.debug( + "Migrating entity %s from old unique ID '%s' to new unique ID '%s'", + entity_id, + old_unique_id, + new_unique_id, + ) + ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id) diff --git a/homeassistant/components/plugwise/const.py b/homeassistant/components/plugwise/const.py index dd13e0e5092..34bb5c926ae 100644 --- a/homeassistant/components/plugwise/const.py +++ b/homeassistant/components/plugwise/const.py @@ -21,7 +21,7 @@ SMILE: Final = "smile" STRETCH: Final = "stretch" STRETCH_USERNAME: Final = "stretch" -PLATFORMS_GATEWAY: Final[list[str]] = [ +PLATFORMS: Final[list[str]] = [ Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.NUMBER, diff --git a/homeassistant/components/plugwise/gateway.py b/homeassistant/components/plugwise/gateway.py deleted file mode 100644 index 282fd163e8a..00000000000 --- a/homeassistant/components/plugwise/gateway.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Plugwise platform for Home Assistant Core.""" -from __future__ import annotations - -from typing import Any - -from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import device_registry as dr, entity_registry as er - -from .const import DOMAIN, LOGGER, PLATFORMS_GATEWAY, Platform -from .coordinator import PlugwiseDataUpdateCoordinator - - -async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool: - """Set up Plugwise Smiles from a config entry.""" - await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry) - - coordinator = PlugwiseDataUpdateCoordinator(hass, entry) - await coordinator.async_config_entry_first_refresh() - migrate_sensor_entities(hass, coordinator) - - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator - - device_registry = dr.async_get(hass) - device_registry.async_get_or_create( - config_entry_id=entry.entry_id, - identifiers={(DOMAIN, str(coordinator.api.gateway_id))}, - manufacturer="Plugwise", - model=coordinator.api.smile_model, - name=coordinator.api.smile_name, - sw_version=coordinator.api.smile_version[0], - ) - - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS_GATEWAY) - - return True - - -async def async_unload_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool: - """Unload a config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms( - entry, PLATFORMS_GATEWAY - ): - hass.data[DOMAIN].pop(entry.entry_id) - return unload_ok - - -@callback -def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None: - """Migrate Plugwise entity entries. - - - Migrates unique ID from old relay switches to the new unique ID - """ - if entry.domain == Platform.SWITCH and entry.unique_id.endswith("-plug"): - return {"new_unique_id": entry.unique_id.replace("-plug", "-relay")} - - # No migration needed - return None - - -def migrate_sensor_entities( - hass: HomeAssistant, - coordinator: PlugwiseDataUpdateCoordinator, -) -> None: - """Migrate Sensors if needed.""" - ent_reg = er.async_get(hass) - - # Migrating opentherm_outdoor_temperature - # to opentherm_outdoor_air_temperature sensor - for device_id, device in coordinator.data.devices.items(): - if device.get("dev_class") != "heater_central": - continue - - old_unique_id = f"{device_id}-outdoor_temperature" - if entity_id := ent_reg.async_get_entity_id( - Platform.SENSOR, DOMAIN, old_unique_id - ): - new_unique_id = f"{device_id}-outdoor_air_temperature" - LOGGER.debug( - "Migrating entity %s from old unique ID '%s' to new unique ID '%s'", - entity_id, - old_unique_id, - new_unique_id, - ) - ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)