From fb758bd8b62bc144b21e0a08b92410fa57c9c339 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 9 Jul 2024 23:36:02 -0700 Subject: [PATCH] Migrate screenlogic to use entry.runtime_data (#121644) --- .../components/screenlogic/__init__.py | 21 ++++++++++++------- .../components/screenlogic/binary_sensor.py | 12 +++-------- .../components/screenlogic/climate.py | 10 +++------ .../components/screenlogic/diagnostics.py | 10 +++------ homeassistant/components/screenlogic/light.py | 14 ++++--------- .../components/screenlogic/number.py | 9 +++----- .../components/screenlogic/sensor.py | 9 +++----- .../components/screenlogic/services.py | 11 ++++++---- .../components/screenlogic/switch.py | 14 ++++--------- homeassistant/components/screenlogic/types.py | 7 +++++++ 10 files changed, 50 insertions(+), 67 deletions(-) create mode 100644 homeassistant/components/screenlogic/types.py diff --git a/homeassistant/components/screenlogic/__init__.py b/homeassistant/components/screenlogic/__init__.py index c7dbaabd565c0..6f58e9b366699 100644 --- a/homeassistant/components/screenlogic/__init__.py +++ b/homeassistant/components/screenlogic/__init__.py @@ -20,6 +20,9 @@ from .services import async_load_screenlogic_services from .util import generate_unique_id +type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator] + + _LOGGER = logging.getLogger(__name__) @@ -49,7 +52,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: ScreenLogicConfigEntry) -> bool: """Set up Screenlogic from a config entry.""" await _async_migrate_entries(hass, entry) @@ -72,31 +75,33 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry.async_on_unload(entry.add_update_listener(async_update_listener)) - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, entry: ScreenLogicConfigEntry +) -> bool: """Unload a config entry.""" unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: - coordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data await coordinator.gateway.async_disconnect() - hass.data[DOMAIN].pop(entry.entry_id) - return unload_ok -async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: +async def async_update_listener( + hass: HomeAssistant, entry: ScreenLogicConfigEntry +) -> None: """Handle options update.""" await hass.config_entries.async_reload(entry.entry_id) async def _async_migrate_entries( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: ScreenLogicConfigEntry ) -> None: """Migrate to new entity names.""" entity_registry = er.async_get(hass) diff --git a/homeassistant/components/screenlogic/binary_sensor.py b/homeassistant/components/screenlogic/binary_sensor.py index 1277ea7e1d45e..13582b811965b 100644 --- a/homeassistant/components/screenlogic/binary_sensor.py +++ b/homeassistant/components/screenlogic/binary_sensor.py @@ -2,7 +2,6 @@ from copy import copy import dataclasses -import logging from screenlogicpy.const.common import ON_OFF from screenlogicpy.const.data import ATTR, DEVICE, GROUP, VALUE @@ -15,12 +14,10 @@ BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN as SL_DOMAIN from .coordinator import ScreenlogicDataUpdateCoordinator from .entity import ( ScreenLogicEntity, @@ -28,10 +25,9 @@ ScreenLogicPushEntity, ScreenLogicPushEntityDescription, ) +from .types import ScreenLogicConfigEntry from .util import cleanup_excluded_entity -_LOGGER = logging.getLogger(__name__) - @dataclasses.dataclass(frozen=True, kw_only=True) class ScreenLogicBinarySensorDescription( @@ -171,13 +167,11 @@ class ScreenLogicPushBinarySensorDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ScreenLogicConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up entry.""" - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data gateway = coordinator.gateway entities: list[ScreenLogicBinarySensor] = [ diff --git a/homeassistant/components/screenlogic/climate.py b/homeassistant/components/screenlogic/climate.py index 8e89cb2eb03da..4d93dcf81d366 100644 --- a/homeassistant/components/screenlogic/climate.py +++ b/homeassistant/components/screenlogic/climate.py @@ -18,16 +18,14 @@ HVACAction, HVACMode, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity -from .const import DOMAIN as SL_DOMAIN -from .coordinator import ScreenlogicDataUpdateCoordinator from .entity import ScreenLogicPushEntity, ScreenLogicPushEntityDescription +from .types import ScreenLogicConfigEntry _LOGGER = logging.getLogger(__name__) @@ -43,13 +41,11 @@ async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ScreenLogicConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up entry.""" - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data gateway = coordinator.gateway diff --git a/homeassistant/components/screenlogic/diagnostics.py b/homeassistant/components/screenlogic/diagnostics.py index 92e700239ff72..ddb3ee9e5e964 100644 --- a/homeassistant/components/screenlogic/diagnostics.py +++ b/homeassistant/components/screenlogic/diagnostics.py @@ -2,20 +2,16 @@ from typing import Any -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from .const import DOMAIN -from .coordinator import ScreenlogicDataUpdateCoordinator +from .types import ScreenLogicConfigEntry async def async_get_config_entry_diagnostics( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: ScreenLogicConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data return { "config_entry": config_entry.as_dict(), diff --git a/homeassistant/components/screenlogic/light.py b/homeassistant/components/screenlogic/light.py index 4def432d97cc8..412b2df5f81e9 100644 --- a/homeassistant/components/screenlogic/light.py +++ b/homeassistant/components/screenlogic/light.py @@ -1,7 +1,6 @@ """Support for a ScreenLogic light 'circuit' switch.""" from dataclasses import dataclass -import logging from screenlogicpy.const.data import ATTR, DEVICE from screenlogicpy.const.msg import CODE @@ -12,27 +11,22 @@ LightEntity, LightEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN as SL_DOMAIN, LIGHT_CIRCUIT_FUNCTIONS -from .coordinator import ScreenlogicDataUpdateCoordinator +from .const import LIGHT_CIRCUIT_FUNCTIONS from .entity import ScreenLogicCircuitEntity, ScreenLogicPushEntityDescription - -_LOGGER = logging.getLogger(__name__) +from .types import ScreenLogicConfigEntry async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ScreenLogicConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up entry.""" entities: list[ScreenLogicLight] = [] - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data gateway = coordinator.gateway for circuit_index, circuit_data in gateway.get_data(DEVICE.CIRCUIT).items(): if ( diff --git a/homeassistant/components/screenlogic/number.py b/homeassistant/components/screenlogic/number.py index ca75f5fadce4a..c5d67b8f285a1 100644 --- a/homeassistant/components/screenlogic/number.py +++ b/homeassistant/components/screenlogic/number.py @@ -14,13 +14,11 @@ NumberEntityDescription, NumberMode, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN as SL_DOMAIN from .coordinator import ScreenlogicDataUpdateCoordinator from .entity import ( ScreenLogicEntity, @@ -28,6 +26,7 @@ ScreenLogicPushEntity, ScreenLogicPushEntityDescription, ) +from .types import ScreenLogicConfigEntry from .util import cleanup_excluded_entity, get_ha_unit _LOGGER = logging.getLogger(__name__) @@ -98,14 +97,12 @@ class ScreenLogicPushNumberDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ScreenLogicConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up entry.""" entities: list[ScreenLogicNumber] = [] - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data gateway = coordinator.gateway for chem_number_description in SUPPORTED_INTELLICHEM_NUMBERS: diff --git a/homeassistant/components/screenlogic/sensor.py b/homeassistant/components/screenlogic/sensor.py index 1a09f3c738a8f..0b8e41474202d 100644 --- a/homeassistant/components/screenlogic/sensor.py +++ b/homeassistant/components/screenlogic/sensor.py @@ -18,12 +18,10 @@ SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN as SL_DOMAIN from .coordinator import ScreenlogicDataUpdateCoordinator from .entity import ( ScreenLogicEntity, @@ -31,6 +29,7 @@ ScreenLogicPushEntity, ScreenLogicPushEntityDescription, ) +from .types import ScreenLogicConfigEntry from .util import cleanup_excluded_entity, get_ha_unit _LOGGER = logging.getLogger(__name__) @@ -227,13 +226,11 @@ class ScreenLogicPushSensorDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ScreenLogicConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up entry.""" - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data gateway = coordinator.gateway entities: list[ScreenLogicSensor] = [ diff --git a/homeassistant/components/screenlogic/services.py b/homeassistant/components/screenlogic/services.py index 116a66d97df54..3177f27ab2a19 100644 --- a/homeassistant/components/screenlogic/services.py +++ b/homeassistant/components/screenlogic/services.py @@ -1,12 +1,13 @@ """Services for ScreenLogic integration.""" import logging +from typing import cast from screenlogicpy import ScreenLogicError from screenlogicpy.device_const.system import EQUIPMENT_FLAG import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigEntryState +from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import ( @@ -29,6 +30,7 @@ SUPPORTED_COLOR_MODES, ) from .coordinator import ScreenlogicDataUpdateCoordinator +from .types import ScreenLogicConfigEntry _LOGGER = logging.getLogger(__name__) @@ -103,8 +105,9 @@ async def get_coordinators( coordinators: list[ScreenlogicDataUpdateCoordinator] = [] for entry_id in entry_ids: - config_entry: ConfigEntry | None = hass.config_entries.async_get_entry( - entry_id + config_entry = cast( + ScreenLogicConfigEntry | None, + hass.config_entries.async_get_entry(entry_id), ) if not config_entry: raise ServiceValidationError( @@ -121,7 +124,7 @@ async def get_coordinators( f"Failed to call service '{service_call.service}'. Config entry " f"'{entry_id}' not loaded" ) - coordinators.append(hass.data[DOMAIN][entry_id]) + coordinators.append(config_entry.runtime_data) return coordinators diff --git a/homeassistant/components/screenlogic/switch.py b/homeassistant/components/screenlogic/switch.py index fe697567babd7..1d36ee00b944d 100644 --- a/homeassistant/components/screenlogic/switch.py +++ b/homeassistant/components/screenlogic/switch.py @@ -1,26 +1,22 @@ """Support for a ScreenLogic 'circuit' switch.""" from dataclasses import dataclass -import logging from screenlogicpy.const.data import ATTR, DEVICE from screenlogicpy.const.msg import CODE from screenlogicpy.device_const.circuit import GENERIC_CIRCUIT_NAMES, INTERFACE from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN as SL_DOMAIN, LIGHT_CIRCUIT_FUNCTIONS -from .coordinator import ScreenlogicDataUpdateCoordinator +from .const import LIGHT_CIRCUIT_FUNCTIONS from .entity import ( ScreenLogicCircuitEntity, ScreenLogicPushEntityDescription, ScreenLogicSwitchingEntity, ) - -_LOGGER = logging.getLogger(__name__) +from .types import ScreenLogicConfigEntry @dataclass(frozen=True, kw_only=True) @@ -32,14 +28,12 @@ class ScreenLogicCircuitSwitchDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ScreenLogicConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up entry.""" entities: list[ScreenLogicSwitchingEntity] = [] - coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ - config_entry.entry_id - ] + coordinator = config_entry.runtime_data gateway = coordinator.gateway for circuit_index, circuit_data in gateway.get_data(DEVICE.CIRCUIT).items(): if ( diff --git a/homeassistant/components/screenlogic/types.py b/homeassistant/components/screenlogic/types.py new file mode 100644 index 0000000000000..6ec2bfe92f71c --- /dev/null +++ b/homeassistant/components/screenlogic/types.py @@ -0,0 +1,7 @@ +"""The Screenlogic integration.""" + +from homeassistant.config_entries import ConfigEntry + +from .coordinator import ScreenlogicDataUpdateCoordinator + +type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator]