Yolink feature garage door (#75120)

This commit is contained in:
Matrix 2022-07-18 17:40:18 +08:00 committed by GitHub
parent e522b6e3b8
commit 943e0b9cf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 99 additions and 4 deletions

View File

@ -1498,6 +1498,7 @@ omit =
homeassistant/components/yolink/climate.py
homeassistant/components/yolink/const.py
homeassistant/components/yolink/coordinator.py
homeassistant/components/yolink/cover.py
homeassistant/components/yolink/entity.py
homeassistant/components/yolink/lock.py
homeassistant/components/yolink/sensor.py

View File

@ -27,6 +27,7 @@ SCAN_INTERVAL = timedelta(minutes=5)
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.CLIMATE,
Platform.COVER,
Platform.LOCK,
Platform.SENSOR,
Platform.SIREN,

View File

@ -47,6 +47,13 @@ SENSOR_DEVICE_TYPE = [
]
def is_door_sensor(device: YoLinkDevice) -> bool:
"""Check Door Sensor type."""
return device.device_type == ATTR_DEVICE_DOOR_SENSOR and (
device.parent_id is None or device.parent_id == "null"
)
SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = (
YoLinkBinarySensorEntityDescription(
key="door_state",
@ -54,7 +61,7 @@ SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = (
device_class=BinarySensorDeviceClass.DOOR,
name="State",
value=lambda value: value == "open" if value is not None else None,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_DOOR_SENSOR,
exists_fn=is_door_sensor,
),
YoLinkBinarySensorEntityDescription(
key="motion_state",

View File

@ -0,0 +1,86 @@
"""YoLink Garage Door."""
from __future__ import annotations
from typing import Any
from homeassistant.components.cover import (
CoverDeviceClass,
CoverEntity,
CoverEntityFeature,
)
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_DOOR_SENSOR, DOMAIN
from .coordinator import YoLinkCoordinator
from .entity import YoLinkEntity
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up YoLink garage door from a config entry."""
device_coordinators = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATORS]
entities = [
YoLinkCoverEntity(config_entry, device_coordinator)
for device_coordinator in device_coordinators.values()
if device_coordinator.device.device_type == ATTR_DEVICE_DOOR_SENSOR
and device_coordinator.device.parent_id is not None
and device_coordinator.device.parent_id != "null"
]
async_add_entities(entities)
class YoLinkCoverEntity(YoLinkEntity, CoverEntity):
"""YoLink Cover Entity."""
def __init__(
self,
config_entry: ConfigEntry,
coordinator: YoLinkCoordinator,
) -> None:
"""Init YoLink garage door entity."""
super().__init__(config_entry, coordinator)
self._attr_unique_id = f"{coordinator.device.device_id}_door_state"
self._attr_name = f"{coordinator.device.device_name} (State)"
self._attr_device_class = CoverDeviceClass.GARAGE
self._attr_supported_features = (
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
)
@callback
def update_entity_state(self, state: dict[str, Any]) -> None:
"""Update HA Entity State."""
self._attr_is_closed = state.get("state") == "closed"
self.async_write_ha_state()
async def toggle_garage_state(self, state: str) -> None:
"""Toggle Garage door state."""
# make sure current state is correct
await self.coordinator.async_refresh()
if state == "open" and self.is_closed is False:
return
if state == "close" and self.is_closed is True:
return
# get paired controller
door_controller_coordinator = self.hass.data[DOMAIN][
self.config_entry.entry_id
][ATTR_COORDINATORS].get(self.coordinator.device.parent_id)
if door_controller_coordinator is None:
raise ValueError(
"This device has not been paired with a garage door controller"
)
# call controller api open/close garage door
await door_controller_coordinator.device.call_device_http_api("toggle", None)
await self.coordinator.async_refresh()
async def async_open_cover(self, **kwargs: Any) -> None:
"""Open garage door."""
await self.toggle_garage_state("open")
async def async_close_cover(self, **kwargs: Any) -> None:
"""Close garage door."""
await self.toggle_garage_state("close")

View File

@ -3,7 +3,7 @@
"name": "YoLink",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/yolink",
"requirements": ["yolink-api==0.0.8"],
"requirements": ["yolink-api==0.0.9"],
"dependencies": ["auth", "application_credentials"],
"codeowners": ["@matrixd2"],
"iot_class": "cloud_push"

View File

@ -2486,7 +2486,7 @@ yeelight==0.7.10
yeelightsunflower==0.0.10
# homeassistant.components.yolink
yolink-api==0.0.8
yolink-api==0.0.9
# homeassistant.components.youless
youless-api==0.16

View File

@ -1662,7 +1662,7 @@ yalexs==1.1.25
yeelight==0.7.10
# homeassistant.components.yolink
yolink-api==0.0.8
yolink-api==0.0.9
# homeassistant.components.youless
youless-api==0.16