From 8110b60fc9d45829f216ded6099646dab581eec1 Mon Sep 17 00:00:00 2001 From: Christophe Gagnier Date: Sun, 7 Jul 2024 14:21:28 -0400 Subject: [PATCH] Use ConfigEntry runtime_data in TechnoVE (#121410) --- homeassistant/components/technove/__init__.py | 14 ++++++-------- .../components/technove/binary_sensor.py | 8 +++----- .../components/technove/coordinator.py | 18 ++++++++++-------- .../components/technove/diagnostics.py | 9 +++------ homeassistant/components/technove/number.py | 8 +++----- homeassistant/components/technove/sensor.py | 8 +++----- homeassistant/components/technove/switch.py | 9 ++++----- 7 files changed, 32 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/technove/__init__.py b/homeassistant/components/technove/__init__.py index 7315f6f785c..b886dabc80c 100644 --- a/homeassistant/components/technove/__init__.py +++ b/homeassistant/components/technove/__init__.py @@ -6,18 +6,19 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from .const import DOMAIN from .coordinator import TechnoVEDataUpdateCoordinator PLATFORMS = [Platform.BINARY_SENSOR, Platform.NUMBER, Platform.SENSOR, Platform.SWITCH] +TechnoVEConfigEntry = ConfigEntry[TechnoVEDataUpdateCoordinator] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + +async def async_setup_entry(hass: HomeAssistant, entry: TechnoVEConfigEntry) -> bool: """Set up TechnoVE from a config entry.""" - coordinator = TechnoVEDataUpdateCoordinator(hass) + coordinator = TechnoVEDataUpdateCoordinator(hass, entry) await coordinator.async_config_entry_first_refresh() - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) @@ -26,7 +27,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/technove/binary_sensor.py b/homeassistant/components/technove/binary_sensor.py index e9570397dc1..a1ff9d16baf 100644 --- a/homeassistant/components/technove/binary_sensor.py +++ b/homeassistant/components/technove/binary_sensor.py @@ -12,12 +12,11 @@ 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 +from . import TechnoVEConfigEntry from .coordinator import TechnoVEDataUpdateCoordinator from .entity import TechnoVEEntity @@ -72,13 +71,12 @@ BINARY_SENSORS = [ async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: TechnoVEConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the binary sensor platform.""" - coordinator: TechnoVEDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] async_add_entities( - TechnoVEBinarySensorEntity(coordinator, description) + TechnoVEBinarySensorEntity(entry.runtime_data, description) for description in BINARY_SENSORS ) diff --git a/homeassistant/components/technove/coordinator.py b/homeassistant/components/technove/coordinator.py index c89219e698f..8527c6e543a 100644 --- a/homeassistant/components/technove/coordinator.py +++ b/homeassistant/components/technove/coordinator.py @@ -2,9 +2,10 @@ from __future__ import annotations +from typing import TYPE_CHECKING + from technove import Station as TechnoVEStation, TechnoVE, TechnoVEError -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -12,24 +13,25 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda from .const import DOMAIN, LOGGER, SCAN_INTERVAL +if TYPE_CHECKING: + from . import TechnoVEConfigEntry + class TechnoVEDataUpdateCoordinator(DataUpdateCoordinator[TechnoVEStation]): """Class to manage fetching TechnoVE data from single endpoint.""" - config_entry: ConfigEntry - - def __init__(self, hass: HomeAssistant) -> None: + def __init__(self, hass: HomeAssistant, entry: TechnoVEConfigEntry) -> None: """Initialize global TechnoVE data updater.""" + self.technove = TechnoVE( + entry.data[CONF_HOST], + session=async_get_clientsession(hass), + ) super().__init__( hass, LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL, ) - self.technove = TechnoVE( - self.config_entry.data[CONF_HOST], - session=async_get_clientsession(hass), - ) async def _async_update_data(self) -> TechnoVEStation: """Fetch data from TechnoVE.""" diff --git a/homeassistant/components/technove/diagnostics.py b/homeassistant/components/technove/diagnostics.py index 11a530ef263..f070d58ab6f 100644 --- a/homeassistant/components/technove/diagnostics.py +++ b/homeassistant/components/technove/diagnostics.py @@ -6,18 +6,15 @@ from dataclasses import asdict from typing import Any from homeassistant.components.diagnostics import async_redact_data -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from .const import DOMAIN -from .coordinator import TechnoVEDataUpdateCoordinator +from . import TechnoVEConfigEntry TO_REDACT = {"unique_id", "mac_address"} async def async_get_config_entry_diagnostics( - hass: HomeAssistant, entry: ConfigEntry + hass: HomeAssistant, entry: TechnoVEConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - coordinator: TechnoVEDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] - return async_redact_data(asdict(coordinator.data.info), TO_REDACT) + return async_redact_data(asdict(entry.runtime_data.data.info), TO_REDACT) diff --git a/homeassistant/components/technove/number.py b/homeassistant/components/technove/number.py index 9f2af47c24f..a1cf094c6bf 100644 --- a/homeassistant/components/technove/number.py +++ b/homeassistant/components/technove/number.py @@ -14,12 +14,12 @@ 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 ServiceValidationError from homeassistant.helpers.entity_platform import AddEntitiesCallback +from . import TechnoVEConfigEntry from .const import DOMAIN from .coordinator import TechnoVEDataUpdateCoordinator from .entity import TechnoVEEntity @@ -65,14 +65,12 @@ NUMBERS = [ async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: TechnoVEConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up TechnoVE number entity based on a config entry.""" - coordinator: TechnoVEDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] - async_add_entities( - TechnoVENumberEntity(coordinator, description) for description in NUMBERS + TechnoVENumberEntity(entry.runtime_data, description) for description in NUMBERS ) diff --git a/homeassistant/components/technove/sensor.py b/homeassistant/components/technove/sensor.py index 6f8e63ffbc6..e16ac23f89c 100644 --- a/homeassistant/components/technove/sensor.py +++ b/homeassistant/components/technove/sensor.py @@ -13,7 +13,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( SIGNAL_STRENGTH_DECIBELS_MILLIWATT, EntityCategory, @@ -25,7 +24,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from .const import DOMAIN +from . import TechnoVEConfigEntry from .coordinator import TechnoVEDataUpdateCoordinator from .entity import TechnoVEEntity @@ -122,13 +121,12 @@ SENSORS: tuple[TechnoVESensorEntityDescription, ...] = ( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: TechnoVEConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the sensor platform.""" - coordinator: TechnoVEDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] async_add_entities( - TechnoVESensorEntity(coordinator, description) for description in SENSORS + TechnoVESensorEntity(entry.runtime_data, description) for description in SENSORS ) diff --git a/homeassistant/components/technove/switch.py b/homeassistant/components/technove/switch.py index 72c95f676d9..bb9250215be 100644 --- a/homeassistant/components/technove/switch.py +++ b/homeassistant/components/technove/switch.py @@ -9,12 +9,11 @@ from typing import Any from technove import Station as TechnoVEStation, TechnoVE from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription -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 +from . import TechnoVEConfigEntry from .coordinator import TechnoVEDataUpdateCoordinator from .entity import TechnoVEEntity from .helpers import technove_exception_handler @@ -43,14 +42,14 @@ SWITCHES = [ async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: TechnoVEConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up TechnoVE switch based on a config entry.""" - coordinator: TechnoVEDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] async_add_entities( - TechnoVESwitchEntity(coordinator, description) for description in SWITCHES + TechnoVESwitchEntity(entry.runtime_data, description) + for description in SWITCHES )