forked from dext0r/yandex_smart_home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capability.py
630 lines (507 loc) · 19.5 KB
/
capability.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
"""Implement the Yandex Smart Home capabilities."""
import logging
from homeassistant.components import (
climate,
cover,
group,
fan,
input_boolean,
media_player,
light,
switch,
vacuum,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
SERVICE_CLOSE_COVER,
SERVICE_OPEN_COVER,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
SERVICE_VOLUME_MUTE,
STATE_OFF,
STATE_ON,
)
from homeassistant.core import DOMAIN as HA_DOMAIN
from homeassistant.util import color as color_util
from .const import (
ERR_INVALID_VALUE,
ERR_NOT_SUPPORTED_IN_CURRENT_MODE
)
from .error import SmartHomeError
_LOGGER = logging.getLogger(__name__)
PREFIX_CAPABILITIES = 'devices.capabilities.'
CAPABILITIES_ONOFF = PREFIX_CAPABILITIES + 'on_off'
CAPABILITIES_TOGGLE = PREFIX_CAPABILITIES + 'toggle'
CAPABILITIES_RANGE = PREFIX_CAPABILITIES + 'range'
CAPABILITIES_MODE = PREFIX_CAPABILITIES + 'mode'
CAPABILITIES_COLOR_SETTING = PREFIX_CAPABILITIES + 'color_setting'
CAPABILITIES = []
def register_capability(capability):
"""Decorate a function to register a capability."""
CAPABILITIES.append(capability)
return capability
class _Capability:
"""Represents a Capability."""
type = ''
instance = ''
retrievable = True
def __init__(self, hass, state, config):
"""Initialize a trait for a state."""
self.hass = hass
self.state = state
self.config = config
def description(self):
"""Return description for a devices request."""
response = {
'type': self.type,
'retrievable': self.retrievable,
}
parameters = self.parameters()
if parameters is not None:
response['parameters'] = parameters
return response
def get_state(self):
"""Return the state of this capability for this entity."""
return {
'type': self.type,
'state': {
'instance': self.instance,
'value': self.get_value()
}
}
def parameters(self):
"""Return parameters for a devices request."""
raise NotImplementedError
def get_value(self):
"""Return the state value of this capability for this entity."""
raise NotImplementedError
async def set_state(self, data, state):
"""Set device state."""
raise NotImplementedError
@register_capability
class OnOffCapability(_Capability):
"""On_off to offer basic on and off functionality.
https://yandex.ru/dev/dialogs/alice/doc/smart-home/concepts/on_off-docpage/
"""
type = CAPABILITIES_ONOFF
instance = 'on'
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain in (
cover.DOMAIN,
group.DOMAIN,
input_boolean.DOMAIN,
switch.DOMAIN,
fan.DOMAIN,
light.DOMAIN,
media_player.DOMAIN,
climate.DOMAIN,
) or (vacuum.DOMAIN
and ((features & vacuum.SUPPORT_START
and (features & vacuum.SUPPORT_RETURN_HOME
or features & vacuum.SUPPORT_STOP)
) or (features & vacuum.SUPPORT_TURN_ON
and features & vacuum.SUPPORT_TURN_OFF
)))
def parameters(self):
"""Return parameters for a devices request."""
return None
def get_value(self):
"""Return the state value of this capability for this entity."""
if self.state.domain == cover.DOMAIN:
return self.state.state != cover.STATE_OPEN
elif self.state.domain == vacuum.DOMAIN:
return self.state.state == STATE_ON or self.state.state == \
vacuum.STATE_CLEANING
elif self.state.domain == climate.DOMAIN:
return self.state.state != climate.HVAC_MODE_OFF
else:
return self.state.state != STATE_OFF
async def set_state(self, data, state):
"""Set device state."""
domain = self.state.domain
if type(state['value']) is not bool:
raise SmartHomeError(ERR_INVALID_VALUE, "Value is not boolean")
if domain == group.DOMAIN:
service_domain = HA_DOMAIN
service = SERVICE_TURN_ON if state['value'] else SERVICE_TURN_OFF
elif domain == cover.DOMAIN:
service_domain = domain
service = SERVICE_CLOSE_COVER if state['value'] else \
SERVICE_OPEN_COVER
elif domain == vacuum.DOMAIN:
service_domain = domain
features = self.state.attributes.get(ATTR_SUPPORTED_FEATURES)
if state['value']:
if features & vacuum.SUPPORT_START:
service = vacuum.SERVICE_START
else:
service = SERVICE_TURN_ON
else:
if features & vacuum.SUPPORT_RETURN_HOME:
service = vacuum.SERVICE_RETURN_TO_BASE
elif features & vacuum.SUPPORT_STOP:
service = vacuum.SERVICE_STOP
else:
service = SERVICE_TURN_OFF
else:
service_domain = domain
service = SERVICE_TURN_ON if state['value'] else SERVICE_TURN_OFF
await self.hass.services.async_call(service_domain, service, {
ATTR_ENTITY_ID: self.state.entity_id
}, blocking=True, context=data.context)
@register_capability
class ToggleCapability(_Capability):
"""Mute and unmute functionality.
https://yandex.ru/dev/dialogs/alice/doc/smart-home/concepts/toggle-docpage/
"""
type = CAPABILITIES_TOGGLE
instance = 'mute'
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == media_player.DOMAIN and features & \
media_player.SUPPORT_VOLUME_MUTE
def parameters(self):
"""Return parameters for a devices request."""
return {
'instance': self.instance
}
def get_value(self):
"""Return the state value of this capability for this entity."""
muted = self.state.attributes.get(media_player.ATTR_MEDIA_VOLUME_MUTED)
return bool(muted)
async def set_state(self, data, state):
"""Set device state."""
if type(state['value']) is not bool:
raise SmartHomeError(ERR_INVALID_VALUE, "Value is not boolean")
muted = self.state.attributes.get(media_player.ATTR_MEDIA_VOLUME_MUTED)
if muted is None:
raise SmartHomeError(ERR_NOT_SUPPORTED_IN_CURRENT_MODE,
"Device probably turned off")
await self.hass.services.async_call(
self.state.domain,
SERVICE_VOLUME_MUTE, {
ATTR_ENTITY_ID: self.state.entity_id,
media_player.ATTR_MEDIA_VOLUME_MUTED: state['value']
}, blocking=True, context=data.context)
class _ModeCapability(_Capability):
"""Base class of capabilities with mode functionality like thermostat mode
or fan speed.
https://yandex.ru/dev/dialogs/alice/doc/smart-home/concepts/mode-docpage/
"""
type = CAPABILITIES_MODE
@register_capability
class ThermostatCapability(_ModeCapability):
"""Thermostat functionality"""
instance = 'thermostat'
climate_map = {
climate.const.HVAC_MODE_HEAT: 'heat',
climate.const.HVAC_MODE_COOL: 'cool',
climate.const.HVAC_MODE_AUTO: 'auto',
climate.const.HVAC_MODE_DRY: 'dry',
climate.const.HVAC_MODE_FAN_ONLY: 'fan_only'
}
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == climate.DOMAIN
def parameters(self):
"""Return parameters for a devices request."""
operation_list = self.state.attributes.get(climate.ATTR_HVAC_MODES)
modes = []
for operation in operation_list:
if operation in self.climate_map:
modes.append({'value': self.climate_map[operation]})
return {
'instance': self.instance,
'modes': modes,
'ordered': False
}
def get_value(self):
"""Return the state value of this capability for this entity."""
operation = self.state.attributes.get(climate.ATTR_HVAC_MODE)
if operation is not None and operation in self.climate_map:
return self.climate_map[operation]
return 'auto'
async def set_state(self, data, state):
"""Set device state."""
value = None
for climate_value, yandex_value in self.climate_map.items():
if yandex_value == state['value']:
value = climate_value
break
if value is None:
raise SmartHomeError(ERR_INVALID_VALUE, "Unacceptable value")
await self.hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_HVAC_MODE, {
ATTR_ENTITY_ID: self.state.entity_id,
climate.ATTR_HVAC_MODE: value
}, blocking=True, context=data.context)
@register_capability
class FanSpeedCapability(_ModeCapability):
"""Fan speed functionality."""
instance = 'fan_speed'
values = {
'auto': ['auto'],
'low': ['low', 'min', 'silent'],
'medium': ['medium', 'middle'],
'high': ['high', 'max', 'strong', 'favorite'],
}
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
if domain == climate.DOMAIN:
return features & climate.SUPPORT_FAN_MODE
elif domain == fan.DOMAIN:
return features & fan.SUPPORT_SET_SPEED
return False
def parameters(self):
"""Return parameters for a devices request."""
if self.state.domain == climate.DOMAIN:
speed_list = self.state.attributes.get(climate.ATTR_FAN_MODES)
elif self.state.domain == fan.DOMAIN:
speed_list = self.state.attributes.get(fan.ATTR_SPEED_LIST)
else:
speed_list = []
speeds = []
added = []
for ha_value in speed_list:
value = self.get_yandex_value_by_ha_value(ha_value)
if value is not None and value not in added:
speeds.append({'value': value})
added.append(value)
return {
'instance': self.instance,
'modes': speeds,
'ordered': True
}
def get_yandex_value_by_ha_value(self, ha_value):
for yandex_value, names in self.values.items():
for name in names:
if ha_value.lower().find(name) != -1:
return yandex_value
return None
def get_ha_by_yandex_value(self, yandex_value):
if self.state.domain == climate.DOMAIN:
speed_list = self.state.attributes.get(climate.ATTR_FAN_MODES)
elif self.state.domain == fan.DOMAIN:
speed_list = self.state.attributes.get(fan.ATTR_SPEED_LIST)
else:
speed_list = []
for ha_value in speed_list:
if yandex_value in self.values:
for name in self.values[yandex_value]:
if ha_value.lower().find(name) != -1:
return ha_value
return None
def get_value(self):
"""Return the state value of this capability for this entity."""
if self.state.domain == climate.DOMAIN:
ha_value = self.state.attributes.get(climate.ATTR_FAN_MODE)
elif self.state.domain == fan.DOMAIN:
ha_value = self.state.attributes.get(fan.ATTR_SPEED)
else:
ha_value = None
if ha_value is not None:
yandex_value = self.get_yandex_value_by_ha_value(ha_value)
if yandex_value is not None:
return yandex_value
return 'auto'
async def set_state(self, data, state):
"""Set device state."""
value = self.get_ha_by_yandex_value(state['value'])
if value is None:
raise SmartHomeError(ERR_INVALID_VALUE, "Unsupported value")
if self.state.domain == climate.DOMAIN:
service = climate.SERVICE_SET_FAN_MODE
attr = climate.ATTR_FAN_MODE
elif self.state.domain == fan.DOMAIN:
service = fan.SERVICE_SET_SPEED
attr = fan.ATTR_SPEED
else:
raise SmartHomeError(ERR_INVALID_VALUE, "Unsupported domain")
await self.hass.services.async_call(
self.state.domain,
service, {
ATTR_ENTITY_ID: self.state.entity_id,
attr: value
}, blocking=True, context=data.context)
class _RangeCapability(_Capability):
"""Base class of capabilities with range functionality like volume or
brightness.
https://yandex.ru/dev/dialogs/alice/doc/smart-home/concepts/range-docpage/
"""
type = CAPABILITIES_RANGE
@register_capability
class TemperatureCapability(_RangeCapability):
"""Set temperature functionality."""
instance = 'temperature'
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == climate.DOMAIN and features & \
climate.const.SUPPORT_TARGET_TEMPERATURE
def parameters(self):
"""Return parameters for a devices request."""
min_temp = self.state.attributes.get(climate.ATTR_MIN_TEMP)
max_temp = self.state.attributes.get(climate.ATTR_MAX_TEMP)
return {
'instance': self.instance,
'unit': 'unit.temperature.celsius',
'range': {
'min': min_temp,
'max': max_temp,
'precision': 0.5
}
}
def get_value(self):
"""Return the state value of this capability for this entity."""
temperature = self.state.attributes.get(climate.ATTR_TEMPERATURE)
if temperature is None:
return 0
else:
return float(temperature)
async def set_state(self, data, state):
"""Set device state."""
await self.hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_TEMPERATURE, {
ATTR_ENTITY_ID: self.state.entity_id,
climate.ATTR_TEMPERATURE: state['value']
}, blocking=True, context=data.context)
@register_capability
class BrightnessCapability(_RangeCapability):
"""Set brightness functionality."""
instance = 'brightness'
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == light.DOMAIN and features & light.SUPPORT_BRIGHTNESS
def parameters(self):
"""Return parameters for a devices request."""
return {
'instance': self.instance,
'unit': 'unit.percent',
'range': {
'min': 0,
'max': 100,
'precision': 1
}
}
def get_value(self):
"""Return the state value of this capability for this entity."""
brightness = self.state.attributes.get(light.ATTR_BRIGHTNESS)
if brightness is None:
return 0
else:
return int(100 * (brightness / 255))
async def set_state(self, data, state):
"""Set device state."""
await self.hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_ON, {
ATTR_ENTITY_ID: self.state.entity_id,
light.ATTR_BRIGHTNESS_PCT: state['value']
}, blocking=True, context=data.context)
@register_capability
class VolumeCapability(_RangeCapability):
"""Set volume functionality."""
instance = 'volume'
retrievable = False
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == media_player.DOMAIN and features & \
media_player.SUPPORT_VOLUME_STEP
def parameters(self):
"""Return parameters for a devices request."""
return {
'instance': self.instance
}
async def set_state(self, data, state):
"""Set device state."""
if not state['value']:
raise SmartHomeError(ERR_INVALID_VALUE, "Supported relative mode "
"only")
if state['value'] > 0:
service = media_player.SERVICE_VOLUME_UP
else:
service = media_player.SERVICE_VOLUME_DOWN
await self.hass.services.async_call(
media_player.DOMAIN,
service, {
ATTR_ENTITY_ID: self.state.entity_id
}, blocking=True, context=data.context)
class _ColorSettingCapability(_Capability):
"""Base color setting functionality.
https://yandex.ru/dev/dialogs/alice/doc/smart-home/concepts/color_setting-docpage/
"""
type = CAPABILITIES_COLOR_SETTING
def parameters(self):
"""Return parameters for a devices request."""
result = {}
features = self.state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if features & light.SUPPORT_COLOR:
result['color_model'] = 'rgb'
if features & light.SUPPORT_COLOR_TEMP:
max_temp = self.state.attributes[light.ATTR_MIN_MIREDS]
min_temp = self.state.attributes[light.ATTR_MAX_MIREDS]
result['temperature_k'] = {
'min': color_util.color_temperature_mired_to_kelvin(min_temp),
'max': color_util.color_temperature_mired_to_kelvin(max_temp)
}
return result
@register_capability
class RgbCapability(_ColorSettingCapability):
"""RGB color functionality."""
instance = 'rgb'
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == light.DOMAIN and features & light.SUPPORT_COLOR
def get_value(self):
"""Return the state value of this capability for this entity."""
color = self.state.attributes.get(light.ATTR_RGB_COLOR)
if color is None:
return 0
rgb = color[0]
rgb = (rgb << 8) + color[1]
rgb = (rgb << 8) + color[2]
return rgb
async def set_state(self, data, state):
"""Set device state."""
red = (state['value'] >> 16) & 0xFF
green = (state['value'] >> 8) & 0xFF
blue = state['value'] & 0xFF
await self.hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_ON, {
ATTR_ENTITY_ID: self.state.entity_id,
light.ATTR_RGB_COLOR: (red, green, blue)
}, blocking=True, context=data.context)
@register_capability
class TemperatureKCapability(_ColorSettingCapability):
"""Color temperature functionality."""
instance = 'temperature_k'
@staticmethod
def supported(domain, features):
"""Test if state is supported."""
return domain == light.DOMAIN and features & light.SUPPORT_COLOR_TEMP
def get_value(self):
"""Return the state value of this capability for this entity."""
kelvin = self.state.attributes.get(light.ATTR_COLOR_TEMP)
if kelvin is None:
return 0
return color_util.color_temperature_mired_to_kelvin(kelvin)
async def set_state(self, data, state):
"""Set device state."""
await self.hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_ON, {
ATTR_ENTITY_ID: self.state.entity_id,
light.ATTR_KELVIN: state['value']
}, blocking=True, context=data.context)