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 .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)

View File

@ -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 @@ from homeassistant.components.binary_sensor import (
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 @@ from .entity import (
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 @@ SUPPORTED_SCG_SENSORS = [
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] = [

View File

@ -18,16 +18,14 @@ from homeassistant.components.climate import (
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 @@ SUPPORTED_PRESETS = [
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

View File

@ -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(),

View File

@ -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 @@ from homeassistant.components.light import (
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 (

View File

@ -14,13 +14,11 @@ from homeassistant.components.number import (
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 @@ from .entity import (
ScreenLogicPushEntity,
ScreenLogicPushEntityDescription,
)
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit
_LOGGER = logging.getLogger(__name__)
@ -98,14 +97,12 @@ SUPPORTED_SCG_NUMBERS = [
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:

View File

@ -18,12 +18,10 @@ from homeassistant.components.sensor import (
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 @@ from .entity import (
ScreenLogicPushEntity,
ScreenLogicPushEntityDescription,
)
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit
_LOGGER = logging.getLogger(__name__)
@ -227,13 +226,11 @@ SUPPORTED_SCG_SENSORS = [
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] = [

View File

@ -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 @@ from .const import (
SUPPORTED_COLOR_MODES,
)
from .coordinator import ScreenlogicDataUpdateCoordinator
from .types import ScreenLogicConfigEntry
_LOGGER = logging.getLogger(__name__)
@ -103,8 +105,9 @@ def async_load_screenlogic_services(hass: HomeAssistant):
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 @@ def async_load_screenlogic_services(hass: HomeAssistant):
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

View File

@ -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 (

View File

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