Report unavailability for yolink sensor and binary_sensor (#100743)

This commit is contained in:
hlyi 2023-10-01 06:21:26 -05:00 committed by Franck Nijhof
parent 5106907571
commit c4d85ac41f
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
4 changed files with 21 additions and 2 deletions

View File

@ -136,3 +136,8 @@ class YoLinkBinarySensorEntity(YoLinkEntity, BinarySensorEntity):
state.get(self.entity_description.state_key) state.get(self.entity_description.state_key)
) )
self.async_write_ha_state() self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return true is device is available."""
return super().available and self.coordinator.dev_online

View File

@ -8,3 +8,4 @@ ATTR_DEVICE_NAME = "name"
ATTR_DEVICE_STATE = "state" ATTR_DEVICE_STATE = "state"
ATTR_DEVICE_ID = "deviceId" ATTR_DEVICE_ID = "deviceId"
YOLINK_EVENT = f"{DOMAIN}_event" YOLINK_EVENT = f"{DOMAIN}_event"
YOLINK_OFFLINE_TIME = 32400

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from datetime import timedelta from datetime import UTC, datetime, timedelta
import logging import logging
from yolink.device import YoLinkDevice from yolink.device import YoLinkDevice
@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import ATTR_DEVICE_STATE, DOMAIN from .const import ATTR_DEVICE_STATE, DOMAIN, YOLINK_OFFLINE_TIME
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -37,6 +37,7 @@ class YoLinkCoordinator(DataUpdateCoordinator[dict]):
) )
self.device = device self.device = device
self.paired_device = paired_device self.paired_device = paired_device
self.dev_online = True
async def _async_update_data(self) -> dict: async def _async_update_data(self) -> dict:
"""Fetch device state.""" """Fetch device state."""
@ -44,6 +45,13 @@ class YoLinkCoordinator(DataUpdateCoordinator[dict]):
async with asyncio.timeout(10): async with asyncio.timeout(10):
device_state_resp = await self.device.fetch_state() device_state_resp = await self.device.fetch_state()
device_state = device_state_resp.data.get(ATTR_DEVICE_STATE) device_state = device_state_resp.data.get(ATTR_DEVICE_STATE)
device_reporttime = device_state_resp.data.get("reportAt")
if device_reporttime is not None:
rpt_time_delta = (
datetime.now(tz=UTC).replace(tzinfo=None)
- datetime.strptime(device_reporttime, "%Y-%m-%dT%H:%M:%S.%fZ")
).total_seconds()
self.dev_online = rpt_time_delta < YOLINK_OFFLINE_TIME
if self.paired_device is not None and device_state is not None: if self.paired_device is not None and device_state is not None:
paried_device_state_resp = await self.paired_device.fetch_state() paried_device_state_resp = await self.paired_device.fetch_state()
paried_device_state = paried_device_state_resp.data.get( paried_device_state = paried_device_state_resp.data.get(

View File

@ -261,3 +261,8 @@ class YoLinkSensorEntity(YoLinkEntity, SensorEntity):
return return
self._attr_native_value = attr_val self._attr_native_value = attr_val
self.async_write_ha_state() self.async_write_ha_state()
@property
def available(self) -> bool:
"""Return true is device is available."""
return super().available and self.coordinator.dev_online