mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add yolink valve controller support (#73111)
This commit is contained in:
parent
5f2b4001f3
commit
68d67a3e49
@ -21,3 +21,4 @@ ATTR_DEVICE_VIBRATION_SENSOR = "VibrationSensor"
|
|||||||
ATTR_DEVICE_OUTLET = "Outlet"
|
ATTR_DEVICE_OUTLET = "Outlet"
|
||||||
ATTR_DEVICE_SIREN = "Siren"
|
ATTR_DEVICE_SIREN = "Siren"
|
||||||
ATTR_DEVICE_LOCK = "Lock"
|
ATTR_DEVICE_LOCK = "Lock"
|
||||||
|
ATTR_DEVICE_MANIPULATOR = "Manipulator"
|
||||||
|
@ -22,6 +22,7 @@ from .const import (
|
|||||||
ATTR_COORDINATORS,
|
ATTR_COORDINATORS,
|
||||||
ATTR_DEVICE_DOOR_SENSOR,
|
ATTR_DEVICE_DOOR_SENSOR,
|
||||||
ATTR_DEVICE_LOCK,
|
ATTR_DEVICE_LOCK,
|
||||||
|
ATTR_DEVICE_MANIPULATOR,
|
||||||
ATTR_DEVICE_MOTION_SENSOR,
|
ATTR_DEVICE_MOTION_SENSOR,
|
||||||
ATTR_DEVICE_TH_SENSOR,
|
ATTR_DEVICE_TH_SENSOR,
|
||||||
ATTR_DEVICE_VIBRATION_SENSOR,
|
ATTR_DEVICE_VIBRATION_SENSOR,
|
||||||
@ -53,17 +54,28 @@ SENSOR_DEVICE_TYPE = [
|
|||||||
ATTR_DEVICE_TH_SENSOR,
|
ATTR_DEVICE_TH_SENSOR,
|
||||||
ATTR_DEVICE_VIBRATION_SENSOR,
|
ATTR_DEVICE_VIBRATION_SENSOR,
|
||||||
ATTR_DEVICE_LOCK,
|
ATTR_DEVICE_LOCK,
|
||||||
|
ATTR_DEVICE_MANIPULATOR,
|
||||||
]
|
]
|
||||||
|
|
||||||
BATTERY_POWER_SENSOR = [
|
BATTERY_POWER_SENSOR = [
|
||||||
ATTR_DEVICE_DOOR_SENSOR,
|
ATTR_DEVICE_DOOR_SENSOR,
|
||||||
ATTR_DEVICE_TH_SENSOR,
|
|
||||||
ATTR_DEVICE_MOTION_SENSOR,
|
ATTR_DEVICE_MOTION_SENSOR,
|
||||||
|
ATTR_DEVICE_TH_SENSOR,
|
||||||
ATTR_DEVICE_VIBRATION_SENSOR,
|
ATTR_DEVICE_VIBRATION_SENSOR,
|
||||||
ATTR_DEVICE_LOCK,
|
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, ...] = (
|
SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
|
||||||
YoLinkSensorEntityDescription(
|
YoLinkSensorEntityDescription(
|
||||||
key="battery",
|
key="battery",
|
||||||
@ -71,11 +83,7 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
|
|||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
name="Battery",
|
name="Battery",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
value=lambda value: percentage.ordered_list_item_to_percentage(
|
value=cvt_battery,
|
||||||
[1, 2, 3, 4], value
|
|
||||||
)
|
|
||||||
if value is not None
|
|
||||||
else None,
|
|
||||||
exists_fn=lambda device: device.device_type in BATTERY_POWER_SENSOR,
|
exists_fn=lambda device: device.device_type in BATTERY_POWER_SENSOR,
|
||||||
),
|
),
|
||||||
YoLinkSensorEntityDescription(
|
YoLinkSensorEntityDescription(
|
||||||
|
@ -16,7 +16,12 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 .coordinator import YoLinkCoordinator
|
||||||
from .entity import YoLinkEntity
|
from .entity import YoLinkEntity
|
||||||
|
|
||||||
@ -27,19 +32,27 @@ class YoLinkSwitchEntityDescription(SwitchEntityDescription):
|
|||||||
|
|
||||||
exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
|
exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
|
||||||
value: Callable[[Any], bool | None] = lambda _: None
|
value: Callable[[Any], bool | None] = lambda _: None
|
||||||
|
state_key: str = "state"
|
||||||
|
|
||||||
|
|
||||||
DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
|
DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
|
||||||
YoLinkSwitchEntityDescription(
|
YoLinkSwitchEntityDescription(
|
||||||
key="state",
|
key="outlet_state",
|
||||||
device_class=SwitchDeviceClass.OUTLET,
|
device_class=SwitchDeviceClass.OUTLET,
|
||||||
name="State",
|
name="State",
|
||||||
value=lambda value: value == "open" if value is not None else None,
|
value=lambda value: value == "open" if value is not None else None,
|
||||||
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_OUTLET],
|
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(
|
async def async_setup_entry(
|
||||||
@ -47,7 +60,7 @@ async def async_setup_entry(
|
|||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> 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]
|
device_coordinators = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATORS]
|
||||||
switch_device_coordinators = [
|
switch_device_coordinators = [
|
||||||
device_coordinator
|
device_coordinator
|
||||||
@ -77,7 +90,7 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
|
|||||||
coordinator: YoLinkCoordinator,
|
coordinator: YoLinkCoordinator,
|
||||||
description: YoLinkSwitchEntityDescription,
|
description: YoLinkSwitchEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Init YoLink Outlet."""
|
"""Init YoLink switch."""
|
||||||
super().__init__(config_entry, coordinator)
|
super().__init__(config_entry, coordinator)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
@ -91,12 +104,12 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
|
|||||||
def update_entity_state(self, state: dict[str, Any]) -> None:
|
def update_entity_state(self, state: dict[str, Any]) -> None:
|
||||||
"""Update HA Entity State."""
|
"""Update HA Entity State."""
|
||||||
self._attr_is_on = self.entity_description.value(
|
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()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def call_state_change(self, state: str) -> None:
|
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})
|
await self.call_device_api("setState", {"state": state})
|
||||||
self._attr_is_on = self.entity_description.value(state)
|
self._attr_is_on = self.entity_description.value(state)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user