Add yolink valve controller support (#73111)

This commit is contained in:
Matrix 2022-06-07 22:34:12 +08:00 committed by GitHub
parent 5f2b4001f3
commit 68d67a3e49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 13 deletions

View file

@ -21,3 +21,4 @@ ATTR_DEVICE_VIBRATION_SENSOR = "VibrationSensor"
ATTR_DEVICE_OUTLET = "Outlet"
ATTR_DEVICE_SIREN = "Siren"
ATTR_DEVICE_LOCK = "Lock"
ATTR_DEVICE_MANIPULATOR = "Manipulator"

View file

@ -22,6 +22,7 @@ from .const import (
ATTR_COORDINATORS,
ATTR_DEVICE_DOOR_SENSOR,
ATTR_DEVICE_LOCK,
ATTR_DEVICE_MANIPULATOR,
ATTR_DEVICE_MOTION_SENSOR,
ATTR_DEVICE_TH_SENSOR,
ATTR_DEVICE_VIBRATION_SENSOR,
@ -53,17 +54,28 @@ SENSOR_DEVICE_TYPE = [
ATTR_DEVICE_TH_SENSOR,
ATTR_DEVICE_VIBRATION_SENSOR,
ATTR_DEVICE_LOCK,
ATTR_DEVICE_MANIPULATOR,
]
BATTERY_POWER_SENSOR = [
ATTR_DEVICE_DOOR_SENSOR,
ATTR_DEVICE_TH_SENSOR,
ATTR_DEVICE_MOTION_SENSOR,
ATTR_DEVICE_TH_SENSOR,
ATTR_DEVICE_VIBRATION_SENSOR,
ATTR_DEVICE_LOCK,
ATTR_DEVICE_MANIPULATOR,
]
def cvt_battery(val: int | None) -> int | None:
"""Convert battery to percentage."""
if val is None:
return None
if val > 0:
return percentage.ordered_list_item_to_percentage([1, 2, 3, 4], val)
return 0
SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
YoLinkSensorEntityDescription(
key="battery",
@ -71,11 +83,7 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE,
name="Battery",
state_class=SensorStateClass.MEASUREMENT,
value=lambda value: percentage.ordered_list_item_to_percentage(
[1, 2, 3, 4], value
)
if value is not None
else None,
value=cvt_battery,
exists_fn=lambda device: device.device_type in BATTERY_POWER_SENSOR,
),
YoLinkSensorEntityDescription(

View file

@ -16,7 +16,12 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ATTR_COORDINATORS, ATTR_DEVICE_OUTLET, DOMAIN
from .const import (
ATTR_COORDINATORS,
ATTR_DEVICE_MANIPULATOR,
ATTR_DEVICE_OUTLET,
DOMAIN,
)
from .coordinator import YoLinkCoordinator
from .entity import YoLinkEntity
@ -27,19 +32,27 @@ class YoLinkSwitchEntityDescription(SwitchEntityDescription):
exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
value: Callable[[Any], bool | None] = lambda _: None
state_key: str = "state"
DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
YoLinkSwitchEntityDescription(
key="state",
key="outlet_state",
device_class=SwitchDeviceClass.OUTLET,
name="State",
value=lambda value: value == "open" if value is not None else None,
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_OUTLET],
),
YoLinkSwitchEntityDescription(
key="manipulator_state",
device_class=SwitchDeviceClass.SWITCH,
name="State",
value=lambda value: value == "open" if value is not None else None,
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_MANIPULATOR],
),
)
DEVICE_TYPE = [ATTR_DEVICE_OUTLET]
DEVICE_TYPE = [ATTR_DEVICE_MANIPULATOR, ATTR_DEVICE_OUTLET]
async def async_setup_entry(
@ -47,7 +60,7 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up YoLink Sensor from a config entry."""
"""Set up YoLink switch from a config entry."""
device_coordinators = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATORS]
switch_device_coordinators = [
device_coordinator
@ -77,7 +90,7 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
coordinator: YoLinkCoordinator,
description: YoLinkSwitchEntityDescription,
) -> None:
"""Init YoLink Outlet."""
"""Init YoLink switch."""
super().__init__(config_entry, coordinator)
self.entity_description = description
self._attr_unique_id = (
@ -91,12 +104,12 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
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.key)
state.get(self.entity_description.state_key)
)
self.async_write_ha_state()
async def call_state_change(self, state: str) -> None:
"""Call setState api to change outlet state."""
"""Call setState api to change switch state."""
await self.call_device_api("setState", {"state": state})
self._attr_is_on = self.entity_description.value(state)
self.async_write_ha_state()