Migrate screenlogic to use entry.runtime_data (#121644)

pull/121658/head
J. Nick Koston 2024-07-09 23:36:02 -07:00 committed by GitHub
parent 6f15352eda
commit fb758bd8b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 50 additions and 67 deletions

View File

@ -20,6 +20,9 @@ from .data import ENTITY_MIGRATIONS
from .services import async_load_screenlogic_services from .services import async_load_screenlogic_services
from .util import generate_unique_id from .util import generate_unique_id
type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -49,7 +52,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True 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.""" """Set up Screenlogic from a config entry."""
await _async_migrate_entries(hass, 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)) 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) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True 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 a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok: if unload_ok:
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
await coordinator.gateway.async_disconnect() await coordinator.gateway.async_disconnect()
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok 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.""" """Handle options update."""
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)
async def _async_migrate_entries( async def _async_migrate_entries(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: ScreenLogicConfigEntry
) -> None: ) -> None:
"""Migrate to new entity names.""" """Migrate to new entity names."""
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)

View File

@ -2,7 +2,6 @@
from copy import copy from copy import copy
import dataclasses import dataclasses
import logging
from screenlogicpy.const.common import ON_OFF from screenlogicpy.const.common import ON_OFF
from screenlogicpy.const.data import ATTR, DEVICE, GROUP, VALUE from screenlogicpy.const.data import ATTR, DEVICE, GROUP, VALUE
@ -15,12 +14,10 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN as SL_DOMAIN
from .coordinator import ScreenlogicDataUpdateCoordinator from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import ( from .entity import (
ScreenLogicEntity, ScreenLogicEntity,
@ -28,10 +25,9 @@ from .entity import (
ScreenLogicPushEntity, ScreenLogicPushEntity,
ScreenLogicPushEntityDescription, ScreenLogicPushEntityDescription,
) )
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity from .util import cleanup_excluded_entity
_LOGGER = logging.getLogger(__name__)
@dataclasses.dataclass(frozen=True, kw_only=True) @dataclasses.dataclass(frozen=True, kw_only=True)
class ScreenLogicBinarySensorDescription( class ScreenLogicBinarySensorDescription(
@ -171,13 +167,11 @@ SUPPORTED_SCG_SENSORS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
gateway = coordinator.gateway gateway = coordinator.gateway
entities: list[ScreenLogicBinarySensor] = [ entities: list[ScreenLogicBinarySensor] = [

View File

@ -18,16 +18,14 @@ from homeassistant.components.climate import (
HVACAction, HVACAction,
HVACMode, HVACMode,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from .const import DOMAIN as SL_DOMAIN
from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import ScreenLogicPushEntity, ScreenLogicPushEntityDescription from .entity import ScreenLogicPushEntity, ScreenLogicPushEntityDescription
from .types import ScreenLogicConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -43,13 +41,11 @@ SUPPORTED_PRESETS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
gateway = coordinator.gateway gateway = coordinator.gateway

View File

@ -2,20 +2,16 @@
from typing import Any from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN from .types import ScreenLogicConfigEntry
from .coordinator import ScreenlogicDataUpdateCoordinator
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry hass: HomeAssistant, config_entry: ScreenLogicConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
return { return {
"config_entry": config_entry.as_dict(), "config_entry": config_entry.as_dict(),

View File

@ -1,7 +1,6 @@
"""Support for a ScreenLogic light 'circuit' switch.""" """Support for a ScreenLogic light 'circuit' switch."""
from dataclasses import dataclass from dataclasses import dataclass
import logging
from screenlogicpy.const.data import ATTR, DEVICE from screenlogicpy.const.data import ATTR, DEVICE
from screenlogicpy.const.msg import CODE from screenlogicpy.const.msg import CODE
@ -12,27 +11,22 @@ from homeassistant.components.light import (
LightEntity, LightEntity,
LightEntityDescription, LightEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN as SL_DOMAIN, LIGHT_CIRCUIT_FUNCTIONS from .const import LIGHT_CIRCUIT_FUNCTIONS
from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import ScreenLogicCircuitEntity, ScreenLogicPushEntityDescription from .entity import ScreenLogicCircuitEntity, ScreenLogicPushEntityDescription
from .types import ScreenLogicConfigEntry
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
entities: list[ScreenLogicLight] = [] entities: list[ScreenLogicLight] = []
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
gateway = coordinator.gateway gateway = coordinator.gateway
for circuit_index, circuit_data in gateway.get_data(DEVICE.CIRCUIT).items(): for circuit_index, circuit_data in gateway.get_data(DEVICE.CIRCUIT).items():
if ( if (

View File

@ -14,13 +14,11 @@ from homeassistant.components.number import (
NumberEntityDescription, NumberEntityDescription,
NumberMode, NumberMode,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN as SL_DOMAIN
from .coordinator import ScreenlogicDataUpdateCoordinator from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import ( from .entity import (
ScreenLogicEntity, ScreenLogicEntity,
@ -28,6 +26,7 @@ from .entity import (
ScreenLogicPushEntity, ScreenLogicPushEntity,
ScreenLogicPushEntityDescription, ScreenLogicPushEntityDescription,
) )
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit from .util import cleanup_excluded_entity, get_ha_unit
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -98,14 +97,12 @@ SUPPORTED_SCG_NUMBERS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
entities: list[ScreenLogicNumber] = [] entities: list[ScreenLogicNumber] = []
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
gateway = coordinator.gateway gateway = coordinator.gateway
for chem_number_description in SUPPORTED_INTELLICHEM_NUMBERS: for chem_number_description in SUPPORTED_INTELLICHEM_NUMBERS:

View File

@ -18,12 +18,10 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN as SL_DOMAIN
from .coordinator import ScreenlogicDataUpdateCoordinator from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import ( from .entity import (
ScreenLogicEntity, ScreenLogicEntity,
@ -31,6 +29,7 @@ from .entity import (
ScreenLogicPushEntity, ScreenLogicPushEntity,
ScreenLogicPushEntityDescription, ScreenLogicPushEntityDescription,
) )
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit from .util import cleanup_excluded_entity, get_ha_unit
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -227,13 +226,11 @@ SUPPORTED_SCG_SENSORS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
gateway = coordinator.gateway gateway = coordinator.gateway
entities: list[ScreenLogicSensor] = [ entities: list[ScreenLogicSensor] = [

View File

@ -1,12 +1,13 @@
"""Services for ScreenLogic integration.""" """Services for ScreenLogic integration."""
import logging import logging
from typing import cast
from screenlogicpy import ScreenLogicError from screenlogicpy import ScreenLogicError
from screenlogicpy.device_const.system import EQUIPMENT_FLAG from screenlogicpy.device_const.system import EQUIPMENT_FLAG
import voluptuous as vol 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.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import ( from homeassistant.helpers import (
@ -29,6 +30,7 @@ from .const import (
SUPPORTED_COLOR_MODES, SUPPORTED_COLOR_MODES,
) )
from .coordinator import ScreenlogicDataUpdateCoordinator from .coordinator import ScreenlogicDataUpdateCoordinator
from .types import ScreenLogicConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -103,8 +105,9 @@ def async_load_screenlogic_services(hass: HomeAssistant):
coordinators: list[ScreenlogicDataUpdateCoordinator] = [] coordinators: list[ScreenlogicDataUpdateCoordinator] = []
for entry_id in entry_ids: for entry_id in entry_ids:
config_entry: ConfigEntry | None = hass.config_entries.async_get_entry( config_entry = cast(
entry_id ScreenLogicConfigEntry | None,
hass.config_entries.async_get_entry(entry_id),
) )
if not config_entry: if not config_entry:
raise ServiceValidationError( raise ServiceValidationError(
@ -121,7 +124,7 @@ def async_load_screenlogic_services(hass: HomeAssistant):
f"Failed to call service '{service_call.service}'. Config entry " f"Failed to call service '{service_call.service}'. Config entry "
f"'{entry_id}' not loaded" f"'{entry_id}' not loaded"
) )
coordinators.append(hass.data[DOMAIN][entry_id]) coordinators.append(config_entry.runtime_data)
return coordinators return coordinators

View File

@ -1,26 +1,22 @@
"""Support for a ScreenLogic 'circuit' switch.""" """Support for a ScreenLogic 'circuit' switch."""
from dataclasses import dataclass from dataclasses import dataclass
import logging
from screenlogicpy.const.data import ATTR, DEVICE from screenlogicpy.const.data import ATTR, DEVICE
from screenlogicpy.const.msg import CODE from screenlogicpy.const.msg import CODE
from screenlogicpy.device_const.circuit import GENERIC_CIRCUIT_NAMES, INTERFACE from screenlogicpy.device_const.circuit import GENERIC_CIRCUIT_NAMES, INTERFACE
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN as SL_DOMAIN, LIGHT_CIRCUIT_FUNCTIONS from .const import LIGHT_CIRCUIT_FUNCTIONS
from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import ( from .entity import (
ScreenLogicCircuitEntity, ScreenLogicCircuitEntity,
ScreenLogicPushEntityDescription, ScreenLogicPushEntityDescription,
ScreenLogicSwitchingEntity, ScreenLogicSwitchingEntity,
) )
from .types import ScreenLogicConfigEntry
_LOGGER = logging.getLogger(__name__)
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
@ -32,14 +28,12 @@ class ScreenLogicCircuitSwitchDescription(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up entry.""" """Set up entry."""
entities: list[ScreenLogicSwitchingEntity] = [] entities: list[ScreenLogicSwitchingEntity] = []
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][ coordinator = config_entry.runtime_data
config_entry.entry_id
]
gateway = coordinator.gateway gateway = coordinator.gateway
for circuit_index, circuit_data in gateway.get_data(DEVICE.CIRCUIT).items(): for circuit_index, circuit_data in gateway.get_data(DEVICE.CIRCUIT).items():
if ( if (

View File

@ -0,0 +1,7 @@
"""The Screenlogic integration."""
from homeassistant.config_entries import ConfigEntry
from .coordinator import ScreenlogicDataUpdateCoordinator
type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator]