mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +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 homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import Plenticore
|
||||
from .coordinator import Plenticore, PlenticoreConfigEntry
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
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."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
|
||||
plenticore = Plenticore(hass, entry)
|
||||
|
||||
if not await plenticore.async_setup():
|
||||
return False
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = plenticore
|
||||
entry.runtime_data = plenticore
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
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_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
# remove API object
|
||||
plenticore = hass.data[DOMAIN].pop(entry.entry_id)
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
try:
|
||||
await plenticore.async_unload()
|
||||
await entry.runtime_data.async_unload()
|
||||
except ApiException as err:
|
||||
_LOGGER.error("Error logging out from inverter: %s", err)
|
||||
|
||||
|
@ -30,6 +30,8 @@ from .helper import get_hostname_id
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type PlenticoreConfigEntry = ConfigEntry[Plenticore]
|
||||
|
||||
|
||||
class Plenticore:
|
||||
"""Manages the Plenticore API."""
|
||||
@ -166,12 +168,12 @@ class DataUpdateCoordinatorMixin:
|
||||
class PlenticoreUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
||||
"""Base implementation of DataUpdateCoordinator for Plenticore data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: PlenticoreConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: PlenticoreConfigEntry,
|
||||
logger: logging.Logger,
|
||||
name: str,
|
||||
update_inverval: timedelta,
|
||||
@ -248,12 +250,12 @@ class SettingDataUpdateCoordinator(
|
||||
class PlenticoreSelectUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
||||
"""Base implementation of DataUpdateCoordinator for Plenticore data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: PlenticoreConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: PlenticoreConfigEntry,
|
||||
logger: logging.Logger,
|
||||
name: str,
|
||||
update_inverval: timedelta,
|
||||
|
@ -5,23 +5,21 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
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.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import Plenticore
|
||||
from .coordinator import PlenticoreConfigEntry
|
||||
|
||||
TO_REDACT = {CONF_PASSWORD}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
hass: HomeAssistant, config_entry: PlenticoreConfigEntry
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
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
|
||||
available_process_data = await plenticore.client.get_process_data()
|
||||
|
@ -14,15 +14,13 @@ from homeassistant.components.number import (
|
||||
NumberEntityDescription,
|
||||
NumberMode,
|
||||
)
|
||||
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 homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import SettingDataUpdateCoordinator
|
||||
from .coordinator import PlenticoreConfigEntry, SettingDataUpdateCoordinator
|
||||
from .helper import PlenticoreDataFormatter
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -74,11 +72,11 @@ NUMBER_SETTINGS_DATA = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: PlenticoreConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add Kostal Plenticore Number entities."""
|
||||
plenticore = hass.data[DOMAIN][entry.entry_id]
|
||||
plenticore = entry.runtime_data
|
||||
|
||||
entities = []
|
||||
|
||||
|
@ -7,15 +7,13 @@ from datetime import timedelta
|
||||
import logging
|
||||
|
||||
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 homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import Plenticore, SelectDataUpdateCoordinator
|
||||
from .coordinator import PlenticoreConfigEntry, SelectDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -43,11 +41,11 @@ SELECT_SETTINGS_DATA = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: PlenticoreConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""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()
|
||||
select_data_update_coordinator = SelectDataUpdateCoordinator(
|
||||
|
@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
EntityCategory,
|
||||
@ -29,8 +28,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import ProcessDataUpdateCoordinator
|
||||
from .coordinator import PlenticoreConfigEntry, ProcessDataUpdateCoordinator
|
||||
from .helper import PlenticoreDataFormatter
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -808,11 +806,11 @@ SENSOR_PROCESS_DATA = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: PlenticoreConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add kostal plenticore Sensors."""
|
||||
plenticore = hass.data[DOMAIN][entry.entry_id]
|
||||
plenticore = entry.runtime_data
|
||||
|
||||
entities = []
|
||||
|
||||
|
@ -8,15 +8,13 @@ import logging
|
||||
from typing import Any
|
||||
|
||||
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.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import SettingDataUpdateCoordinator
|
||||
from .coordinator import PlenticoreConfigEntry, SettingDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -49,11 +47,11 @@ SWITCH_SETTINGS_DATA = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: PlenticoreConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add kostal plenticore Switch."""
|
||||
plenticore = hass.data[DOMAIN][entry.entry_id]
|
||||
plenticore = entry.runtime_data
|
||||
|
||||
entities = []
|
||||
|
||||
|
@ -67,7 +67,7 @@ async def test_plenticore_async_setup_g1(
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
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(
|
||||
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)
|
||||
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(
|
||||
configuration_url="http://192.168.1.2",
|
||||
|
Loading…
x
Reference in New Issue
Block a user