Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """YoLink Garage Door."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from yolink.client_request import ClientRequest
8 from yolink.const import ATTR_DEVICE_FINGER, ATTR_GARAGE_DOOR_CONTROLLER
9 
11  CoverDeviceClass,
12  CoverEntity,
13  CoverEntityFeature,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import DOMAIN
20 from .coordinator import YoLinkCoordinator
21 from .entity import YoLinkEntity
22 
23 
25  hass: HomeAssistant,
26  config_entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up YoLink garage door from a config entry."""
30  device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
31  entities = [
32  YoLinkCoverEntity(config_entry, device_coordinator)
33  for device_coordinator in device_coordinators.values()
34  if device_coordinator.device.device_type
35  in [ATTR_GARAGE_DOOR_CONTROLLER, ATTR_DEVICE_FINGER]
36  ]
37  async_add_entities(entities)
38 
39 
41  """YoLink Cover Entity."""
42 
43  _attr_name = None
44 
45  def __init__(
46  self,
47  config_entry: ConfigEntry,
48  coordinator: YoLinkCoordinator,
49  ) -> None:
50  """Init YoLink garage door entity."""
51  super().__init__(config_entry, coordinator)
52  self._attr_unique_id_attr_unique_id = f"{coordinator.device.device_id}_door_state"
53  self._attr_device_class_attr_device_class = CoverDeviceClass.GARAGE
54  self._attr_supported_features_attr_supported_features = (
55  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
56  )
57 
58  @callback
59  def update_entity_state(self, state: dict[str, Any]) -> None:
60  """Update HA Entity State."""
61  if (state_val := state.get("state")) is None:
62  return
63  if self.coordinator.paired_device is None or state_val == "error":
64  self._attr_is_closed_attr_is_closed = None
65  self._attr_available_attr_available = False
66  self.async_write_ha_stateasync_write_ha_state()
67  elif state_val in ["open", "closed"]:
68  self._attr_is_closed_attr_is_closed = state_val == "closed"
69  self._attr_available_attr_available = True
70  self.async_write_ha_stateasync_write_ha_state()
71 
72  async def toggle_garage_state(self) -> None:
73  """Toggle Garage door state."""
74  # garage door state will not be changed by device call
75  # it depends on paired device state, such as door sensor or contact sensor
76  await self.call_devicecall_device(ClientRequest("toggle", {}))
77 
78  async def async_open_cover(self, **kwargs: Any) -> None:
79  """Toggle garage door."""
80  await self.toggle_garage_statetoggle_garage_state()
81 
82  async def async_close_cover(self, **kwargs: Any) -> None:
83  """Toggle garage door."""
84  await self.toggle_garage_statetoggle_garage_state()