Fix yolink device unavailable on startup (#72579)
* fetch device state on startup * Suggest change * suggest fix * fix * fix * Fix suggest * suggest fix
This commit is contained in:
parent
1d57626ff0
commit
7ff1b53d4f
8 changed files with 177 additions and 161 deletions
|
@ -19,7 +19,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.util import percentage
|
||||
|
||||
from .const import (
|
||||
ATTR_COORDINATOR,
|
||||
ATTR_COORDINATORS,
|
||||
ATTR_DEVICE_DOOR_SENSOR,
|
||||
ATTR_DEVICE_MOTION_SENSOR,
|
||||
ATTR_DEVICE_TH_SENSOR,
|
||||
|
@ -54,7 +54,9 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
|
|||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value=lambda value: percentage.ordered_list_item_to_percentage(
|
||||
[1, 2, 3, 4], value
|
||||
),
|
||||
)
|
||||
if value is not None
|
||||
else None,
|
||||
exists_fn=lambda device: device.device_type
|
||||
in [ATTR_DEVICE_DOOR_SENSOR, ATTR_DEVICE_TH_SENSOR, ATTR_DEVICE_MOTION_SENSOR],
|
||||
),
|
||||
|
@ -89,18 +91,21 @@ async def async_setup_entry(
|
|||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up YoLink Sensor from a config entry."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATOR]
|
||||
sensor_devices = [
|
||||
device
|
||||
for device in coordinator.yl_devices
|
||||
if device.device_type in SENSOR_DEVICE_TYPE
|
||||
device_coordinators = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATORS]
|
||||
sensor_device_coordinators = [
|
||||
device_coordinator
|
||||
for device_coordinator in device_coordinators.values()
|
||||
if device_coordinator.device.device_type in SENSOR_DEVICE_TYPE
|
||||
]
|
||||
entities = []
|
||||
for sensor_device in sensor_devices:
|
||||
for sensor_device_coordinator in sensor_device_coordinators:
|
||||
for description in SENSOR_TYPES:
|
||||
if description.exists_fn(sensor_device):
|
||||
if description.exists_fn(sensor_device_coordinator.device):
|
||||
entities.append(
|
||||
YoLinkSensorEntity(coordinator, description, sensor_device)
|
||||
YoLinkSensorEntity(
|
||||
sensor_device_coordinator,
|
||||
description,
|
||||
)
|
||||
)
|
||||
async_add_entities(entities)
|
||||
|
||||
|
@ -114,18 +119,21 @@ class YoLinkSensorEntity(YoLinkEntity, SensorEntity):
|
|||
self,
|
||||
coordinator: YoLinkCoordinator,
|
||||
description: YoLinkSensorEntityDescription,
|
||||
device: YoLinkDevice,
|
||||
) -> None:
|
||||
"""Init YoLink Sensor."""
|
||||
super().__init__(coordinator, device)
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device.device_id} {self.entity_description.key}"
|
||||
self._attr_name = f"{device.device_name} ({self.entity_description.name})"
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.device_id} {self.entity_description.key}"
|
||||
)
|
||||
self._attr_name = (
|
||||
f"{coordinator.device.device_name} ({self.entity_description.name})"
|
||||
)
|
||||
|
||||
@callback
|
||||
def update_entity_state(self, state: dict) -> None:
|
||||
"""Update HA Entity State."""
|
||||
self._attr_native_value = self.entity_description.value(
|
||||
state[self.entity_description.key]
|
||||
state.get(self.entity_description.key)
|
||||
)
|
||||
self.async_write_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue