mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Migrate HomeWizard to config entry runtime data (#122088)
This commit is contained in:
parent
7a4e40ade0
commit
e6dec7c856
@ -5,12 +5,14 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import DOMAIN, PLATFORMS
|
from .const import DOMAIN, PLATFORMS
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator as Coordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
|
|
||||||
|
type HomeWizardConfigEntry = ConfigEntry[HWEnergyDeviceUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Homewizard from a config entry."""
|
"""Set up Homewizard from a config entry."""
|
||||||
coordinator = Coordinator(hass)
|
coordinator = HWEnergyDeviceUpdateCoordinator(hass)
|
||||||
try:
|
try:
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
@ -22,7 +24,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
raise
|
raise
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
# Abort reauth config flow if active
|
# Abort reauth config flow if active
|
||||||
for progress_flow in hass.config_entries.flow.async_progress_by_handler(DOMAIN):
|
for progress_flow in hass.config_entries.flow.async_progress_by_handler(DOMAIN):
|
||||||
@ -41,6 +43,5 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
coordinator = hass.data[DOMAIN].pop(entry.entry_id)
|
entry.runtime_data.api.close()
|
||||||
await coordinator.api.close()
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
"""Support for HomeWizard buttons."""
|
"""Support for HomeWizard buttons."""
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
||||||
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.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import HomeWizardConfigEntry
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
from .entity import HomeWizardEntity
|
from .entity import HomeWizardEntity
|
||||||
from .helpers import homewizard_exception_handler
|
from .helpers import homewizard_exception_handler
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: HomeWizardConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Identify button."""
|
"""Set up the Identify button."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
if entry.runtime_data.supports_identify():
|
||||||
if coordinator.supports_identify():
|
async_add_entities([HomeWizardIdentifyButton(entry.runtime_data)])
|
||||||
async_add_entities([HomeWizardIdentifyButton(coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
|
class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
|
||||||
|
@ -6,12 +6,10 @@ from dataclasses import asdict
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_IP_ADDRESS
|
from homeassistant.const import CONF_IP_ADDRESS
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import HomeWizardConfigEntry
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
|
||||||
|
|
||||||
TO_REDACT = {
|
TO_REDACT = {
|
||||||
CONF_IP_ADDRESS,
|
CONF_IP_ADDRESS,
|
||||||
@ -24,25 +22,25 @@ TO_REDACT = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: HomeWizardConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data.data
|
||||||
|
|
||||||
state: dict[str, Any] | None = None
|
state: dict[str, Any] | None = None
|
||||||
if coordinator.data.state:
|
if data.state:
|
||||||
state = asdict(coordinator.data.state)
|
state = asdict(data.state)
|
||||||
|
|
||||||
system: dict[str, Any] | None = None
|
system: dict[str, Any] | None = None
|
||||||
if coordinator.data.system:
|
if data.system:
|
||||||
system = asdict(coordinator.data.system)
|
system = asdict(data.system)
|
||||||
|
|
||||||
return async_redact_data(
|
return async_redact_data(
|
||||||
{
|
{
|
||||||
"entry": async_redact_data(entry.data, TO_REDACT),
|
"entry": async_redact_data(entry.data, TO_REDACT),
|
||||||
"data": {
|
"data": {
|
||||||
"device": asdict(coordinator.data.device),
|
"device": asdict(data.device),
|
||||||
"data": asdict(coordinator.data.data),
|
"data": asdict(data.data),
|
||||||
"state": state,
|
"state": state,
|
||||||
"system": system,
|
"system": system,
|
||||||
},
|
},
|
||||||
|
@ -3,13 +3,12 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.number import NumberEntity
|
from homeassistant.components.number import NumberEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import PERCENTAGE, EntityCategory
|
from homeassistant.const import PERCENTAGE, EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.color import brightness_to_value, value_to_brightness
|
from homeassistant.util.color import brightness_to_value, value_to_brightness
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import HomeWizardConfigEntry
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
from .entity import HomeWizardEntity
|
from .entity import HomeWizardEntity
|
||||||
from .helpers import homewizard_exception_handler
|
from .helpers import homewizard_exception_handler
|
||||||
@ -17,13 +16,12 @@ from .helpers import homewizard_exception_handler
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: HomeWizardConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up numbers for device."""
|
"""Set up numbers for device."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
if entry.runtime_data.supports_state():
|
||||||
if coordinator.supports_state():
|
async_add_entities([HWEnergyNumberEntity(entry.runtime_data)])
|
||||||
async_add_entities([HWEnergyNumberEntity(coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
||||||
|
@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_VIA_DEVICE,
|
ATTR_VIA_DEVICE,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -36,6 +35,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
|
from . import HomeWizardConfigEntry
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
from .entity import HomeWizardEntity
|
from .entity import HomeWizardEntity
|
||||||
@ -619,24 +619,25 @@ EXTERNAL_SENSORS = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: HomeWizardConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize sensors."""
|
"""Initialize sensors."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
# Migrate original gas meter sensor to ExternalDevice
|
# Migrate original gas meter sensor to ExternalDevice
|
||||||
# This is sensor that was directly linked to the P1 Meter
|
# This is sensor that was directly linked to the P1 Meter
|
||||||
# Migration can be removed after 2024.8.0
|
# Migration can be removed after 2024.8.0
|
||||||
ent_reg = er.async_get(hass)
|
ent_reg = er.async_get(hass)
|
||||||
|
data = entry.runtime_data.data.data
|
||||||
if (
|
if (
|
||||||
entity_id := ent_reg.async_get_entity_id(
|
entity_id := ent_reg.async_get_entity_id(
|
||||||
Platform.SENSOR, DOMAIN, f"{entry.unique_id}_total_gas_m3"
|
Platform.SENSOR, DOMAIN, f"{entry.unique_id}_total_gas_m3"
|
||||||
)
|
)
|
||||||
) and coordinator.data.data.gas_unique_id is not None:
|
) and data.gas_unique_id is not None:
|
||||||
ent_reg.async_update_entity(
|
ent_reg.async_update_entity(
|
||||||
entity_id,
|
entity_id,
|
||||||
new_unique_id=f"{DOMAIN}_gas_meter_{coordinator.data.data.gas_unique_id}",
|
new_unique_id=f"{DOMAIN}_gas_meter_{data.gas_unique_id}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Remove old gas_unique_id sensor
|
# Remove old gas_unique_id sensor
|
||||||
@ -647,14 +648,14 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
# Initialize default sensors
|
# Initialize default sensors
|
||||||
entities: list = [
|
entities: list = [
|
||||||
HomeWizardSensorEntity(coordinator, description)
|
HomeWizardSensorEntity(entry.runtime_data, description)
|
||||||
for description in SENSORS
|
for description in SENSORS
|
||||||
if description.has_fn(coordinator.data.data)
|
if description.has_fn(data)
|
||||||
]
|
]
|
||||||
|
|
||||||
# Initialize external devices
|
# Initialize external devices
|
||||||
if coordinator.data.data.external_devices is not None:
|
if data.external_devices is not None:
|
||||||
for unique_id, device in coordinator.data.data.external_devices.items():
|
for unique_id, device in data.external_devices.items():
|
||||||
if description := EXTERNAL_SENSORS.get(device.meter_type):
|
if description := EXTERNAL_SENSORS.get(device.meter_type):
|
||||||
# Migrate external devices to new unique_id
|
# Migrate external devices to new unique_id
|
||||||
# This is to ensure that devices with same id but different type are unique
|
# This is to ensure that devices with same id but different type are unique
|
||||||
@ -669,7 +670,9 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
# Add external device
|
# Add external device
|
||||||
entities.append(
|
entities.append(
|
||||||
HomeWizardExternalSensorEntity(coordinator, description, unique_id)
|
HomeWizardExternalSensorEntity(
|
||||||
|
entry.runtime_data, description, unique_id
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
@ -13,12 +13,12 @@ from homeassistant.components.switch import (
|
|||||||
SwitchEntity,
|
SwitchEntity,
|
||||||
SwitchEntityDescription,
|
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.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN, DeviceResponseEntry
|
from . import HomeWizardConfigEntry
|
||||||
|
from .const import DeviceResponseEntry
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
from .entity import HomeWizardEntity
|
from .entity import HomeWizardEntity
|
||||||
from .helpers import homewizard_exception_handler
|
from .helpers import homewizard_exception_handler
|
||||||
@ -67,16 +67,14 @@ SWITCHES = [
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: HomeWizardConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up switches."""
|
"""Set up switches."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
HomeWizardSwitchEntity(coordinator, description)
|
HomeWizardSwitchEntity(entry.runtime_data, description)
|
||||||
for description in SWITCHES
|
for description in SWITCHES
|
||||||
if description.create_fn(coordinator)
|
if description.create_fn(entry.runtime_data)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user