From 1e7b187fc6d9d7fd20f174d7307d3fa6a31dc05a Mon Sep 17 00:00:00 2001 From: Matrix Date: Sat, 21 May 2022 01:06:47 +0800 Subject: [PATCH] Add yolink sensors (#72186) * Add more sensor support * change codeowner to active account * fix suggest --- CODEOWNERS | 4 +-- .../components/yolink/binary_sensor.py | 31 +++++++++++++--- homeassistant/components/yolink/const.py | 3 ++ homeassistant/components/yolink/manifest.json | 2 +- homeassistant/components/yolink/sensor.py | 35 ++++++++++++++++--- 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index a1d329cbee06..c3e36d1fef76 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1201,8 +1201,8 @@ build.json @home-assistant/supervisor /tests/components/yeelight/ @zewelor @shenxn @starkillerOG @alexyao2015 /homeassistant/components/yeelightsunflower/ @lindsaymarkward /homeassistant/components/yi/ @bachya -/homeassistant/components/yolink/ @YoSmart-Inc -/tests/components/yolink/ @YoSmart-Inc +/homeassistant/components/yolink/ @matrixd2 +/tests/components/yolink/ @matrixd2 /homeassistant/components/youless/ @gjong /tests/components/youless/ @gjong /homeassistant/components/zengge/ @emontnemery diff --git a/homeassistant/components/yolink/binary_sensor.py b/homeassistant/components/yolink/binary_sensor.py index c3f2d10cf4c9..494a7ff10ebb 100644 --- a/homeassistant/components/yolink/binary_sensor.py +++ b/homeassistant/components/yolink/binary_sensor.py @@ -15,7 +15,13 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import ATTR_COORDINATOR, ATTR_DEVICE_DOOR_SENSOR, DOMAIN +from .const import ( + ATTR_COORDINATOR, + ATTR_DEVICE_DOOR_SENSOR, + ATTR_DEVICE_LEAK_SENSOR, + ATTR_DEVICE_MOTION_SENSOR, + DOMAIN, +) from .coordinator import YoLinkCoordinator from .entity import YoLinkEntity @@ -25,20 +31,37 @@ class YoLinkBinarySensorEntityDescription(BinarySensorEntityDescription): """YoLink BinarySensorEntityDescription.""" exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True + key: str = "state" value: Callable[[str], bool | None] = lambda _: None -SENSOR_DEVICE_TYPE = [ATTR_DEVICE_DOOR_SENSOR] +SENSOR_DEVICE_TYPE = [ + ATTR_DEVICE_DOOR_SENSOR, + ATTR_DEVICE_MOTION_SENSOR, + ATTR_DEVICE_LEAK_SENSOR, +] SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = ( YoLinkBinarySensorEntityDescription( - key="state", icon="mdi:door", device_class=BinarySensorDeviceClass.DOOR, - name="state", + name="State", value=lambda value: value == "open", exists_fn=lambda device: device.device_type in [ATTR_DEVICE_DOOR_SENSOR], ), + YoLinkBinarySensorEntityDescription( + device_class=BinarySensorDeviceClass.MOTION, + name="Motion", + value=lambda value: value == "alert", + exists_fn=lambda device: device.device_type in [ATTR_DEVICE_MOTION_SENSOR], + ), + YoLinkBinarySensorEntityDescription( + name="Leak", + icon="mdi:water", + device_class=BinarySensorDeviceClass.MOISTURE, + value=lambda value: value == "alert", + exists_fn=lambda device: device.device_type in [ATTR_DEVICE_LEAK_SENSOR], + ), ) diff --git a/homeassistant/components/yolink/const.py b/homeassistant/components/yolink/const.py index 1804838e8b3b..303fd684c024 100644 --- a/homeassistant/components/yolink/const.py +++ b/homeassistant/components/yolink/const.py @@ -14,3 +14,6 @@ ATTR_CLIENT = "client" ATTR_MQTT_CLIENT = "mqtt_client" ATTR_DEVICE_ID = "deviceId" ATTR_DEVICE_DOOR_SENSOR = "DoorSensor" +ATTR_DEVICE_TH_SENSOR = "THSensor" +ATTR_DEVICE_MOTION_SENSOR = "MotionSensor" +ATTR_DEVICE_LEAK_SENSOR = "LeakSensor" diff --git a/homeassistant/components/yolink/manifest.json b/homeassistant/components/yolink/manifest.json index d0072145c194..a89934154e90 100644 --- a/homeassistant/components/yolink/manifest.json +++ b/homeassistant/components/yolink/manifest.json @@ -5,6 +5,6 @@ "documentation": "https://www.home-assistant.io/integrations/yolink", "requirements": ["yolink-api==0.0.5"], "dependencies": ["auth", "application_credentials"], - "codeowners": ["@YoSmart-Inc"], + "codeowners": ["@matrixd2"], "iot_class": "cloud_push" } diff --git a/homeassistant/components/yolink/sensor.py b/homeassistant/components/yolink/sensor.py index 075cfc241795..e33772c24be2 100644 --- a/homeassistant/components/yolink/sensor.py +++ b/homeassistant/components/yolink/sensor.py @@ -13,12 +13,18 @@ from homeassistant.components.sensor import ( SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import PERCENTAGE +from homeassistant.const import PERCENTAGE, TEMP_CELSIUS from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import percentage -from .const import ATTR_COORDINATOR, ATTR_DEVICE_DOOR_SENSOR, DOMAIN +from .const import ( + ATTR_COORDINATOR, + ATTR_DEVICE_DOOR_SENSOR, + ATTR_DEVICE_MOTION_SENSOR, + ATTR_DEVICE_TH_SENSOR, + DOMAIN, +) from .coordinator import YoLinkCoordinator from .entity import YoLinkEntity @@ -49,11 +55,32 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = ( value=lambda value: percentage.ordered_list_item_to_percentage( [1, 2, 3, 4], value ), - exists_fn=lambda device: device.device_type in [ATTR_DEVICE_DOOR_SENSOR], + exists_fn=lambda device: device.device_type + in [ATTR_DEVICE_DOOR_SENSOR, ATTR_DEVICE_TH_SENSOR, ATTR_DEVICE_MOTION_SENSOR], + ), + YoLinkSensorEntityDescription( + key="humidity", + device_class=SensorDeviceClass.HUMIDITY, + native_unit_of_measurement=PERCENTAGE, + name="Humidity", + state_class=SensorStateClass.MEASUREMENT, + exists_fn=lambda device: device.device_type in [ATTR_DEVICE_TH_SENSOR], + ), + YoLinkSensorEntityDescription( + key="temperature", + device_class=SensorDeviceClass.TEMPERATURE, + native_unit_of_measurement=TEMP_CELSIUS, + name="Temperature", + state_class=SensorStateClass.MEASUREMENT, + exists_fn=lambda device: device.device_type in [ATTR_DEVICE_TH_SENSOR], ), ) -SENSOR_DEVICE_TYPE = [ATTR_DEVICE_DOOR_SENSOR] +SENSOR_DEVICE_TYPE = [ + ATTR_DEVICE_DOOR_SENSOR, + ATTR_DEVICE_MOTION_SENSOR, + ATTR_DEVICE_TH_SENSOR, +] async def async_setup_entry(