mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Migrate kostal_plenticore to use runtime_data (#147188)
This commit is contained in:
parent
d0e77eb1e2
commit
e315cb9859
@ -4,42 +4,35 @@ import logging
|
|||||||
|
|
||||||
from pykoplenti import ApiException
|
from pykoplenti import ApiException
|
||||||
|
|
||||||
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 .const import DOMAIN
|
from .coordinator import Plenticore, PlenticoreConfigEntry
|
||||||
from .coordinator import Plenticore
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = [Platform.NUMBER, Platform.SELECT, Platform.SENSOR, Platform.SWITCH]
|
PLATFORMS = [Platform.NUMBER, Platform.SELECT, Platform.SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: PlenticoreConfigEntry) -> bool:
|
||||||
"""Set up Kostal Plenticore Solar Inverter from a config entry."""
|
"""Set up Kostal Plenticore Solar Inverter from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
|
|
||||||
plenticore = Plenticore(hass, entry)
|
plenticore = Plenticore(hass, entry)
|
||||||
|
|
||||||
if not await plenticore.async_setup():
|
if not await plenticore.async_setup():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = plenticore
|
entry.runtime_data = plenticore
|
||||||
|
|
||||||
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: PlenticoreConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
if unload_ok:
|
|
||||||
# remove API object
|
|
||||||
plenticore = hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
try:
|
try:
|
||||||
await plenticore.async_unload()
|
await entry.runtime_data.async_unload()
|
||||||
except ApiException as err:
|
except ApiException as err:
|
||||||
_LOGGER.error("Error logging out from inverter: %s", err)
|
_LOGGER.error("Error logging out from inverter: %s", err)
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ from .helper import get_hostname_id
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type PlenticoreConfigEntry = ConfigEntry[Plenticore]
|
||||||
|
|
||||||
|
|
||||||
class Plenticore:
|
class Plenticore:
|
||||||
"""Manages the Plenticore API."""
|
"""Manages the Plenticore API."""
|
||||||
@ -166,12 +168,12 @@ class DataUpdateCoordinatorMixin:
|
|||||||
class PlenticoreUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
class PlenticoreUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
||||||
"""Base implementation of DataUpdateCoordinator for Plenticore data."""
|
"""Base implementation of DataUpdateCoordinator for Plenticore data."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: PlenticoreConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: PlenticoreConfigEntry,
|
||||||
logger: logging.Logger,
|
logger: logging.Logger,
|
||||||
name: str,
|
name: str,
|
||||||
update_inverval: timedelta,
|
update_inverval: timedelta,
|
||||||
@ -248,12 +250,12 @@ class SettingDataUpdateCoordinator(
|
|||||||
class PlenticoreSelectUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
class PlenticoreSelectUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
||||||
"""Base implementation of DataUpdateCoordinator for Plenticore data."""
|
"""Base implementation of DataUpdateCoordinator for Plenticore data."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: PlenticoreConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: PlenticoreConfigEntry,
|
||||||
logger: logging.Logger,
|
logger: logging.Logger,
|
||||||
name: str,
|
name: str,
|
||||||
update_inverval: timedelta,
|
update_inverval: timedelta,
|
||||||
|
@ -5,23 +5,21 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import REDACTED, async_redact_data
|
from homeassistant.components.diagnostics import REDACTED, async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import ATTR_IDENTIFIERS, CONF_PASSWORD
|
from homeassistant.const import ATTR_IDENTIFIERS, CONF_PASSWORD
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import PlenticoreConfigEntry
|
||||||
from .coordinator import Plenticore
|
|
||||||
|
|
||||||
TO_REDACT = {CONF_PASSWORD}
|
TO_REDACT = {CONF_PASSWORD}
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: PlenticoreConfigEntry
|
||||||
) -> dict[str, dict[str, Any]]:
|
) -> dict[str, dict[str, Any]]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
data = {"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT)}
|
data = {"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT)}
|
||||||
|
|
||||||
plenticore: Plenticore = hass.data[DOMAIN][config_entry.entry_id]
|
plenticore = config_entry.runtime_data
|
||||||
|
|
||||||
# Get information from Kostal Plenticore library
|
# Get information from Kostal Plenticore library
|
||||||
available_process_data = await plenticore.client.get_process_data()
|
available_process_data = await plenticore.client.get_process_data()
|
||||||
|
@ -14,15 +14,13 @@ from homeassistant.components.number import (
|
|||||||
NumberEntityDescription,
|
NumberEntityDescription,
|
||||||
NumberMode,
|
NumberMode,
|
||||||
)
|
)
|
||||||
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 homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import PlenticoreConfigEntry, SettingDataUpdateCoordinator
|
||||||
from .coordinator import SettingDataUpdateCoordinator
|
|
||||||
from .helper import PlenticoreDataFormatter
|
from .helper import PlenticoreDataFormatter
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -74,11 +72,11 @@ NUMBER_SETTINGS_DATA = [
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: PlenticoreConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add Kostal Plenticore Number entities."""
|
"""Add Kostal Plenticore Number entities."""
|
||||||
plenticore = hass.data[DOMAIN][entry.entry_id]
|
plenticore = entry.runtime_data
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
|
@ -7,15 +7,13 @@ from datetime import timedelta
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
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 homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import PlenticoreConfigEntry, SelectDataUpdateCoordinator
|
||||||
from .coordinator import Plenticore, SelectDataUpdateCoordinator
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -43,11 +41,11 @@ SELECT_SETTINGS_DATA = [
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: PlenticoreConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add kostal plenticore Select widget."""
|
"""Add kostal plenticore Select widget."""
|
||||||
plenticore: Plenticore = hass.data[DOMAIN][entry.entry_id]
|
plenticore = entry.runtime_data
|
||||||
|
|
||||||
available_settings_data = await plenticore.client.get_settings()
|
available_settings_data = await plenticore.client.get_settings()
|
||||||
select_data_update_coordinator = SelectDataUpdateCoordinator(
|
select_data_update_coordinator = SelectDataUpdateCoordinator(
|
||||||
|
@ -14,7 +14,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,
|
||||||
@ -29,8 +28,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import PlenticoreConfigEntry, ProcessDataUpdateCoordinator
|
||||||
from .coordinator import ProcessDataUpdateCoordinator
|
|
||||||
from .helper import PlenticoreDataFormatter
|
from .helper import PlenticoreDataFormatter
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -808,11 +806,11 @@ SENSOR_PROCESS_DATA = [
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: PlenticoreConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add kostal plenticore Sensors."""
|
"""Add kostal plenticore Sensors."""
|
||||||
plenticore = hass.data[DOMAIN][entry.entry_id]
|
plenticore = entry.runtime_data
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
|
@ -8,15 +8,13 @@ import logging
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
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 homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import PlenticoreConfigEntry, SettingDataUpdateCoordinator
|
||||||
from .coordinator import SettingDataUpdateCoordinator
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -49,11 +47,11 @@ SWITCH_SETTINGS_DATA = [
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: PlenticoreConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add kostal plenticore Switch."""
|
"""Add kostal plenticore Switch."""
|
||||||
plenticore = hass.data[DOMAIN][entry.entry_id]
|
plenticore = entry.runtime_data
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ async def test_plenticore_async_setup_g1(
|
|||||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
plenticore = hass.data[DOMAIN][mock_config_entry.entry_id]
|
plenticore = mock_config_entry.runtime_data
|
||||||
|
|
||||||
assert plenticore.device_info == DeviceInfo(
|
assert plenticore.device_info == DeviceInfo(
|
||||||
configuration_url="http://192.168.1.2",
|
configuration_url="http://192.168.1.2",
|
||||||
@ -119,7 +119,7 @@ async def test_plenticore_async_setup_g2(
|
|||||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
plenticore = hass.data[DOMAIN][mock_config_entry.entry_id]
|
plenticore = mock_config_entry.runtime_data
|
||||||
|
|
||||||
assert plenticore.device_info == DeviceInfo(
|
assert plenticore.device_info == DeviceInfo(
|
||||||
configuration_url="http://192.168.1.2",
|
configuration_url="http://192.168.1.2",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user