Use ConfigEntry.runtime_data in govee_light_local (#128998)

This commit is contained in:
Jan-Philipp Benecke 2024-10-25 11:29:06 +02:00 committed by GitHub
parent daf0939f09
commit 897ed7e381
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 16 deletions

View File

@ -9,23 +9,21 @@ import logging
from govee_local_api.controller import LISTENING_PORT from govee_local_api.controller import LISTENING_PORT
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from .const import DISCOVERY_TIMEOUT, DOMAIN from .const import DISCOVERY_TIMEOUT
from .coordinator import GoveeLocalApiCoordinator from .coordinator import GoveeLocalApiCoordinator, GoveeLocalConfigEntry
PLATFORMS: list[Platform] = [Platform.LIGHT] PLATFORMS: list[Platform] = [Platform.LIGHT]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: GoveeLocalConfigEntry) -> bool:
"""Set up Govee light local from a config entry.""" """Set up Govee light local from a config entry."""
coordinator = GoveeLocalApiCoordinator(hass=hass)
coordinator: GoveeLocalApiCoordinator = GoveeLocalApiCoordinator(hass=hass)
async def await_cleanup(): async def await_cleanup():
cleanup_complete: asyncio.Event = coordinator.cleanup() cleanup_complete: asyncio.Event = coordinator.cleanup()
@ -52,14 +50,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except TimeoutError as ex: except TimeoutError as ex:
raise ConfigEntryNotReady from ex raise ConfigEntryNotReady from ex
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: GoveeLocalConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -6,6 +6,7 @@ import logging
from govee_local_api import GoveeController, GoveeDevice from govee_local_api import GoveeController, GoveeDevice
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
@ -19,6 +20,8 @@ from .const import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type GoveeLocalConfigEntry = ConfigEntry[GoveeLocalApiCoordinator]
class GoveeLocalApiCoordinator(DataUpdateCoordinator[list[GoveeDevice]]): class GoveeLocalApiCoordinator(DataUpdateCoordinator[list[GoveeDevice]]):
"""Govee light local coordinator.""" """Govee light local coordinator."""

View File

@ -15,26 +15,25 @@ from homeassistant.components.light import (
LightEntity, LightEntity,
filter_supported_color_modes, filter_supported_color_modes,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, MANUFACTURER from .const import DOMAIN, MANUFACTURER
from .coordinator import GoveeLocalApiCoordinator from .coordinator import GoveeLocalApiCoordinator, GoveeLocalConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: GoveeLocalConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Govee light setup.""" """Govee light setup."""
coordinator: GoveeLocalApiCoordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = config_entry.runtime_data
def discovery_callback(device: GoveeDevice, is_new: bool) -> bool: def discovery_callback(device: GoveeDevice, is_new: bool) -> bool:
if is_new: if is_new: