mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Use runtime_data in goodwe (#144325)
This commit is contained in:
parent
0ec7dc5654
commit
32a6b8a0f8
@ -2,26 +2,17 @@
|
|||||||
|
|
||||||
from goodwe import InverterError, connect
|
from goodwe import InverterError, connect
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
|
||||||
from .const import (
|
from .const import CONF_MODEL_FAMILY, DOMAIN, PLATFORMS
|
||||||
CONF_MODEL_FAMILY,
|
from .coordinator import GoodweConfigEntry, GoodweRuntimeData, GoodweUpdateCoordinator
|
||||||
DOMAIN,
|
|
||||||
KEY_COORDINATOR,
|
|
||||||
KEY_DEVICE_INFO,
|
|
||||||
KEY_INVERTER,
|
|
||||||
PLATFORMS,
|
|
||||||
)
|
|
||||||
from .coordinator import 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."""
|
"""Set up the Goodwe components from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
model_family = entry.data[CONF_MODEL_FAMILY]
|
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
|
# Fetch initial data so we have data when entities subscribe
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
entry.runtime_data = GoodweRuntimeData(
|
||||||
KEY_INVERTER: inverter,
|
inverter=inverter,
|
||||||
KEY_COORDINATOR: coordinator,
|
coordinator=coordinator,
|
||||||
KEY_DEVICE_INFO: device_info,
|
device_info=device_info,
|
||||||
}
|
)
|
||||||
|
|
||||||
entry.async_on_unload(entry.add_update_listener(update_listener))
|
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
|
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 a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||||
config_entry, PLATFORMS
|
|
||||||
)
|
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
async def update_listener(hass: HomeAssistant, config_entry: GoodweConfigEntry) -> None:
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
await hass.config_entries.async_reload(config_entry.entry_id)
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
||||||
|
@ -8,13 +8,12 @@ import logging
|
|||||||
from goodwe import Inverter, InverterError
|
from goodwe import Inverter, InverterError
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN, KEY_DEVICE_INFO, KEY_INVERTER
|
from .coordinator import GoodweConfigEntry
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -36,12 +35,12 @@ SYNCHRONIZE_CLOCK = GoodweButtonEntityDescription(
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: GoodweConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the inverter button entities from a config entry."""
|
"""Set up the inverter button entities from a config entry."""
|
||||||
inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
|
inverter = config_entry.runtime_data.inverter
|
||||||
device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
|
device_info = config_entry.runtime_data.device_info
|
||||||
|
|
||||||
# read current time from the inverter
|
# read current time from the inverter
|
||||||
try:
|
try:
|
||||||
|
@ -12,7 +12,3 @@ DEFAULT_NAME = "GoodWe"
|
|||||||
SCAN_INTERVAL = timedelta(seconds=10)
|
SCAN_INTERVAL = timedelta(seconds=10)
|
||||||
|
|
||||||
CONF_MODEL_FAMILY = "model_family"
|
CONF_MODEL_FAMILY = "model_family"
|
||||||
|
|
||||||
KEY_INVERTER = "inverter"
|
|
||||||
KEY_COORDINATOR = "coordinator"
|
|
||||||
KEY_DEVICE_INFO = "device_info"
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -9,22 +10,34 @@ from goodwe import Inverter, InverterError, RequestFailedException
|
|||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import SCAN_INTERVAL
|
from .const import SCAN_INTERVAL
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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]]):
|
class GoodweUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||||
"""Gather data for the energy device."""
|
"""Gather data for the energy device."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: GoodweConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: GoodweConfigEntry,
|
||||||
inverter: Inverter,
|
inverter: Inverter,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize update coordinator."""
|
"""Initialize update coordinator."""
|
||||||
|
@ -4,19 +4,16 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from goodwe import Inverter
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN, KEY_INVERTER
|
from .coordinator import GoodweConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: GoodweConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
inverter: Inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
|
inverter = config_entry.runtime_data.inverter
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"config_entry": config_entry.as_dict(),
|
"config_entry": config_entry.as_dict(),
|
||||||
|
@ -13,13 +13,13 @@ from homeassistant.components.number import (
|
|||||||
NumberEntity,
|
NumberEntity,
|
||||||
NumberEntityDescription,
|
NumberEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfPower
|
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfPower
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -86,12 +86,12 @@ NUMBERS = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: GoodweConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the inverter select entities from a config entry."""
|
"""Set up the inverter select entities from a config entry."""
|
||||||
inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
|
inverter = config_entry.runtime_data.inverter
|
||||||
device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
|
device_info = config_entry.runtime_data.device_info
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
|
@ -5,13 +5,13 @@ import logging
|
|||||||
from goodwe import Inverter, InverterError, OperationMode
|
from goodwe import Inverter, InverterError, OperationMode
|
||||||
|
|
||||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -39,12 +39,12 @@ OPERATION_MODE = SelectEntityDescription(
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: GoodweConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the inverter select entities from a config entry."""
|
"""Set up the inverter select entities from a config entry."""
|
||||||
inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
|
inverter = config_entry.runtime_data.inverter
|
||||||
device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
|
device_info = config_entry.runtime_data.device_info
|
||||||
|
|
||||||
supported_modes = await inverter.get_operation_modes(False)
|
supported_modes = await inverter.get_operation_modes(False)
|
||||||
# read current operating mode from the inverter
|
# read current operating mode from the inverter
|
||||||
|
@ -17,7 +17,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
@ -39,8 +38,8 @@ from homeassistant.helpers.typing import StateType
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN, KEY_COORDINATOR, KEY_DEVICE_INFO, KEY_INVERTER
|
from .const import DOMAIN
|
||||||
from .coordinator import GoodweUpdateCoordinator
|
from .coordinator import GoodweConfigEntry, GoodweUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -165,14 +164,14 @@ TEXT_SENSOR = GoodweSensorEntityDescription(
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: GoodweConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the GoodWe inverter from a config entry."""
|
"""Set up the GoodWe inverter from a config entry."""
|
||||||
entities: list[InverterSensor] = []
|
entities: list[InverterSensor] = []
|
||||||
inverter = hass.data[DOMAIN][config_entry.entry_id][KEY_INVERTER]
|
inverter = config_entry.runtime_data.inverter
|
||||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
|
coordinator = config_entry.runtime_data.coordinator
|
||||||
device_info = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE_INFO]
|
device_info = config_entry.runtime_data.device_info
|
||||||
|
|
||||||
# Individual inverter sensors entities
|
# Individual inverter sensors entities
|
||||||
entities.extend(
|
entities.extend(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user