-
Notifications
You must be signed in to change notification settings - Fork 13
/
dueros.py
executable file
·495 lines (429 loc) · 17 KB
/
dueros.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
import json, math, time
import logging
import voluptuous as vol
from homeassistant.helpers import config_validation as cv
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import (MAJOR_VERSION, MINOR_VERSION)
from homeassistant.auth.const import ACCESS_TOKEN_EXPIRATION
import homeassistant.util.color as color_util
import homeassistant.auth.models as models
from typing import Optional
from datetime import timedelta
from homeassistant.helpers.state import AsyncTrackStates
from urllib.request import urlopen
_LOGGER = logging.getLogger(__name__)
MAIN = 'dueros'
EXPIRE_HOURS = 'expire_hours'
DOMAIN = 'dueros'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(EXPIRE_HOURS): cv.positive_int
})
}, extra=vol.ALLOW_EXTRA)
_hass = None
_expire_hours = None
async def async_create_refresh_token77(
user: models.User, client_id: Optional[str] = None) \
-> models.RefreshToken:
"""Create a new token for a user."""
_LOGGER.info('access token expiration: %d hours', _expire_hours)
refresh_token = models.RefreshToken(user=user,
client_id=client_id,
access_token_expiration = timedelta(hours=_expire_hours))
user.refresh_tokens[refresh_token.id] = refresh_token
_hass.auth._store._async_schedule_save()
return refresh_token
async def async_create_refresh_token78(
user: models.User, client_id: Optional[str] = None,
client_name: Optional[str] = None,
client_icon: Optional[str] = None,
token_type: str = models.TOKEN_TYPE_NORMAL,
access_token_expiration: timedelta = ACCESS_TOKEN_EXPIRATION) \
-> models.RefreshToken:
if access_token_expiration == ACCESS_TOKEN_EXPIRATION:
access_token_expiration = timedelta(hours=_expire_hours)
_LOGGER.info('Access token expiration: %d hours', _expire_hours)
"""Create a new token for a user."""
kwargs = {
'user': user,
'client_id': client_id,
'token_type': token_type,
'access_token_expiration': access_token_expiration
} # type: Dict[str, Any]
if client_name:
kwargs['client_name'] = client_name
if client_icon:
kwargs['client_icon'] = client_icon
refresh_token = models.RefreshToken(**kwargs)
user.refresh_tokens[refresh_token.id] = refresh_token
_hass.auth._store._async_schedule_save()
return refresh_token
async def async_setup(hass, config):
global _hass, _expire_hours
_hass = hass
_expire_hours = config[DOMAIN].get(EXPIRE_HOURS)
if _expire_hours is not None:
if MAJOR_VERSION == 0 and MINOR_VERSION <= 77:
_hass.auth._store.async_create_refresh_token = async_create_refresh_token77
else:
_hass.auth._store.async_create_refresh_token = async_create_refresh_token78
_hass.http.register_view(DuerosGateView)
return True
class DuerosGateView(HomeAssistantView):
"""View to handle Configuration requests."""
url = '/dueros_gate'
name = 'dueros_gate'
requires_auth = False
async def post(self, request):
"""Update state of entity."""
try:
data = await request.json()
response = await handleRequest(data)
except:
import traceback
_LOGGER.error(traceback.format_exc())
response = {'header': {'name': 'errorResult'}, 'payload': errorResult('SERVICE_ERROR', 'service exception')}
return self.json(response)
def errorResult(errorCode, messsage=None):
"""Generate error result"""
messages = {
'INVALIDATE_CONTROL_ORDER': 'invalidate control order',
'SERVICE_ERROR': 'service error',
'DEVICE_NOT_SUPPORT_FUNCTION': 'device not support',
'INVALIDATE_PARAMS': 'invalidate params',
'DEVICE_IS_NOT_EXIST': 'device is not exist',
'IOT_DEVICE_OFFLINE': 'device is offline',
'ACCESS_TOKEN_INVALIDATE': ' access_token is invalidate'
}
return {'errorCode': errorCode, 'message': messsage if messsage else messages[errorCode]}
async def handleRequest(data):
"""Handle request"""
header = data['header']
payload = data['payload']
properties = None
name = header['name']
_LOGGER.info("Handle Request: %s", data)
token = await _hass.auth.async_validate_access_token(payload['accessToken'])
if token is not None:
namespace = header['namespace']
if namespace == 'DuerOS.ConnectedHome.Discovery':
name = 'DiscoverAppliancesResponse'
result = discoveryDevice()
elif namespace == 'DuerOS.ConnectedHome.Control':
result = await controlDevice(name, payload)
elif namespace == 'DuerOS.ConnectedHome.Query':
result = queryDevice(name, payload)
if not 'errorCode' in result:
properties = result
result = {}
else:
result = errorResult('SERVICE_ERROR')
else:
result = errorResult('ACCESS_TOKEN_INVALIDATE')
# Check error and fill response name
header['name'] = name
# Fill response deviceId
if 'deviceId' in payload:
result['deviceId'] = payload['deviceId']
response = {'header': header, 'payload': result}
if properties:
response['properties'] = properties
_LOGGER.info("Respnose: %s", response)
return response
def discoveryDevice():
states = _hass.states.async_all()
# groups_ttributes = groupsAttributes(states)
devices = []
# devices.append({
# 'applianceId': 'sensor.temperature_158d0001830246',
# 'friendlyName': '卧室温度',
# 'friendlyDescription': '卧室温度',
# 'additionalApplianceDetails': [],
# 'applianceTypes': ['AIR_CONDITION'],
# 'isReachable': True,
# 'manufacturerName': 'HomeAssistant',
# 'modelName': 'HomeAssistantLight',
# 'version': '1.0',
# 'actions': ["getTemperatureReading"],
# })
for state in states:
attributes = state.attributes
if attributes.get('hidden') or attributes.get('dueros_hidden'):
continue
friendly_name = attributes.get('friendly_name')
if friendly_name is None:
continue
entity_id = state.entity_id
deviceTypes = guessDeviceType(entity_id, attributes)
if deviceTypes is None:
continue
actions = guessAction(entity_id, attributes)
devices.append({
'applianceId': entity_id,
'friendlyName': friendly_name,
'friendlyDescription': friendly_name,
'additionalApplianceDetails': [],
'applianceTypes': deviceTypes,
'isReachable': True,
'manufacturerName': 'HomeAssistant',
'modelName': 'HomeAssistantLight',
'version': '1.0',
'actions': actions,
})
#for sensor in devices:
#if sensor['deviceType'] == 'sensor':
#_LOGGER.info(json.dumps(sensor, indent=2, ensure_ascii=False))
return {'discoveredAppliances': devices}
async def controlDevice(action, payload):
applianceDic = payload['appliance']
entity_id = applianceDic['applianceId']
domain = entity_id[:entity_id.find('.')]
data = {"entity_id": entity_id }
if domain in TRANSLATIONS.keys():
translation = TRANSLATIONS[domain][action]
if callable(translation):
service, content = translation(_hass.states.get(entity_id), payload)
data.update(content)
else:
service = translation
else:
service = getControlService(action)
_LOGGER.info(_hass.states.get(entity_id).attributes)
with AsyncTrackStates(_hass) as changed_states:
result = await _hass.services.async_call(domain, service, data, True)
return {} if result else errorResult('IOT_DEVICE_OFFLINE')
def queryDevice(name, payload):
deviceId = payload['deviceId']
if payload['deviceType'] == 'sensor':
states = _hass.states.async_all()
entity_ids = []
for state in states:
attributes = state.attributes
if state.entity_id.startswith('group.') and (attributes['friendly_name'] == deviceId or attributes.get('hagenie_zone') == deviceId):
entity_ids = attributes.get('entity_id')
break
properties = [{'name':'powerstate', 'value':'on'}]
for state in states:
entity_id = state.entity_id
attributes = state.attributes
if entity_id.startswith('sensor.') and (entity_id in entity_ids or attributes['friendly_name'].startswith(deviceId) or attributes.get('hagenie_zone') == deviceId):
prop,action = guessAction(entity_id, attributes, state.state)
if prop is None:
continue
properties.append(prop)
return properties
else:
state = _hass.states.get(deviceId)
if state is not None or state.state != 'unavailable':
return {'name':'powerstate', 'value':state.state}
return errorResult('IOT_DEVICE_OFFLINE')
def getControlService(action):
i = 0
service = ''
for c in action:
service += (('_' if i else '') + c.lower()) if c.isupper() else c
i += 1
return service
def hsv2rgb(hsvColorDic):
h = float(hsvColorDic['hue'])
s = float(hsvColorDic['saturation'])
v = float(hsvColorDic['brightness'])
rgb = color_util.color_hsv_to_RGB(h, s, v)
return rgb
def timestamp2Delay(timestamp):
delay = abs(int(time.time()) - timestamp)
_LOGGER.info(delay)
return delay
DEVICE_TYPES = [
'TV_SET',#: '电视',
'LIGHT',#: '灯',
'AIR_CONDITION',#: '空调',
'AIR_PURIFIER',#: '空气净化器',
'AIR_MONITOR',#: '空气监测器类设备',
'SOCKET',#: '插座',
'SWITCH',#: '开关',
'HEATER',#: '电暖器类设备',
'CLOTHES_RACK',# '晾衣架',
'GAS_STOVE',# '燃气灶类设备',
'SWEEPING_ROBOT',#: '扫地机器人',
'CURTAIN',#: '窗帘',
'HUMIDIFIER',#: '加湿器',
'FAN',#: '风扇',
'KETTLE',#: '电热水壶',
'RICE_COOKER',#: '电饭煲',
'WATER_HEATER',#: '热水器',
'OVEN',#: '烤箱',
'WATER_PURIFIER',#: '净水器',
'FRIDGE',#: '冰箱',
'SET_TOP_BOX',#: '机顶盒',
'WASHING_MACHINE',#: '洗衣机',
'WINDOW_OPENER',#: '窗',
'RANGE_HOOD',#: '抽油烟机',
]
INCLUDE_DOMAINS = {
'climate': 'AIR_CONDITION',
'fan': 'FAN',
'light': 'LIGHT',
'media_player': 'TV_SET',
'switch': 'SWITCH',
'vacuum': 'SWEEPING_ROBOT',
# 'sensor': 'AIR_MONITOR',
'cover': 'CURTAIN'
}
EXCLUDE_DOMAINS = [
'automation',
'binary_sensor',
'device_tracker',
'group',
'zone',
'sun',
]
ALL_ACTIONS = [
'turnOn', # 打开
'timingTurnOn', # 定时打开
'turnOff', # 关闭
'timingTurnOff', # 定时关闭
'pause', # 暂停
'continue', # 继续
'setBrightnessPercentage', # 设置灯光亮度
'incrementBrightnessPercentage', # 调亮灯光
'decrementBrightnessPercentage', # 调暗灯光
'incrementTemperature', # 升高温度
'decrementTemperature', # 降低温度
'setTemperature', # 设置温度
'incrementVolume', # 调高音量
'decrementVolume', # 调低音量
'setVolume', # 设置音量
'setVolumeMute', # 设置设备静音状态
'incrementFanSpeed', # 增加风速
'decrementFanSpeed', # 减小风速
'setFanSpeed', # 设置风速
'setMode', # 设置模式
'unSetMode', # 取消设置的模式
'timingSetMode', # 定时设置模式
'timingUnsetMode', # 定时取消设置的模式
'setColor', # 设置颜色
'getAirQualityIndex', # 查询空气质量
'getAirPM25', # 查询PM2.5
'getTemperatureReading', # 查询温度
'getTargetTemperature', # 查询目标温度
'getHumidity', # 查询湿度
'getTimeLeft', # 查询剩余时间
'getRunningTime', # 查询运行时间
'getRunningStatus', # 查询运行状态
'getWaterQuality', # 查询水质
'setHumidity', # 设置湿度模式
'setLockState', # 上锁解锁
'getLockState', # 查询锁状态
'incrementPower', # 增大功率
'decrementPower', # 减小功率
'returnTVChannel', # 返回上个频道
'decrementTVChannel', # 上一个频道
'incrementTVChannel', # 下一个频道
'setTVChannel', # 设置频道
'decrementHeight', # 降低高度
'incrementHeight', # 升高高度
'chargeTurnOn', # 开始充电
'chargeTurnOff', # 停止充电
'submitPrint', #打印
'getTurnOnState', #查询设备打开状态
'setSuction', # 设置吸力
'setDirection', # 设置移动方向
'getElectricityCapacity', # 查询电量
'getOilCapacity', # 查询油量
]
TRANSLATIONS = {
'cover': {
'TurnOnRequest': 'open_cover',
'TurnOffRequest': 'close_cover',
'TimingTurnOnRequest': 'open_cover',
'TimingTurnOffRequest': 'close_cover',
},
'vacuum': {
'TurnOnRequest': 'start',
'TurnOffRequest': 'return_to_base',
'TimingTurnOnRequest': 'start',
'TimingTurnOffRequest': 'return_to_base',
'SetSuctionRequest': lambda state, payload: ('set_fan_speed', {'fan_speed': 90 if payload['suction']['value'] == 'STRONG' else 60}),
},
'switch': {
'TurnOnRequest': 'turn_on',
'TurnOffRequest': 'turn_off',
'TimingTurnOnRequest': 'turn_on',
'TimingTurnOffRequest': 'turn_off'
},
'light': {
'TurnOnRequest': 'turn_on',
'TurnOffRequest': 'turn_off',
'TimingTurnOnRequest': 'turn_on',
'TimingTurnOffRequest': 'turn_off',
'SetBrightnessPercentageRequest': lambda state, payload: ('turn_on', {'brightness_pct': payload['brightness']['value']}),
'IncrementBrightnessPercentageRequest': lambda state, payload: ('turn_on', {'brightness_pct': min(state.attributes['brightness'] / 255 * 100 + payload['deltaPercentage'][
'value'], 100)}),
'DecrementBrightnessPercentageRequest': lambda state, payload: ('turn_on', {'brightness_pct': max(state.attributes['brightness'] / 255 * 100 - payload['deltaPercentage']['value'], 0)}),
'SetColorRequest': lambda state, payload: ('turn_on', {"hs_color": [float(payload['color']['hue']), float(payload['color']['saturation']) * 100]})
},
}
def guessDeviceType(entity_id, attributes):
deviceTypes = []
if 'dueros_deviceType' in attributes:
deviceTypes = attributes['dueros_deviceType']
return deviceTypes
# Exclude with domain
domain = entity_id[:entity_id.find('.')]
if domain in EXCLUDE_DOMAINS:
return None
# Guess from entity_id
for deviceType in DEVICE_TYPES:
if deviceType in entity_id:
deviceTypes.append(deviceType)
# Map from domain
if domain in INCLUDE_DOMAINS:
deviceTypes.append(INCLUDE_DOMAINS[domain])
return deviceTypes
def groupsAttributes(states):
groups_attributes = []
for state in states:
group_entity_id = state.entity_id
if group_entity_id.startswith('group.') and not group_entity_id.startswith('group.all_') and group_entity_id != 'group.default_view':
group_attributes = state.attributes
if 'entity_id' in group_attributes:
groups_attributes.append(group_attributes)
return groups_attributes
def guessAction(entity_id, attributes):
# Support On/Off/Query only at this time
if 'dueros_actions' in attributes:
actions = attributes['dueros_actions']
elif entity_id.startswith('switch.'):
actions = ["turnOn", "timingTurnOn", "turnOff", "timingTurnOff"]
elif entity_id.startswith('light.'):
actions = ["turnOn", "timingTurnOn", "turnOff", "timingTurnOff", "setBrightnessPercentage", "incrementBrightnessPercentage", "decrementBrightnessPercentage", "setColor"]
elif entity_id.startswith('cover.'):
actions = ["turnOn", "timingTurnOn", "turnOff", "timingTurnOff", "pause"]
elif entity_id.startswith('vacuum.'):
actions = ["turnOn", "timingTurnOn", "turnOff", "timingTurnOff", "setSuction"]
elif entity_id.startswith('sensor.'):
actions = ["getTemperatureReading", "getHumidity"]
else:
actions = ["turnOn", "timingTurnOn", "turnOff", "timingTurnOff"]
# elif entity_id.startswith('sensor.'):
# unit = attributes['unit_of_measurement'] if 'unit_of_measurement' in attributes else ''
# if unit == u'°C' or unit == u'℃':
# name = 'Temperature'
# elif unit == 'lx' or unit == 'lm':
# name = 'Brightness'
# elif ('hcho' in entity_id):
# name = 'Fog'
# elif ('humidity' in entity_id):
# name = 'Humidity'
# elif ('pm25' in entity_id):
# name = 'PM2.5'
# elif ('co2' in entity_id):
# name = 'WindSpeed'
# else:
# return (None, None)
# else:
# name = 'PowerState'
# if state != 'off':
# state = 'on'
return actions