Skip to content

Commit

Permalink
Migrate screenlogic to use entry.runtime_data (home-assistant#121644)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 10, 2024
1 parent 6f15352 commit fb758bd
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 67 deletions.
21 changes: 13 additions & 8 deletions homeassistant/components/screenlogic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 3 additions & 9 deletions homeassistant/components/screenlogic/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,23 +14,20 @@
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,
ScreenLogicEntityDescription,
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(
Expand Down Expand Up @@ -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] = [
Expand Down
10 changes: 3 additions & 7 deletions homeassistant/components/screenlogic/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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

Expand Down
10 changes: 3 additions & 7 deletions homeassistant/components/screenlogic/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 4 additions & 10 deletions homeassistant/components/screenlogic/light.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 (
Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/screenlogic/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
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,
ScreenLogicEntityDescription,
ScreenLogicPushEntity,
ScreenLogicPushEntityDescription,
)
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/screenlogic/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
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,
ScreenLogicEntityDescription,
ScreenLogicPushEntity,
ScreenLogicPushEntityDescription,
)
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -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] = [
Expand Down
11 changes: 7 additions & 4 deletions homeassistant/components/screenlogic/services.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -29,6 +30,7 @@
SUPPORTED_COLOR_MODES,
)
from .coordinator import ScreenlogicDataUpdateCoordinator
from .types import ScreenLogicConfigEntry

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down
14 changes: 4 additions & 10 deletions homeassistant/components/screenlogic/switch.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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 (
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/screenlogic/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""The Screenlogic integration."""

from homeassistant.config_entries import ConfigEntry

from .coordinator import ScreenlogicDataUpdateCoordinator

type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator]

0 comments on commit fb758bd

Please sign in to comment.