Add water flowing status for YoLink water meter(YS5018). (#144535)

* Add water flowing status for YoLink water meter(YS5018).

* Fixes
This commit is contained in:
Matrix 2025-05-10 00:44:43 +08:00 committed by GitHub
parent 87bd6e3ca0
commit 356775c19b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 5 deletions

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -1,5 +1,10 @@
{
"entity": {
"binary_sensor": {
"water_running": {
"default": "mdi:waves-arrow-right"
}
},
"number": {
"config_volume": {
"default": "mdi:volume-high"

View File

@ -44,6 +44,9 @@
}
},
"entity": {
"binary_sensor": {
"water_running": { "name": "Water is flowing" }
},
"switch": {
"usb_ports": { "name": "USB ports" },
"plug_1": { "name": "Plug 1" },