From 356775c19b1e8523345b7c765dcc7b798cab3cf2 Mon Sep 17 00:00:00 2001 From: Matrix Date: Sat, 10 May 2025 00:44:43 +0800 Subject: [PATCH] Add water flowing status for YoLink water meter(YS5018). (#144535) * Add water flowing status for YoLink water meter(YS5018). * Fixes --- .../components/yolink/binary_sensor.py | 28 ++++++++++++++++--- homeassistant/components/yolink/const.py | 2 ++ .../components/yolink/coordinator.py | 5 ++++ homeassistant/components/yolink/entity.py | 2 +- homeassistant/components/yolink/icons.json | 5 ++++ homeassistant/components/yolink/strings.json | 3 ++ 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/yolink/binary_sensor.py b/homeassistant/components/yolink/binary_sensor.py index 30c04d3a424..e5200c66afd 100644 --- a/homeassistant/components/yolink/binary_sensor.py +++ b/homeassistant/components/yolink/binary_sensor.py @@ -25,7 +25,11 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN +from .const import ( + DEV_MODEL_WATER_METER_YS5018_EC, + DEV_MODEL_WATER_METER_YS5018_UC, + DOMAIN, +) from .coordinator import YoLinkCoordinator from .entity import YoLinkEntity @@ -37,6 +41,7 @@ class YoLinkBinarySensorEntityDescription(BinarySensorEntityDescription): exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True state_key: str = "state" value: Callable[[Any], bool | None] = lambda _: None + should_update_entity: Callable = lambda state: True SENSOR_DEVICE_TYPE = [ @@ -95,6 +100,17 @@ SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = ( device.device_type == ATTR_DEVICE_WATER_METER_CONTROLLER ), ), + YoLinkBinarySensorEntityDescription( + key="water_running", + translation_key="water_running", + value=lambda state: state.get("waterFlowing") if state is not None else None, + should_update_entity=lambda value: value is not None, + exists_fn=lambda device: ( + device.device_type == ATTR_DEVICE_WATER_METER_CONTROLLER + and device.device_model_name + in [DEV_MODEL_WATER_METER_YS5018_EC, DEV_MODEL_WATER_METER_YS5018_UC] + ), + ), ) @@ -141,9 +157,13 @@ class YoLinkBinarySensorEntity(YoLinkEntity, BinarySensorEntity): @callback def update_entity_state(self, state: dict[str, Any]) -> None: """Update HA Entity State.""" - self._attr_is_on = self.entity_description.value( - state.get(self.entity_description.state_key) - ) + if ( + _attr_val := self.entity_description.value( + state.get(self.entity_description.state_key) + ) + ) is None or self.entity_description.should_update_entity(_attr_val) is False: + return + self._attr_is_on = _attr_val self.async_write_ha_state() @property diff --git a/homeassistant/components/yolink/const.py b/homeassistant/components/yolink/const.py index 8879ef15125..960bf8568d4 100644 --- a/homeassistant/components/yolink/const.py +++ b/homeassistant/components/yolink/const.py @@ -37,3 +37,5 @@ DEV_MODEL_SWITCH_YS5708_UC = "YS5708-UC" DEV_MODEL_SWITCH_YS5708_EC = "YS5708-EC" DEV_MODEL_SWITCH_YS5709_UC = "YS5709-UC" DEV_MODEL_SWITCH_YS5709_EC = "YS5709-EC" +DEV_MODEL_WATER_METER_YS5018_EC = "YS5018-EC" +DEV_MODEL_WATER_METER_YS5018_UC = "YS5018-UC" diff --git a/homeassistant/components/yolink/coordinator.py b/homeassistant/components/yolink/coordinator.py index d18a37bd276..8fd450df4a5 100644 --- a/homeassistant/components/yolink/coordinator.py +++ b/homeassistant/components/yolink/coordinator.py @@ -76,6 +76,11 @@ class YoLinkCoordinator(DataUpdateCoordinator[dict]): except YoLinkAuthFailError as yl_auth_err: raise ConfigEntryAuthFailed from yl_auth_err except YoLinkClientError as yl_client_err: + _LOGGER.error( + "Failed to obtain device status, device: %s, error: %s ", + self.device.device_id, + yl_client_err, + ) raise UpdateFailed from yl_client_err if device_state is not None: return device_state diff --git a/homeassistant/components/yolink/entity.py b/homeassistant/components/yolink/entity.py index 0f500b72404..7828bf91541 100644 --- a/homeassistant/components/yolink/entity.py +++ b/homeassistant/components/yolink/entity.py @@ -45,7 +45,7 @@ class YoLinkEntity(CoordinatorEntity[YoLinkCoordinator]): def _handle_coordinator_update(self) -> None: """Update state.""" data = self.coordinator.data - if data is not None: + if data is not None and len(data) > 0: self.update_entity_state(data) @property diff --git a/homeassistant/components/yolink/icons.json b/homeassistant/components/yolink/icons.json index c58d219a2e0..6d9062a92b8 100644 --- a/homeassistant/components/yolink/icons.json +++ b/homeassistant/components/yolink/icons.json @@ -1,5 +1,10 @@ { "entity": { + "binary_sensor": { + "water_running": { + "default": "mdi:waves-arrow-right" + } + }, "number": { "config_volume": { "default": "mdi:volume-high" diff --git a/homeassistant/components/yolink/strings.json b/homeassistant/components/yolink/strings.json index 8867457342f..825f9e3e619 100644 --- a/homeassistant/components/yolink/strings.json +++ b/homeassistant/components/yolink/strings.json @@ -44,6 +44,9 @@ } }, "entity": { + "binary_sensor": { + "water_running": { "name": "Water is flowing" } + }, "switch": { "usb_ports": { "name": "USB ports" }, "plug_1": { "name": "Plug 1" },