Use runtime_data in dynalite (#136448)

* Use runtime_data in dynalite

* Delay listener
This commit is contained in:
epenet 2025-01-27 10:16:33 +01:00 committed by GitHub
parent 1e0165c5f7
commit acb9d68706
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 24 additions and 28 deletions

View File

@ -16,11 +16,11 @@ from .services import setup_services
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
type DynaliteConfigEntry = ConfigEntry[DynaliteBridge]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Dynalite platform."""
hass.data[DOMAIN] = {}
setup_services(hass)
await async_register_dynalite_frontend(hass)
@ -28,35 +28,30 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
async def async_entry_changed(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def async_entry_changed(hass: HomeAssistant, entry: DynaliteConfigEntry) -> None:
"""Reload entry since the data has changed."""
LOGGER.debug("Reconfiguring entry %s", entry.data)
bridge = hass.data[DOMAIN][entry.entry_id]
bridge = entry.runtime_data
bridge.reload_config(entry.data)
LOGGER.debug("Reconfiguring entry finished %s", entry.data)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: DynaliteConfigEntry) -> bool:
"""Set up a bridge from a config entry."""
LOGGER.debug("Setting up entry %s", entry.data)
bridge = DynaliteBridge(hass, convert_config(entry.data))
# need to do it before the listener
hass.data[DOMAIN][entry.entry_id] = bridge
entry.async_on_unload(entry.add_update_listener(async_entry_changed))
if not await bridge.async_setup():
LOGGER.error("Could not set up bridge for entry %s", entry.data)
hass.data[DOMAIN][entry.entry_id] = None
raise ConfigEntryNotReady
entry.runtime_data = bridge
entry.async_on_unload(entry.add_update_listener(async_entry_changed))
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: DynaliteConfigEntry) -> bool:
"""Unload a config entry."""
LOGGER.debug("Unloading entry %s", entry.data)
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -16,6 +16,7 @@ from dynalite_devices_lib.dynalite_devices import (
DynaliteNotification,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -23,6 +24,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import ATTR_AREA, ATTR_HOST, ATTR_PACKET, ATTR_PRESET, LOGGER, PLATFORMS
from .convert_config import convert_config
type DynaliteConfigEntry = ConfigEntry[DynaliteBridge]
class DynaliteBridge:
"""Manages a single Dynalite bridge."""

View File

@ -7,18 +7,17 @@ from homeassistant.components.cover import (
CoverDeviceClass,
CoverEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from .bridge import DynaliteBridge
from .bridge import DynaliteBridge, DynaliteConfigEntry
from .entity import DynaliteBase, async_setup_entry_base
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: DynaliteConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Record the async_add_entities function to add them later when received from Dynalite."""

View File

@ -6,27 +6,26 @@ from abc import ABC, abstractmethod
from collections.abc import Callable
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from .bridge import DynaliteBridge
from .bridge import DynaliteBridge, DynaliteConfigEntry
from .const import DOMAIN, LOGGER
def async_setup_entry_base(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: DynaliteConfigEntry,
async_add_entities: AddEntitiesCallback,
platform: str,
entity_from_device: Callable,
) -> None:
"""Record the async_add_entities function to add them later when received from Dynalite."""
LOGGER.debug("Setting up %s entry = %s", platform, config_entry.data)
bridge = hass.data[DOMAIN][config_entry.entry_id]
bridge = config_entry.runtime_data
@callback
def async_add_entities_platform(devices):

View File

@ -3,16 +3,16 @@
from typing import Any
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .bridge import DynaliteConfigEntry
from .entity import DynaliteBase, async_setup_entry_base
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: DynaliteConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Record the async_add_entities function to add them later when received from Dynalite."""

View File

@ -23,9 +23,9 @@ from .const import (
def _get_bridges(service_call: ServiceCall) -> list[DynaliteBridge]:
host = service_call.data.get(ATTR_HOST, "")
bridges = [
bridge
for bridge in service_call.hass.data[DOMAIN].values()
if not host or bridge.host == host
entry.runtime_data
for entry in service_call.hass.config_entries.async_loaded_entries(DOMAIN)
if not host or entry.runtime_data.host == host
]
LOGGER.debug("Selected bridges for service call: %s", bridges)
return bridges

View File

@ -3,17 +3,17 @@
from typing import Any
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .bridge import DynaliteConfigEntry
from .entity import DynaliteBase, async_setup_entry_base
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: DynaliteConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Record the async_add_entities function to add them later when received from Dynalite."""