-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
cover.py
395 lines (321 loc) · 13.3 KB
/
cover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""Support for TaHoma cover - shutters etc."""
import logging
from homeassistant.components.cover import (
ATTR_POSITION,
ATTR_TILT_POSITION,
DEVICE_CLASS_AWNING,
DEVICE_CLASS_BLIND,
DEVICE_CLASS_CURTAIN,
DEVICE_CLASS_GARAGE,
DEVICE_CLASS_GATE,
DEVICE_CLASS_SHUTTER,
DEVICE_CLASS_WINDOW,
DOMAIN as COVER,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP,
SUPPORT_STOP_TILT,
CoverEntity,
)
from homeassistant.helpers import entity_platform
import voluptuous as vol
from .const import DOMAIN
from .tahoma_entity import TahomaEntity
_LOGGER = logging.getLogger(__name__)
ATTR_OBSTRUCTION_DETECTED = "obstruction-detected"
COMMAND_CYCLE = "cycle"
COMMAND_CLOSE = "close"
COMMAND_CLOSE_SLATS = "closeSlats"
COMMAND_DOWN = "down"
COMMAND_MY = "my"
COMMAND_OPEN = "open"
COMMAND_OPEN_SLATS = "openSlats"
COMMAND_SET_CLOSURE = "setClosure"
COMMAND_SET_ORIENTATION = "setOrientation"
COMMAND_SET_PEDESTRIAN_POSITION = "setPedestrianPosition"
COMMAND_SET_POSITION = "setPosition"
COMMAND_SET_POSITION_AND_LINEAR_SPEED = "setPositionAndLinearSpeed"
COMMAND_STOP = "stop"
COMMAND_STOP_IDENTIFY = "stopIdentify"
COMMAND_UP = "up"
COMMANDS_STOP = [COMMAND_STOP, COMMAND_STOP_IDENTIFY, COMMAND_MY]
COMMANDS_STOP_TILT = [COMMAND_STOP, COMMAND_STOP_IDENTIFY, COMMAND_MY]
COMMANDS_OPEN = [COMMAND_OPEN, COMMAND_UP, COMMAND_CYCLE]
COMMANDS_OPEN_TILT = [COMMAND_OPEN_SLATS]
COMMANDS_CLOSE = [COMMAND_CLOSE, COMMAND_DOWN, COMMAND_CYCLE]
COMMANDS_CLOSE_TILT = [COMMAND_CLOSE_SLATS]
COMMANDS_SET_POSITION = [
COMMAND_SET_POSITION,
COMMAND_SET_CLOSURE,
COMMAND_SET_PEDESTRIAN_POSITION,
]
COMMANDS_SET_TILT_POSITION = [COMMAND_SET_ORIENTATION]
CORE_CLOSURE_STATE = "core:ClosureState"
CORE_CLOSURE_OR_ROCKER_POSITION_STATE = "core:ClosureOrRockerPositionState"
CORE_DEPLOYMENT_STATE = "core:DeploymentState"
CORE_MEMORIZED_1_POSITION_STATE = "core:Memorized1PositionState"
CORE_OPEN_CLOSED_PARTIAL_STATE = "core:OpenClosedPartialState"
CORE_OPEN_CLOSED_PEDESTRIAN_STATE = "core:OpenClosedPedestrianState"
CORE_OPEN_CLOSED_STATE = "core:OpenClosedState"
CORE_OPEN_CLOSED_UNKNOWN_STATE = "core:OpenClosedUnknownState"
CORE_PEDESTRIAN_POSITION_STATE = "core:PedestrianPositionState"
CORE_PRIORITY_LOCK_TIMER_STATE = "core:PriorityLockTimerState"
CORE_SLATS_OPEN_CLOSED_STATE = "core:SlatsOpenClosedState"
CORE_SLATE_ORIENTATION_STATE = "core:SlateOrientationState"
CORE_SLATS_ORIENTATION_STATE = "core:SlatsOrientationState"
CORE_TARGET_CLOSURE_STATE = "core:TargetClosureState"
MYFOX_SHUTTER_STATUS_STATE = "myfox:ShutterStatusState"
ICON_LOCK_ALERT = "mdi:lock-alert"
ICON_WEATHER_WINDY = "mdi:weather-windy"
IO_PRIORITY_LOCK_LEVEL_STATE = "io:PriorityLockLevelState"
IO_PRIORITY_LOCK_ORIGINATOR_STATE = "io:PriorityLockOriginatorState"
STATE_CLOSED = "closed"
SERVICE_COVER_MY_POSITION = "set_cover_my_position"
SERVICE_COVER_POSITION_LOW_SPEED = "set_cover_position_low_speed"
SUPPORT_MY = 512
SUPPORT_COVER_POSITION_LOW_SPEED = 1024
TAHOMA_COVER_DEVICE_CLASSES = {
"Awning": DEVICE_CLASS_AWNING,
"Blind": DEVICE_CLASS_BLIND,
"Curtain": DEVICE_CLASS_CURTAIN,
"ExteriorScreen": DEVICE_CLASS_BLIND,
"ExteriorVenetianBlind": DEVICE_CLASS_BLIND,
"GarageDoor": DEVICE_CLASS_GARAGE,
"Gate": DEVICE_CLASS_GATE,
"MyFoxSecurityCamera": DEVICE_CLASS_SHUTTER,
"Pergola": DEVICE_CLASS_AWNING,
"RollerShutter": DEVICE_CLASS_SHUTTER,
"SwingingShutter": DEVICE_CLASS_SHUTTER,
"VeluxInteriorBlind": DEVICE_CLASS_BLIND,
"Window": DEVICE_CLASS_WINDOW,
}
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the TaHoma covers from a config entry."""
data = hass.data[DOMAIN][entry.entry_id]
coordinator = data["coordinator"]
entities = [
TahomaCover(device.deviceurl, coordinator)
for device in data["platforms"].get(COVER)
]
async_add_entities(entities)
platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
SERVICE_COVER_MY_POSITION, {}, "async_my", [SUPPORT_MY]
)
platform.async_register_entity_service(
SERVICE_COVER_POSITION_LOW_SPEED,
{
vol.Required(ATTR_POSITION): vol.All(
vol.Coerce(int), vol.Range(min=0, max=100)
)
},
"async_set_cover_position_low_speed",
[SUPPORT_COVER_POSITION_LOW_SPEED],
)
class TahomaCover(TahomaEntity, CoverEntity):
"""Representation of a TaHoma Cover."""
@property
def current_cover_position(self):
"""
Return current position of cover.
None is unknown, 0 is closed, 100 is fully open.
"""
position = self.select_state(
CORE_CLOSURE_STATE,
CORE_DEPLOYMENT_STATE,
CORE_PEDESTRIAN_POSITION_STATE,
CORE_TARGET_CLOSURE_STATE,
CORE_CLOSURE_OR_ROCKER_POSITION_STATE,
)
# Uno devices can have a position not in 0 to 100 range when unknown
if position is None or position < 0 or position > 100:
return None
if not self._reversed_position_device():
position = 100 - position
return position
@property
def current_cover_tilt_position(self):
"""Return current position of cover tilt.
None is unknown, 0 is closed, 100 is fully open.
"""
position = self.select_state(
CORE_SLATS_ORIENTATION_STATE, CORE_SLATE_ORIENTATION_STATE
)
return 100 - position if position is not None else None
async def async_set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
position = 100 - kwargs.get(ATTR_POSITION, 0)
if self._reversed_position_device():
position = kwargs.get(ATTR_POSITION, 0)
await self.async_execute_command(
self.select_command(*COMMANDS_SET_POSITION), position
)
async def async_set_cover_position_low_speed(self, **kwargs):
"""Move the cover to a specific position with a low speed."""
position = 100 - kwargs.get(ATTR_POSITION, 0)
if self._reversed_position_device():
position = kwargs.get(ATTR_POSITION, 0)
await self.async_execute_command(
COMMAND_SET_POSITION_AND_LINEAR_SPEED, position, "lowspeed"
)
async def async_set_cover_tilt_position(self, **kwargs):
"""Move the cover tilt to a specific position."""
await self.async_execute_command(
self.select_command(*COMMANDS_SET_TILT_POSITION),
100 - kwargs.get(ATTR_TILT_POSITION, 0),
)
@property
def is_closed(self):
"""Return if the cover is closed."""
state = self.select_state(
CORE_OPEN_CLOSED_STATE,
CORE_SLATS_OPEN_CLOSED_STATE,
CORE_OPEN_CLOSED_PARTIAL_STATE,
CORE_OPEN_CLOSED_PEDESTRIAN_STATE,
CORE_OPEN_CLOSED_UNKNOWN_STATE,
MYFOX_SHUTTER_STATUS_STATE,
)
if state is not None:
return state == STATE_CLOSED
if self.current_cover_position is not None:
return self.current_cover_position == 0
if self.current_cover_tilt_position is not None:
return self.current_cover_tilt_position == 0
return None
@property
def device_class(self):
"""Return the class of the device."""
return (
TAHOMA_COVER_DEVICE_CLASSES.get(self.device.widget)
or TAHOMA_COVER_DEVICE_CLASSES.get(self.device.ui_class)
or DEVICE_CLASS_BLIND
)
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
if (
self.has_state(CORE_PRIORITY_LOCK_TIMER_STATE)
and self.select_state(CORE_PRIORITY_LOCK_TIMER_STATE) > 0
):
if self.select_state(IO_PRIORITY_LOCK_ORIGINATOR_STATE) == "wind":
return ICON_WEATHER_WINDY
return ICON_LOCK_ALERT
return None
async def async_open_cover(self, **_):
"""Open the cover."""
await self.async_execute_command(self.select_command(*COMMANDS_OPEN))
async def async_open_cover_tilt(self, **_):
"""Open the cover tilt."""
await self.async_execute_command(self.select_command(*COMMANDS_OPEN_TILT))
async def async_close_cover(self, **_):
"""Close the cover."""
await self.async_execute_command(self.select_command(*COMMANDS_CLOSE))
async def async_close_cover_tilt(self, **_):
"""Close the cover tilt."""
await self.async_execute_command(self.select_command(*COMMANDS_CLOSE_TILT))
async def async_stop_cover(self, **_):
"""Stop the cover."""
await self.async_cancel_or_stop_cover(
COMMANDS_OPEN + COMMANDS_SET_POSITION + COMMANDS_CLOSE,
COMMANDS_STOP,
)
async def async_stop_cover_tilt(self, **_):
"""Stop the cover tilt."""
await self.async_cancel_or_stop_cover(
COMMANDS_OPEN_TILT + COMMANDS_SET_TILT_POSITION + COMMANDS_CLOSE_TILT,
COMMANDS_STOP_TILT,
)
async def async_cancel_or_stop_cover(self, cancel_commands, stop_commands) -> None:
"""Cancel running execution or send stop command."""
# Cancelling a running execution will stop the cover movement
# Retrieve executions initiated via Home Assistant from Data Update Coordinator queue
exec_id = next(
(
exec_id
# Reverse dictionary to cancel the last added execution
for exec_id, execution in reversed(self.coordinator.executions.items())
if execution.get("deviceurl") == self.device.deviceurl
and execution.get("command_name") in cancel_commands
),
None,
)
if exec_id:
return await self.async_cancel_command(exec_id)
# Retrieve executions initiated outside Home Assistant via API
executions = await self.coordinator.client.get_current_executions()
exec_id = next(
(
execution.id
for execution in executions
# Reverse dictionary to cancel the last added execution
for action in reversed(execution.action_group.get("actions"))
for command in action.get("commands")
if action.get("deviceurl") == self.device.deviceurl
and command.get("name") in cancel_commands
),
None,
)
if exec_id:
return await self.async_cancel_command(exec_id)
# Fallback to available stop commands when no executions are found
# Stop commands don't work with all devices, due to a bug in Somfy service
await self.async_execute_command(self.select_command(*stop_commands))
async def async_my(self, **_):
"""Set cover to preset position."""
await self.async_execute_command(COMMAND_MY)
@property
def is_opening(self):
"""Return if the cover is opening or not."""
return any(
execution.get("deviceurl") == self.device.deviceurl
and execution.get("command_name") in COMMANDS_OPEN + COMMANDS_OPEN_TILT
for execution in self.coordinator.executions.values()
)
@property
def is_closing(self):
"""Return if the cover is closing or not."""
return any(
execution.get("deviceurl") == self.device.deviceurl
and execution.get("command_name") in COMMANDS_CLOSE + COMMANDS_CLOSE_TILT
for execution in self.coordinator.executions.values()
)
@property
def device_state_attributes(self):
"""Return the device state attributes."""
attr = super().device_state_attributes or {}
# Obstruction Detected attribute is used by HomeKit
if self.has_state(IO_PRIORITY_LOCK_LEVEL_STATE):
attr[ATTR_OBSTRUCTION_DETECTED] = True
return attr
@property
def supported_features(self):
"""Flag supported features."""
supported_features = 0
if self.has_command(*COMMANDS_OPEN_TILT):
supported_features |= SUPPORT_OPEN_TILT
if self.has_command(*COMMANDS_STOP_TILT):
supported_features |= SUPPORT_STOP_TILT
if self.has_command(*COMMANDS_CLOSE_TILT):
supported_features |= SUPPORT_CLOSE_TILT
if self.has_command(*COMMANDS_SET_TILT_POSITION):
supported_features |= SUPPORT_SET_TILT_POSITION
if self.has_command(*COMMANDS_SET_POSITION):
supported_features |= SUPPORT_SET_POSITION
if self.has_command(*COMMANDS_OPEN):
supported_features |= SUPPORT_OPEN
if self.has_command(*COMMANDS_STOP):
supported_features |= SUPPORT_STOP
if self.has_command(*COMMANDS_CLOSE):
supported_features |= SUPPORT_CLOSE
if self.has_command(COMMAND_SET_POSITION_AND_LINEAR_SPEED):
supported_features |= SUPPORT_COVER_POSITION_LOW_SPEED
if self.has_command(COMMAND_MY):
supported_features |= SUPPORT_MY
return supported_features
def _reversed_position_device(self):
"""Return true if the device need a reversed position that can not be obtained via the API."""
return "Horizontal" in self.device.widget