Add binary sensor platform to VeSync (#134221)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>pull/136754/head^2
parent
e27a980742
commit
ce432555f0
|
@ -21,6 +21,7 @@ from .const import (
|
|||
from .coordinator import VeSyncDataCoordinator
|
||||
|
||||
PLATFORMS = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.FAN,
|
||||
Platform.HUMIDIFIER,
|
||||
Platform.LIGHT,
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
"""Binary Sensor for VeSync."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pyvesync.vesyncbasedevice import VeSyncBaseDevice
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .common import rgetattr
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class VeSyncBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
"""A class that describes custom binary sensor entities."""
|
||||
|
||||
is_on: Callable[[VeSyncBaseDevice], bool]
|
||||
|
||||
|
||||
SENSOR_DESCRIPTIONS: tuple[VeSyncBinarySensorEntityDescription, ...] = (
|
||||
VeSyncBinarySensorEntityDescription(
|
||||
key="water_lacks",
|
||||
translation_key="water_lacks",
|
||||
is_on=lambda device: device.water_lacks,
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
),
|
||||
VeSyncBinarySensorEntityDescription(
|
||||
key="details.water_tank_lifted",
|
||||
translation_key="water_tank_lifted",
|
||||
is_on=lambda device: device.details["water_tank_lifted"],
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up binary_sensor platform."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
|
||||
@callback
|
||||
def discover(devices):
|
||||
"""Add new devices to platform."""
|
||||
_setup_entities(devices, async_add_entities)
|
||||
|
||||
config_entry.async_on_unload(
|
||||
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_DEVICES), discover)
|
||||
)
|
||||
|
||||
_setup_entities(hass.data[DOMAIN][VS_DEVICES], async_add_entities, coordinator)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(devices, async_add_entities, coordinator):
|
||||
"""Add entity."""
|
||||
async_add_entities(
|
||||
(
|
||||
VeSyncBinarySensor(dev, description, coordinator)
|
||||
for dev in devices
|
||||
for description in SENSOR_DESCRIPTIONS
|
||||
if rgetattr(dev, description.key) is not None
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class VeSyncBinarySensor(BinarySensorEntity, VeSyncBaseEntity):
|
||||
"""Vesync binary sensor class."""
|
||||
|
||||
entity_description: VeSyncBinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
device: VeSyncBaseDevice,
|
||||
description: VeSyncBinarySensorEntityDescription,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(device, coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{super().unique_id}-{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the binary sensor is on."""
|
||||
_LOGGER.debug(rgetattr(self.device, self.entity_description.key))
|
||||
return self.entity_description.is_on(self.device)
|
|
@ -12,6 +12,28 @@ from .const import VeSyncHumidifierDevice
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def rgetattr(obj: object, attr: str):
|
||||
"""Return a string in the form word.1.2.3 and return the item as 3. Note that this last value could be in a dict as well."""
|
||||
_this_func = rgetattr
|
||||
sp = attr.split(".", 1)
|
||||
if len(sp) == 1:
|
||||
left, right = sp[0], ""
|
||||
else:
|
||||
left, right = sp
|
||||
|
||||
if isinstance(obj, dict):
|
||||
obj = obj.get(left)
|
||||
elif hasattr(obj, left):
|
||||
obj = getattr(obj, left)
|
||||
else:
|
||||
return None
|
||||
|
||||
if right:
|
||||
obj = _this_func(obj, right)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
async def async_generate_device_list(
|
||||
hass: HomeAssistant, manager: VeSync
|
||||
) -> list[VeSyncBaseDevice]:
|
||||
|
|
|
@ -43,6 +43,14 @@
|
|||
"name": "Current voltage"
|
||||
}
|
||||
},
|
||||
"binary_sensor": {
|
||||
"water_lacks": {
|
||||
"name": "Low water"
|
||||
},
|
||||
"water_tank_lifted": {
|
||||
"name": "Water tank lifted"
|
||||
}
|
||||
},
|
||||
"number": {
|
||||
"mist_level": {
|
||||
"name": "Mist level"
|
||||
|
|
|
@ -101,6 +101,9 @@ async def test_async_get_device_diagnostics__single_fan(
|
|||
"home_assistant.entities.2.state.last_changed": (str,),
|
||||
"home_assistant.entities.2.state.last_reported": (str,),
|
||||
"home_assistant.entities.2.state.last_updated": (str,),
|
||||
"home_assistant.entities.3.state.last_changed": (str,),
|
||||
"home_assistant.entities.3.state.last_reported": (str,),
|
||||
"home_assistant.entities.3.state.last_updated": (str,),
|
||||
}
|
||||
)
|
||||
)
|
||||
|
|
|
@ -48,6 +48,7 @@ async def test_async_setup_entry__no_devices(
|
|||
assert setups_mock.call_count == 1
|
||||
assert setups_mock.call_args.args[0] == config_entry
|
||||
assert setups_mock.call_args.args[1] == [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.FAN,
|
||||
Platform.HUMIDIFIER,
|
||||
Platform.LIGHT,
|
||||
|
@ -78,6 +79,7 @@ async def test_async_setup_entry__loads_fans(
|
|||
assert setups_mock.call_count == 1
|
||||
assert setups_mock.call_args.args[0] == config_entry
|
||||
assert setups_mock.call_args.args[1] == [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.FAN,
|
||||
Platform.HUMIDIFIER,
|
||||
Platform.LIGHT,
|
||||
|
|
Loading…
Reference in New Issue