Use built in config entry from coordinator in HomeWizard (#102959)
parent
94e192db12
commit
b7667d44fd
|
@ -1,6 +1,5 @@
|
||||||
"""The Homewizard integration."""
|
"""The Homewizard integration."""
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
||||||
from homeassistant.const import CONF_IP_ADDRESS
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
|
@ -10,7 +9,7 @@ from .coordinator import HWEnergyDeviceUpdateCoordinator as Coordinator
|
||||||
|
|
||||||
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, entry, entry.data[CONF_IP_ADDRESS])
|
coordinator = Coordinator(hass)
|
||||||
try:
|
try:
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ async def async_setup_entry(
|
||||||
"""Set up the Identify button."""
|
"""Set up the Identify button."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
if coordinator.supports_identify():
|
if coordinator.supports_identify():
|
||||||
async_add_entities([HomeWizardIdentifyButton(coordinator, entry)])
|
async_add_entities([HomeWizardIdentifyButton(coordinator)])
|
||||||
|
|
||||||
|
|
||||||
class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
|
class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
|
||||||
|
@ -27,14 +27,10 @@ class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
|
||||||
_attr_entity_category = EntityCategory.CONFIG
|
_attr_entity_category = EntityCategory.CONFIG
|
||||||
_attr_device_class = ButtonDeviceClass.IDENTIFY
|
_attr_device_class = ButtonDeviceClass.IDENTIFY
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, coordinator: HWEnergyDeviceUpdateCoordinator) -> None:
|
||||||
self,
|
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator,
|
|
||||||
entry: ConfigEntry,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize button."""
|
"""Initialize button."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_unique_id = f"{entry.unique_id}_identify"
|
self._attr_unique_id = f"{coordinator.config_entry.unique_id}_identify"
|
||||||
|
|
||||||
@homewizard_exception_handler
|
@homewizard_exception_handler
|
||||||
async def async_press(self) -> None:
|
async def async_press(self) -> None:
|
||||||
|
|
|
@ -9,6 +9,7 @@ from homewizard_energy.errors import DisabledError, RequestError, UnsupportedErr
|
||||||
from homewizard_energy.models import Device
|
from homewizard_energy.models import Device
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import CONF_IP_ADDRESS
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
@ -26,16 +27,18 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
|
||||||
|
|
||||||
_unsupported_error: bool = False
|
_unsupported_error: bool = False
|
||||||
|
|
||||||
|
config_entry: ConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
|
||||||
host: str,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize update coordinator."""
|
"""Initialize update coordinator."""
|
||||||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
|
||||||
self.entry = entry
|
self.api = HomeWizardEnergy(
|
||||||
self.api = HomeWizardEnergy(host, clientsession=async_get_clientsession(hass))
|
self.config_entry.data[CONF_IP_ADDRESS],
|
||||||
|
clientsession=async_get_clientsession(hass),
|
||||||
|
)
|
||||||
|
|
||||||
async def _async_update_data(self) -> DeviceResponseEntry:
|
async def _async_update_data(self) -> DeviceResponseEntry:
|
||||||
"""Fetch all device and sensor data from api."""
|
"""Fetch all device and sensor data from api."""
|
||||||
|
@ -58,7 +61,7 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
|
||||||
self._unsupported_error = True
|
self._unsupported_error = True
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"%s is running an outdated firmware version (%s). Contact HomeWizard support to update your device",
|
"%s is running an outdated firmware version (%s). Contact HomeWizard support to update your device",
|
||||||
self.entry.title,
|
self.config_entry.title,
|
||||||
ex,
|
ex,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,7 +74,9 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
|
||||||
|
|
||||||
# Do not reload when performing first refresh
|
# Do not reload when performing first refresh
|
||||||
if self.data is not None:
|
if self.data is not None:
|
||||||
await self.hass.config_entries.async_reload(self.entry.entry_id)
|
await self.hass.config_entries.async_reload(
|
||||||
|
self.config_entry.entry_id
|
||||||
|
)
|
||||||
|
|
||||||
raise UpdateFailed(ex) from ex
|
raise UpdateFailed(ex) from ex
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class HomeWizardEntity(CoordinatorEntity[HWEnergyDeviceUpdateCoordinator]):
|
||||||
|
|
||||||
def __init__(self, coordinator: HWEnergyDeviceUpdateCoordinator) -> None:
|
def __init__(self, coordinator: HWEnergyDeviceUpdateCoordinator) -> None:
|
||||||
"""Initialize the HomeWizard entity."""
|
"""Initialize the HomeWizard entity."""
|
||||||
super().__init__(coordinator=coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
manufacturer="HomeWizard",
|
manufacturer="HomeWizard",
|
||||||
sw_version=coordinator.data.device.firmware_version,
|
sw_version=coordinator.data.device.firmware_version,
|
||||||
|
|
|
@ -32,7 +32,9 @@ def homewizard_exception_handler(
|
||||||
except RequestError as ex:
|
except RequestError as ex:
|
||||||
raise HomeAssistantError from ex
|
raise HomeAssistantError from ex
|
||||||
except DisabledError as ex:
|
except DisabledError as ex:
|
||||||
await self.hass.config_entries.async_reload(self.coordinator.entry.entry_id)
|
await self.hass.config_entries.async_reload(
|
||||||
|
self.coordinator.config_entry.entry_id
|
||||||
|
)
|
||||||
raise HomeAssistantError from ex
|
raise HomeAssistantError from ex
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
|
@ -21,7 +21,7 @@ async def async_setup_entry(
|
||||||
"""Set up numbers for device."""
|
"""Set up numbers for device."""
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
if coordinator.supports_state():
|
if coordinator.supports_state():
|
||||||
async_add_entities([HWEnergyNumberEntity(coordinator, entry)])
|
async_add_entities([HWEnergyNumberEntity(coordinator)])
|
||||||
|
|
||||||
|
|
||||||
class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
||||||
|
@ -35,11 +35,12 @@ class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator,
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
||||||
entry: ConfigEntry,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the control number."""
|
"""Initialize the control number."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_unique_id = f"{entry.unique_id}_status_light_brightness"
|
self._attr_unique_id = (
|
||||||
|
f"{coordinator.config_entry.unique_id}_status_light_brightness"
|
||||||
|
)
|
||||||
|
|
||||||
@homewizard_exception_handler
|
@homewizard_exception_handler
|
||||||
async def async_set_native_value(self, value: float) -> None:
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
|
|
|
@ -26,6 +26,7 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
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.helpers.typing import StateType
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
|
@ -39,7 +40,7 @@ class HomeWizardEntityDescriptionMixin:
|
||||||
"""Mixin values for HomeWizard entities."""
|
"""Mixin values for HomeWizard entities."""
|
||||||
|
|
||||||
has_fn: Callable[[Data], bool]
|
has_fn: Callable[[Data], bool]
|
||||||
value_fn: Callable[[Data], float | int | str | None]
|
value_fn: Callable[[Data], StateType]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -433,7 +434,7 @@ async def async_setup_entry(
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
HomeWizardSensorEntity(coordinator, entry, description)
|
HomeWizardSensorEntity(coordinator, description)
|
||||||
for description in SENSORS
|
for description in SENSORS
|
||||||
if description.has_fn(coordinator.data.data)
|
if description.has_fn(coordinator.data.data)
|
||||||
)
|
)
|
||||||
|
@ -447,18 +448,17 @@ class HomeWizardSensorEntity(HomeWizardEntity, SensorEntity):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator,
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
||||||
entry: ConfigEntry,
|
|
||||||
description: HomeWizardSensorEntityDescription,
|
description: HomeWizardSensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Sensor Domain."""
|
"""Initialize Sensor Domain."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
|
self._attr_unique_id = f"{coordinator.config_entry.unique_id}_{description.key}"
|
||||||
if not description.enabled_fn(self.coordinator.data.data):
|
if not description.enabled_fn(self.coordinator.data.data):
|
||||||
self._attr_entity_registry_enabled_default = False
|
self._attr_entity_registry_enabled_default = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | int | str | None:
|
def native_value(self) -> StateType:
|
||||||
"""Return the sensor value."""
|
"""Return the sensor value."""
|
||||||
return self.entity_description.value_fn(self.coordinator.data.data)
|
return self.entity_description.value_fn(self.coordinator.data.data)
|
||||||
|
|
||||||
|
|
|
@ -86,11 +86,7 @@ async def async_setup_entry(
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
HomeWizardSwitchEntity(
|
HomeWizardSwitchEntity(coordinator, description)
|
||||||
coordinator=coordinator,
|
|
||||||
description=description,
|
|
||||||
entry=entry,
|
|
||||||
)
|
|
||||||
for description in SWITCHES
|
for description in SWITCHES
|
||||||
if description.create_fn(coordinator)
|
if description.create_fn(coordinator)
|
||||||
)
|
)
|
||||||
|
@ -105,12 +101,11 @@ class HomeWizardSwitchEntity(HomeWizardEntity, SwitchEntity):
|
||||||
self,
|
self,
|
||||||
coordinator: HWEnergyDeviceUpdateCoordinator,
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
||||||
description: HomeWizardSwitchEntityDescription,
|
description: HomeWizardSwitchEntityDescription,
|
||||||
entry: ConfigEntry,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
|
self._attr_unique_id = f"{coordinator.config_entry.unique_id}_{description.key}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str | None:
|
def icon(self) -> str | None:
|
||||||
|
|
Loading…
Reference in New Issue