Home Assistant Unofficial Reference 2024.12.1
generic_cover.py
Go to the documentation of this file.
1 """Base class for Overkiz covers, shutters, awnings, etc."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from pyoverkiz.enums import OverkizCommand, OverkizCommandParam, OverkizState
8 
10  ATTR_TILT_POSITION,
11  CoverEntity,
12  CoverEntityFeature,
13 )
14 
15 from ..entity import OverkizEntity
16 
17 ATTR_OBSTRUCTION_DETECTED = "obstruction-detected"
18 
19 COMMANDS_STOP: list[OverkizCommand] = [
20  OverkizCommand.STOP,
21  OverkizCommand.MY,
22 ]
23 COMMANDS_STOP_TILT: list[OverkizCommand] = [
24  OverkizCommand.STOP,
25  OverkizCommand.MY,
26 ]
27 COMMANDS_OPEN: list[OverkizCommand] = [
28  OverkizCommand.OPEN,
29  OverkizCommand.UP,
30 ]
31 COMMANDS_OPEN_TILT: list[OverkizCommand] = [
32  OverkizCommand.OPEN_SLATS,
33  OverkizCommand.TILT_DOWN,
34 ]
35 COMMANDS_CLOSE: list[OverkizCommand] = [
36  OverkizCommand.CLOSE,
37  OverkizCommand.DOWN,
38 ]
39 COMMANDS_CLOSE_TILT: list[OverkizCommand] = [
40  OverkizCommand.CLOSE_SLATS,
41  OverkizCommand.TILT_UP,
42 ]
43 
44 COMMANDS_SET_TILT_POSITION: list[OverkizCommand] = [OverkizCommand.SET_ORIENTATION]
45 
46 
48  """Representation of an Overkiz Cover."""
49 
50  @property
51  def current_cover_tilt_position(self) -> int | None:
52  """Return current position of cover tilt.
53 
54  None is unknown, 0 is closed, 100 is fully open.
55  """
56  position = self.executorexecutor.select_state(
57  OverkizState.CORE_SLATS_ORIENTATION, OverkizState.CORE_SLATE_ORIENTATION
58  )
59  if position is not None:
60  return 100 - cast(int, position)
61 
62  return None
63 
64  async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
65  """Move the cover tilt to a specific position."""
66  if command := self.executorexecutor.select_command(*COMMANDS_SET_TILT_POSITION):
67  await self.executorexecutor.async_execute_command(
68  command,
69  100 - kwargs[ATTR_TILT_POSITION],
70  )
71 
72  @property
73  def is_closed(self) -> bool | None:
74  """Return if the cover is closed."""
75 
76  state = self.executorexecutor.select_state(
77  OverkizState.CORE_OPEN_CLOSED,
78  OverkizState.CORE_SLATS_OPEN_CLOSED,
79  OverkizState.CORE_OPEN_CLOSED_PARTIAL,
80  OverkizState.CORE_OPEN_CLOSED_PEDESTRIAN,
81  OverkizState.CORE_OPEN_CLOSED_UNKNOWN,
82  OverkizState.MYFOX_SHUTTER_STATUS,
83  )
84  if state is not None:
85  return state == OverkizCommandParam.CLOSED
86 
87  # Keep this condition after the previous one. Some device like the pedestrian gate, always return 50 as position.
88  if self.current_cover_positioncurrent_cover_positioncurrent_cover_positioncurrent_cover_position is not None:
90 
93 
94  return None
95 
96  async def async_open_cover_tilt(self, **kwargs: Any) -> None:
97  """Open the cover tilt."""
98  if command := self.executorexecutor.select_command(*COMMANDS_OPEN_TILT):
99  await self.executorexecutor.async_execute_command(command)
100 
101  async def async_close_cover_tilt(self, **kwargs: Any) -> None:
102  """Close the cover tilt."""
103  if command := self.executorexecutor.select_command(*COMMANDS_CLOSE_TILT):
104  await self.executorexecutor.async_execute_command(command)
105 
106  async def async_stop_cover(self, **kwargs: Any) -> None:
107  """Stop the cover."""
108  if command := self.executorexecutor.select_command(*COMMANDS_STOP):
109  await self.executorexecutor.async_execute_command(command)
110 
111  async def async_stop_cover_tilt(self, **kwargs: Any) -> None:
112  """Stop the cover tilt."""
113  if command := self.executorexecutor.select_command(*COMMANDS_STOP_TILT):
114  await self.executorexecutor.async_execute_command(command)
115 
116  def is_running(self, commands: list[OverkizCommand]) -> bool:
117  """Return if the given commands are currently running."""
118  return any(
119  execution.get("device_url") == self.devicedevice.device_url
120  and execution.get("command_name") in commands
121  for execution in self.coordinator.executions.values()
122  )
123 
124  @property
125  def supported_features(self) -> CoverEntityFeature:
126  """Flag supported features."""
127  supported_features = CoverEntityFeature(0)
128 
129  if self.executorexecutor.has_command(*COMMANDS_OPEN_TILT):
130  supported_features |= CoverEntityFeature.OPEN_TILT
131 
132  if self.executorexecutor.has_command(*COMMANDS_STOP_TILT):
133  supported_features |= CoverEntityFeature.STOP_TILT
134 
135  if self.executorexecutor.has_command(*COMMANDS_CLOSE_TILT):
136  supported_features |= CoverEntityFeature.CLOSE_TILT
137 
138  if self.executorexecutor.has_command(*COMMANDS_SET_TILT_POSITION):
139  supported_features |= CoverEntityFeature.SET_TILT_POSITION
140 
141  return supported_features