Use built in config entry from coordinator in HomeWizard (#102959)

pull/103026/head
Joost Lekkerkerker 2023-10-29 18:23:48 +01:00 committed by GitHub
parent 94e192db12
commit b7667d44fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 32 deletions

View File

@ -1,6 +1,5 @@
"""The Homewizard integration."""
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
from homeassistant.const import CONF_IP_ADDRESS
from homeassistant.core import HomeAssistant
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:
"""Set up Homewizard from a config entry."""
coordinator = Coordinator(hass, entry, entry.data[CONF_IP_ADDRESS])
coordinator = Coordinator(hass)
try:
await coordinator.async_config_entry_first_refresh()

View File

@ -18,7 +18,7 @@ async def async_setup_entry(
"""Set up the Identify button."""
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
if coordinator.supports_identify():
async_add_entities([HomeWizardIdentifyButton(coordinator, entry)])
async_add_entities([HomeWizardIdentifyButton(coordinator)])
class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
@ -27,14 +27,10 @@ class HomeWizardIdentifyButton(HomeWizardEntity, ButtonEntity):
_attr_entity_category = EntityCategory.CONFIG
_attr_device_class = ButtonDeviceClass.IDENTIFY
def __init__(
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
entry: ConfigEntry,
) -> None:
def __init__(self, coordinator: HWEnergyDeviceUpdateCoordinator) -> None:
"""Initialize button."""
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
async def async_press(self) -> None:

View File

@ -9,6 +9,7 @@ from homewizard_energy.errors import DisabledError, RequestError, UnsupportedErr
from homewizard_energy.models import Device
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_IP_ADDRESS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@ -26,16 +27,18 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
_unsupported_error: bool = False
config_entry: ConfigEntry
def __init__(
self,
hass: HomeAssistant,
entry: ConfigEntry,
host: str,
) -> None:
"""Initialize update coordinator."""
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
self.entry = entry
self.api = HomeWizardEnergy(host, clientsession=async_get_clientsession(hass))
self.api = HomeWizardEnergy(
self.config_entry.data[CONF_IP_ADDRESS],
clientsession=async_get_clientsession(hass),
)
async def _async_update_data(self) -> DeviceResponseEntry:
"""Fetch all device and sensor data from api."""
@ -58,7 +61,7 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
self._unsupported_error = True
_LOGGER.warning(
"%s is running an outdated firmware version (%s). Contact HomeWizard support to update your device",
self.entry.title,
self.config_entry.title,
ex,
)
@ -71,7 +74,9 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
# Do not reload when performing first refresh
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

View File

@ -16,7 +16,7 @@ class HomeWizardEntity(CoordinatorEntity[HWEnergyDeviceUpdateCoordinator]):
def __init__(self, coordinator: HWEnergyDeviceUpdateCoordinator) -> None:
"""Initialize the HomeWizard entity."""
super().__init__(coordinator=coordinator)
super().__init__(coordinator)
self._attr_device_info = DeviceInfo(
manufacturer="HomeWizard",
sw_version=coordinator.data.device.firmware_version,

View File

@ -32,7 +32,9 @@ def homewizard_exception_handler(
except RequestError as ex:
raise HomeAssistantError from 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
return handler

View File

@ -21,7 +21,7 @@ async def async_setup_entry(
"""Set up numbers for device."""
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
if coordinator.supports_state():
async_add_entities([HWEnergyNumberEntity(coordinator, entry)])
async_add_entities([HWEnergyNumberEntity(coordinator)])
class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
@ -35,11 +35,12 @@ class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
def __init__(
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
entry: ConfigEntry,
) -> None:
"""Initialize the control number."""
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
async def async_set_native_value(self, value: float) -> None:

View File

@ -26,6 +26,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import DOMAIN
from .coordinator import HWEnergyDeviceUpdateCoordinator
@ -39,7 +40,7 @@ class HomeWizardEntityDescriptionMixin:
"""Mixin values for HomeWizard entities."""
has_fn: Callable[[Data], bool]
value_fn: Callable[[Data], float | int | str | None]
value_fn: Callable[[Data], StateType]
@dataclass
@ -433,7 +434,7 @@ async def async_setup_entry(
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
HomeWizardSensorEntity(coordinator, entry, description)
HomeWizardSensorEntity(coordinator, description)
for description in SENSORS
if description.has_fn(coordinator.data.data)
)
@ -447,18 +448,17 @@ class HomeWizardSensorEntity(HomeWizardEntity, SensorEntity):
def __init__(
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
entry: ConfigEntry,
description: HomeWizardSensorEntityDescription,
) -> None:
"""Initialize Sensor Domain."""
super().__init__(coordinator)
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):
self._attr_entity_registry_enabled_default = False
@property
def native_value(self) -> float | int | str | None:
def native_value(self) -> StateType:
"""Return the sensor value."""
return self.entity_description.value_fn(self.coordinator.data.data)

View File

@ -86,11 +86,7 @@ async def async_setup_entry(
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
HomeWizardSwitchEntity(
coordinator=coordinator,
description=description,
entry=entry,
)
HomeWizardSwitchEntity(coordinator, description)
for description in SWITCHES
if description.create_fn(coordinator)
)
@ -105,12 +101,11 @@ class HomeWizardSwitchEntity(HomeWizardEntity, SwitchEntity):
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
description: HomeWizardSwitchEntityDescription,
entry: ConfigEntry,
) -> None:
"""Initialize the switch."""
super().__init__(coordinator)
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
def icon(self) -> str | None: