Use ConfigEntry runtime_data in P1 Monitor (#131048)
parent
74f68316c8
commit
a0ee8eac37
|
@ -7,10 +7,12 @@ from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .const import LOGGER
|
||||
from .coordinator import P1MonitorDataUpdateCoordinator
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
PLATFORMS: list[Platform] = [Platform.SENSOR]
|
||||
|
||||
type P1MonitorConfigEntry = ConfigEntry[P1MonitorDataUpdateCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
@ -23,8 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
await coordinator.p1monitor.close()
|
||||
raise
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
|
@ -55,7 +56,4 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
|||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload P1 Monitor config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -11,13 +11,11 @@ from homeassistant.const import CONF_HOST, CONF_PORT
|
|||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
SERVICE_PHASES,
|
||||
SERVICE_SETTINGS,
|
||||
SERVICE_SMARTMETER,
|
||||
SERVICE_WATERMETER,
|
||||
)
|
||||
from .coordinator import P1MonitorDataUpdateCoordinator
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from _typeshed import DataclassInstance
|
||||
|
@ -29,23 +27,21 @@ async def async_get_config_entry_diagnostics(
|
|||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: P1MonitorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
data = {
|
||||
"entry": {
|
||||
"title": entry.title,
|
||||
"data": async_redact_data(entry.data, TO_REDACT),
|
||||
},
|
||||
"data": {
|
||||
"smartmeter": asdict(coordinator.data[SERVICE_SMARTMETER]),
|
||||
"phases": asdict(coordinator.data[SERVICE_PHASES]),
|
||||
"settings": asdict(coordinator.data[SERVICE_SETTINGS]),
|
||||
"smartmeter": asdict(entry.runtime_data.data[SERVICE_SMARTMETER]),
|
||||
"phases": asdict(entry.runtime_data.data[SERVICE_PHASES]),
|
||||
"settings": asdict(entry.runtime_data.data[SERVICE_SETTINGS]),
|
||||
},
|
||||
}
|
||||
|
||||
if coordinator.has_water_meter:
|
||||
if entry.runtime_data.has_water_meter:
|
||||
data["data"]["watermeter"] = asdict(
|
||||
cast("DataclassInstance", coordinator.data[SERVICE_WATERMETER])
|
||||
cast("DataclassInstance", entry.runtime_data.data[SERVICE_WATERMETER])
|
||||
)
|
||||
|
||||
return data
|
||||
|
|
|
@ -239,11 +239,10 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up P1 Monitor Sensors based on a config entry."""
|
||||
coordinator: P1MonitorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
entities: list[P1MonitorSensorEntity] = []
|
||||
entities.extend(
|
||||
P1MonitorSensorEntity(
|
||||
coordinator=coordinator,
|
||||
entry=entry,
|
||||
description=description,
|
||||
name="SmartMeter",
|
||||
service=SERVICE_SMARTMETER,
|
||||
|
@ -252,7 +251,7 @@ async def async_setup_entry(
|
|||
)
|
||||
entities.extend(
|
||||
P1MonitorSensorEntity(
|
||||
coordinator=coordinator,
|
||||
entry=entry,
|
||||
description=description,
|
||||
name="Phases",
|
||||
service=SERVICE_PHASES,
|
||||
|
@ -261,17 +260,17 @@ async def async_setup_entry(
|
|||
)
|
||||
entities.extend(
|
||||
P1MonitorSensorEntity(
|
||||
coordinator=coordinator,
|
||||
entry=entry,
|
||||
description=description,
|
||||
name="Settings",
|
||||
service=SERVICE_SETTINGS,
|
||||
)
|
||||
for description in SENSORS_SETTINGS
|
||||
)
|
||||
if coordinator.has_water_meter:
|
||||
if entry.runtime_data.has_water_meter:
|
||||
entities.extend(
|
||||
P1MonitorSensorEntity(
|
||||
coordinator=coordinator,
|
||||
entry=entry,
|
||||
description=description,
|
||||
name="WaterMeter",
|
||||
service=SERVICE_WATERMETER,
|
||||
|
@ -291,24 +290,26 @@ class P1MonitorSensorEntity(
|
|||
def __init__(
|
||||
self,
|
||||
*,
|
||||
coordinator: P1MonitorDataUpdateCoordinator,
|
||||
entry: ConfigEntry,
|
||||
description: SensorEntityDescription,
|
||||
name: str,
|
||||
service: Literal["smartmeter", "watermeter", "phases", "settings"],
|
||||
) -> None:
|
||||
"""Initialize P1 Monitor sensor."""
|
||||
super().__init__(coordinator=coordinator)
|
||||
super().__init__(coordinator=entry.runtime_data)
|
||||
self._service_key = service
|
||||
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.config_entry.entry_id}_{service}_{description.key}"
|
||||
f"{entry.runtime_data.config_entry.entry_id}_{service}_{description.key}"
|
||||
)
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
identifiers={(DOMAIN, f"{coordinator.config_entry.entry_id}_{service}")},
|
||||
configuration_url=f"http://{coordinator.config_entry.data[CONF_HOST]}",
|
||||
identifiers={
|
||||
(DOMAIN, f"{entry.runtime_data.config_entry.entry_id}_{service}")
|
||||
},
|
||||
configuration_url=f"http://{entry.runtime_data.config_entry.data[CONF_HOST]}",
|
||||
manufacturer="P1 Monitor",
|
||||
name=name,
|
||||
)
|
||||
|
|
|
@ -26,7 +26,6 @@ async def test_load_unload_config_entry(
|
|||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not hass.data.get(DOMAIN)
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue