forked from MycroftAI/skill-homeassistant
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ha_client.py
325 lines (282 loc) · 12.1 KB
/
ha_client.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
"""
Home Assistant Client
Handle connection between skill and HA instance trough websocket.
"""
import ipaddress
import json
import re
from fuzzywuzzy import fuzz
from requests import get, post
from requests.exceptions import RequestException, Timeout
from requests.models import Response
__author__ = 'btotharye'
# Timeout time for HA requests
TIMEOUT = 10
"""Regex for IP address check"""
IP_REGEX = r"".join(r'\b(?:https?://)?((?:(?:www\.)?(?:[\da-z\.-]+)\.(?:[a-z]{2,6})|'
r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2['
r'0-4][0-9]|[01]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})|'
r'::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(2[0-4]|1{0,1}'
r'[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0'
r',1}[0-9])|(?:(?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4})|(?:'
r'[0-9a-fA-F]{1,4}:){1,7}(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|(?::'
r':(?:[0-9a-fA-F]{1,4}:){,6}(?:[0-9a-fA-F]{1,4})))')
def check_url(ip_address: str) -> str:
"""Function to check if valid url/ip was supplied
First regex check for IPv6.
If nothing found, second regex try to find IPv4 and domains names.
Args:
ip_address: String with ip address set by user.
Returns:
Ip address found by regex.
"""
if not ip_address:
return
valid = False
matches = re.findall(IPV6_REGEX, ip_address)
if matches:
largest = max(matches, key=len)[0]
if ':' in largest:
try:
checked_ip = ipaddress.ip_address(largest)
if checked_ip:
valid = True
except ValueError:
return None
if largest and valid:
return largest
matches = re.search(IP_REGEX, ip_address)
if matches:
return matches.group(1)
return None
# pylint: disable=R0912, W0105, W0511
class HomeAssistantClient:
"""Home Assistant client class"""
def __init__(self, config):
self.ssl = config['ssl'] or False
self.verify = config['verify'] or True
ip_address = config['ip_address']
token = config['token']
port_number = config['port_number']
if self.ssl:
self.url = f"https://{ip_address}"
else:
self.url = f"http://{ip_address}"
if port_number:
self.url = f"{self.url}:{port_number}"
self.headers = {
'Authorization': f"Bearer {token}",
'Content-Type': 'application/json'
}
def _get_state(self) -> json:
"""Get state object
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Returns:
Json containing response from HA.
"""
if self.ssl:
req = get(f"{self.url}/api/states", headers=self.headers,
verify=self.verify, timeout=TIMEOUT)
else:
req = get(f"{self.url}/api/states", headers=self.headers,
timeout=TIMEOUT)
req.raise_for_status()
return req.json()
def connected(self) -> bool:
"""Simple connection test to HA instance
Returns:
Return false if any of errors occur
"""
try:
self._get_state()
return True
except (Timeout, ConnectionError, RequestException):
return False
def find_entity(self, entity: str, types: list) -> dict:
"""Find entity with specified name, fuzzy matching
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Returns:
Dict represeting entity
"""
json_data = self._get_state()
# require a score above 50%
best_score = 50
best_entity = None
if json_data:
for state in json_data:
try:
if state['entity_id'].split(".")[0] in types:
# something like temperature outside
# should score on "outside temperature sensor"
# and repetitions should not count on my behalf
score = fuzz.token_sort_ratio(
entity,
state['attributes']['friendly_name'].lower())
if score > best_score:
best_score = score
best_entity = {
"id": state['entity_id'],
"dev_name": state['attributes']
['friendly_name'],
"state": state['state'],
"best_score": best_score,
"attributes": state['attributes']}
score = fuzz.token_sort_ratio(
entity,
state['entity_id'].lower())
if score > best_score:
best_score = score
best_entity = {
"id": state['entity_id'],
"dev_name": state['attributes']
['friendly_name'],
"state": state['state'],
"best_score": best_score,
"attributes": state['attributes']}
except KeyError:
pass
return best_entity
def find_entity_attr(self, entity: str) -> dict:
"""Get the entity attributes to be used in the response dialog.
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Returns:
Dict with entity's attributes
"""
json_data = self._get_state()
if json_data:
for attr in json_data:
if attr['entity_id'] == entity:
entity_attrs = attr['attributes']
try:
if attr['entity_id'].startswith('light.'):
# Not all lamps do have a color
unit_measur = entity_attrs['brightness']
else:
unit_measur = entity_attrs['unit_of_measurement']
except KeyError:
unit_measur = ""
# IDEA: return the color if available
# TODO: change to return the whole attr dictionary =>
# free use within handle methods
sensor_name = entity_attrs['friendly_name']
sensor_state = attr['state']
entity_attr = {
"unit_measure": unit_measur,
"name": sensor_name,
"state": sensor_state
}
return entity_attr
return None
def list_entities(self, types: list) -> list:
"""List all entities matching domains used within our skill
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Returns:
List with entity and it's friendly name
"""
json_data = self._get_state()
entities = []
if json_data:
for state in json_data:
try:
entity_id = state['entity_id'].split(".")
domain = entity_id[0]
entity = entity_id[1]
if domain in types:
"""Domain of Entity is in handled types.
Add Entity and its friendly name to list.
"""
entities.append(entity)
entities.append(state['attributes']
['friendly_name'].lower())
except KeyError:
pass
return entities
def execute_service(self, domain: str, service: str, data: dict) -> Response:
"""Execute service at HAServer
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Returns:
HA response
"""
if self.ssl:
req = post(f"{self.url}/api/services/{domain}/{service}",
headers=self.headers, data=json.dumps(data),
verify=self.verify, timeout=TIMEOUT)
else:
req = post(f"{self.url}/api/services/{domain}/{service}",
headers=self.headers, data=json.dumps(data),
timeout=TIMEOUT)
req.raise_for_status()
return req
def find_component(self, component: str) -> bool:
"""Check if a component is loaded at the HA-Server
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Returns:
True/False if component found in response
"""
if self.ssl:
req = get(f"{self.url}/api/components",
headers=self.headers, verify=self.verify,
timeout=TIMEOUT)
else:
req = get(f"{self.url}/api/components", headers=self.headers,
timeout=TIMEOUT)
req.raise_for_status()
return component in req.json()
def engage_conversation(self, utterance: str) -> dict:
"""Engage the conversation component at the Home Assistant server
Throws request Exceptions
(Subclasses of ConnectionError or RequestException,
raises HTTPErrors if non-Ok status code)
Attributes:
utterance raw text message to be processed
Returns:
Dict answer by Home Assistant server
{ 'speech': textual answer,
'extra_data': ...}
"""
data = {
"text": utterance
}
if self.ssl:
req = post(f"{self.url}/api/conversation/process",
headers=self.headers,
data=json.dumps(data),
verify=self.verify,
timeout=TIMEOUT
)
else:
req = post(f"{self.url}/api/conversation/process",
headers=self.headers,
data=json.dumps(data),
timeout=TIMEOUT
)
req.raise_for_status()
return req.json()['speech']['plain']