diff --git a/homeassistant/components/goodwe/__init__.py b/homeassistant/components/goodwe/__init__.py index 02c1d5beac7..b6637bc8b50 100644 --- a/homeassistant/components/goodwe/__init__.py +++ b/homeassistant/components/goodwe/__init__.py @@ -2,26 +2,17 @@ from goodwe import InverterError, connect -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.device_registry import DeviceInfo -from .const import ( - CONF_MODEL_FAMILY, - DOMAIN, - KEY_COORDINATOR, - KEY_DEVICE_INFO, - KEY_INVERTER, - PLATFORMS, -) -from .coordinator import GoodweUpdateCoordinator +from .const import CONF_MODEL_FAMILY, DOMAIN, PLATFORMS +from .coordinator import GoodweConfigEntry, GoodweRuntimeData, GoodweUpdateCoordinator -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: GoodweConfigEntry) -> bool: """Set up the Goodwe components from a config entry.""" - hass.data.setdefault(DOMAIN, {}) host = entry.data[CONF_HOST] model_family = entry.data[CONF_MODEL_FAMILY] @@ -50,11 +41,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Fetch initial data so we have data when entities subscribe await coordinator.async_config_entry_first_refresh() - hass.data[DOMAIN][entry.entry_id] = { - KEY_INVERTER: inverter, - KEY_COORDINATOR: coordinator, - KEY_DEVICE_INFO: device_info, - } + entry.runtime_data = GoodweRuntimeData( + inverter=inverter, + coordinator=coordinator, + device_info=device_info, + ) entry.async_on_unload(entry.add_update_listener(update_listener)) @@ -63,18 +54,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, config_entry: GoodweConfigEntry +) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms( - config_entry, PLATFORMS - ) - - if unload_ok: - hass.data[DOMAIN].pop(config_entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) -async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None: +async def update_listener(hass: HomeAssistant, config_entry: GoodweConfigEntry) -> None: """Handle options update.""" await hass.config_entries.async_reload(config_entry.entry_id) diff --git a/homeassistant/components/goodwe/button.py b/homeassistant/components/goodwe/button.py index e93b23570db..64d1e08276d 100644 --- a/homeassistant/components/goodwe/button.py +++ b/homeassistant/components/goodwe/button.py @@ -8,13 +8,12 @@ import logging from goodwe import Inverter, InverterError from homeassistant.components.button import ButtonEntity, ButtonEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER +from .coordinator import GoodweConfigEntry _LOGGER = logging.getLogger(__name__) @@ -36,12 +35,12 @@ SYNCHRONIZE_CLOCK = GoodweButtonEntityDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: GoodweConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the inverter button entities from a config entry.""" - inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER] - device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO] + inverter = config_entry.runtime_data.inverter + device_info = config_entry.runtime_data.device_info # read current time from the inverter try: diff --git a/homeassistant/components/goodwe/const.py b/homeassistant/components/goodwe/const.py index 730433c4a66..432d18e5867 100644 --- a/homeassistant/components/goodwe/const.py +++ b/homeassistant/components/goodwe/const.py @@ -12,7 +12,3 @@ DEFAULT_NAME = "GoodWe" SCAN_INTERVAL = timedelta(seconds=10) CONF_MODEL_FAMILY = "model_family" - -KEY_INVERTER = "inverter" -KEY_COORDINATOR = "coordinator" -KEY_DEVICE_INFO = "device_info" diff --git a/homeassistant/components/goodwe/coordinator.py b/homeassistant/components/goodwe/coordinator.py index 914ba3155b4..3236b95d9e0 100644 --- a/homeassistant/components/goodwe/coordinator.py +++ b/homeassistant/components/goodwe/coordinator.py @@ -2,6 +2,7 @@ from __future__ import annotations +from dataclasses import dataclass import logging from typing import Any @@ -9,22 +10,34 @@ from goodwe import Inverter, InverterError, RequestFailedException from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import SCAN_INTERVAL _LOGGER = logging.getLogger(__name__) +type GoodweConfigEntry = ConfigEntry[GoodweRuntimeData] + + +@dataclass +class GoodweRuntimeData: + """Data class for runtime data.""" + + inverter: Inverter + coordinator: GoodweUpdateCoordinator + device_info: DeviceInfo + class GoodweUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): """Gather data for the energy device.""" - config_entry: ConfigEntry + config_entry: GoodweConfigEntry def __init__( self, hass: HomeAssistant, - entry: ConfigEntry, + entry: GoodweConfigEntry, inverter: Inverter, ) -> None: """Initialize update coordinator.""" diff --git a/homeassistant/components/goodwe/diagnostics.py b/homeassistant/components/goodwe/diagnostics.py index 66806d31589..ece5f3b6507 100644 --- a/homeassistant/components/goodwe/diagnostics.py +++ b/homeassistant/components/goodwe/diagnostics.py @@ -4,19 +4,16 @@ from __future__ import annotations from typing import Any -from goodwe import Inverter - -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from .const import DOMAIN, KEY_INVERTER +from .coordinator import GoodweConfigEntry async def async_get_config_entry_diagnostics( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: GoodweConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - inverter: Inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER] + inverter = config_entry.runtime_data.inverter return { "config_entry": config_entry.as_dict(), diff --git a/homeassistant/components/goodwe/number.py b/homeassistant/components/goodwe/number.py index 0a61ac19d64..0d200c2725c 100644 --- a/homeassistant/components/goodwe/number.py +++ b/homeassistant/components/goodwe/number.py @@ -13,13 +13,13 @@ from homeassistant.components.number import ( NumberEntity, NumberEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfPower from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER +from .const import DOMAIN +from .coordinator import GoodweConfigEntry _LOGGER = logging.getLogger(__name__) @@ -86,12 +86,12 @@ NUMBERS = ( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: GoodweConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the inverter select entities from a config entry.""" - inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER] - device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO] + inverter = config_entry.runtime_data.inverter + device_info = config_entry.runtime_data.device_info entities = [] diff --git a/homeassistant/components/goodwe/select.py b/homeassistant/components/goodwe/select.py index 340e10bfa0f..c26e8135b3f 100644 --- a/homeassistant/components/goodwe/select.py +++ b/homeassistant/components/goodwe/select.py @@ -5,13 +5,13 @@ import logging from goodwe import Inverter, InverterError, OperationMode from homeassistant.components.select import SelectEntity, SelectEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER +from .const import DOMAIN +from .coordinator import GoodweConfigEntry _LOGGER = logging.getLogger(__name__) @@ -39,12 +39,12 @@ OPERATION_MODE = SelectEntityDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: GoodweConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the inverter select entities from a config entry.""" - inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER] - device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO] + inverter = config_entry.runtime_data.inverter + device_info = config_entry.runtime_data.device_info supported_modes = await inverter.get_operation_modes(False) # read current operating mode from the inverter diff --git a/homeassistant/components/goodwe/sensor.py b/homeassistant/components/goodwe/sensor.py index d2dce2770e4..c51827712d4 100644 --- a/homeassistant/components/goodwe/sensor.py +++ b/homeassistant/components/goodwe/sensor.py @@ -17,7 +17,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( PERCENTAGE, EntityCategory, @@ -39,8 +38,8 @@ from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt as dt_util -from .const import DOMAIN, KEY_COORDINATOR, KEY_DEVICE_INFO, KEY_INVERTER -from .coordinator import GoodweUpdateCoordinator +from .const import DOMAIN +from .coordinator import GoodweConfigEntry, GoodweUpdateCoordinator _LOGGER = logging.getLogger(__name__) @@ -165,14 +164,14 @@ TEXT_SENSOR = GoodweSensorEntityDescription( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: GoodweConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the GoodWe inverter from a config entry.""" entities: list[InverterSensor] = [] - inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER] - coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] - device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO] + inverter = config_entry.runtime_data.inverter + coordinator = config_entry.runtime_data.coordinator + device_info = config_entry.runtime_data.device_info # Individual inverter sensors entities entities.extend(