livisi 0.0.23__py3-none-any.whl → 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
livisi/__init__.py CHANGED
@@ -1,4 +1,93 @@
1
- from .aiolivisi import AioLivisi
2
- from .websocket import Websocket
3
- from .errors import IncorrectIpAddressException, WrongCredentialException, ShcUnreachableException, LivisiException
4
- from .livisi_event import LivisiEvent
1
+ # __init__.py
2
+
3
+ # Import key classes, constants, and exceptions
4
+
5
+ # livisi_connector.py
6
+ from .livisi_connector import LivisiConnection, connect
7
+
8
+ # livisi_controller.py
9
+ from .livisi_controller import LivisiController
10
+
11
+ # livisi_device.py
12
+ from .livisi_device import LivisiDevice
13
+
14
+ # livisi_websocket.py
15
+ from .livisi_websocket import LivisiWebsocket
16
+
17
+ # livisi_websocket_event.py
18
+ from .livisi_websocket_event import LivisiWebsocketEvent
19
+
20
+ # livisi_const.py
21
+ from .livisi_const import (
22
+ LOGGER,
23
+ V2_NAME,
24
+ V1_NAME,
25
+ V2_WEBSOCKET_PORT,
26
+ CLASSIC_WEBSOCKET_PORT,
27
+ WEBSERVICE_PORT,
28
+ REQUEST_TIMEOUT,
29
+ CONTROLLER_DEVICE_TYPES,
30
+ BATTERY_LOW,
31
+ UPDATE_AVAILABLE,
32
+ LIVISI_EVENT_STATE_CHANGED,
33
+ LIVISI_EVENT_BUTTON_PRESSED,
34
+ LIVISI_EVENT_MOTION_DETECTED,
35
+ IS_REACHABLE,
36
+ EVENT_BUTTON_PRESSED,
37
+ EVENT_BUTTON_LONG_PRESSED,
38
+ EVENT_MOTION_DETECTED,
39
+ COMMAND_RESTART,
40
+ )
41
+
42
+ # livisi_errors.py
43
+ from .livisi_errors import (
44
+ LivisiException,
45
+ ShcUnreachableException,
46
+ WrongCredentialException,
47
+ IncorrectIpAddressException,
48
+ TokenExpiredException,
49
+ ErrorCodeException,
50
+ ERROR_CODES,
51
+ )
52
+
53
+ # Define __all__ to specify what is exported when using 'from livisi import *'
54
+ __all__ = [
55
+ # From livisi_connector.py
56
+ "LivisiConnection",
57
+ "connect",
58
+ # From livisi_controller.py
59
+ "LivisiController",
60
+ # From livisi_device.py
61
+ "LivisiDevice",
62
+ # From livisi_websocket.py
63
+ "LivisiWebsocket",
64
+ # From livisi_websocket_event.py
65
+ "LivisiWebsocketEvent",
66
+ # From livisi_const.py
67
+ "LOGGER",
68
+ "V2_NAME",
69
+ "V1_NAME",
70
+ "V2_WEBSOCKET_PORT",
71
+ "CLASSIC_WEBSOCKET_PORT",
72
+ "WEBSERVICE_PORT",
73
+ "REQUEST_TIMEOUT",
74
+ "CONTROLLER_DEVICE_TYPES",
75
+ "BATTERY_LOW",
76
+ "UPDATE_AVAILABLE",
77
+ "LIVISI_EVENT_STATE_CHANGED",
78
+ "LIVISI_EVENT_BUTTON_PRESSED",
79
+ "LIVISI_EVENT_MOTION_DETECTED",
80
+ "IS_REACHABLE",
81
+ "EVENT_BUTTON_PRESSED",
82
+ "EVENT_BUTTON_LONG_PRESSED",
83
+ "EVENT_MOTION_DETECTED",
84
+ "COMMAND_RESTART",
85
+ # From livisi_errors.py
86
+ "LivisiException",
87
+ "ShcUnreachableException",
88
+ "WrongCredentialException",
89
+ "IncorrectIpAddressException",
90
+ "TokenExpiredException",
91
+ "ErrorCodeException",
92
+ "ERROR_CODES",
93
+ ]
@@ -0,0 +1,508 @@
1
+ """Code to handle the communication with Livisi Smart home controllers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from contextlib import suppress
7
+
8
+ from typing import Any
9
+ import uuid
10
+ import json
11
+ from aiohttp import ClientResponseError, ServerDisconnectedError, ClientConnectorError
12
+ from aiohttp.client import ClientSession, ClientError, TCPConnector
13
+ from dateutil.parser import parse as parse_timestamp
14
+
15
+ from .livisi_device import LivisiDevice
16
+
17
+ from .livisi_json_util import parse_dataclass
18
+ from .livisi_controller import LivisiController
19
+
20
+ from .livisi_errors import (
21
+ ERROR_CODES,
22
+ IncorrectIpAddressException,
23
+ LivisiException,
24
+ ShcUnreachableException,
25
+ WrongCredentialException,
26
+ ErrorCodeException,
27
+ )
28
+
29
+ from .livisi_websocket import LivisiWebsocket
30
+
31
+ from .livisi_const import (
32
+ COMMAND_RESTART,
33
+ CONTROLLER_DEVICE_TYPES,
34
+ V1_NAME,
35
+ V2_NAME,
36
+ LOGGER,
37
+ REQUEST_TIMEOUT,
38
+ WEBSERVICE_PORT,
39
+ )
40
+
41
+
42
+ async def connect(host: str, password: str) -> LivisiConnection:
43
+ """Initialize the lib and connect to the livisi SHC."""
44
+ connection = LivisiConnection()
45
+ await connection.connect(host, password)
46
+ return connection
47
+
48
+
49
+ class LivisiConnection:
50
+ """Handles the communication with the Livisi Smart Home controller."""
51
+
52
+ def __init__(self) -> None:
53
+ """Initialize the livisi connector."""
54
+
55
+ self.host: str = None
56
+ self.controller: LivisiController = None
57
+
58
+ self._password: str = None
59
+ self._token: str = None
60
+
61
+ self._web_session = None
62
+ self._websocket = LivisiWebsocket(self)
63
+
64
+ async def connect(self, host: str, password: str):
65
+ """Connect to the livisi SHC and retrieve controller information."""
66
+ if self._web_session is not None:
67
+ await self.close()
68
+ self._web_session = self._create_web_session(concurrent_connections=1)
69
+ if host is not None and password is not None:
70
+ self.host = host
71
+ self._password = password
72
+ try:
73
+ await self._async_retrieve_token()
74
+ except:
75
+ await self.close()
76
+ raise
77
+
78
+ self.controller = await self._async_get_controller()
79
+ if self.controller.is_v2:
80
+ # reconnect with more concurrent connections on v2 SHC
81
+ await self._web_session.close()
82
+ self._web_session = self._create_web_session(concurrent_connections=10)
83
+
84
+ async def close(self):
85
+ """Disconnect the http client session and websocket."""
86
+ if self._web_session is not None:
87
+ await self._web_session.close()
88
+ self._web_session = None
89
+ self.controller = None
90
+ await self._websocket.disconnect()
91
+
92
+ async def listen_for_events(self, on_data, on_close) -> None:
93
+ """Connect to the websocket."""
94
+ if self._web_session is None:
95
+ raise LivisiException("Not authenticated to SHC")
96
+ if self._websocket.is_connected():
97
+ with suppress(Exception):
98
+ await self._websocket.disconnect()
99
+ await self._websocket.connect(on_data, on_close)
100
+
101
+ async def async_send_authorized_request(
102
+ self,
103
+ method,
104
+ path: str,
105
+ payload=None,
106
+ ) -> dict:
107
+ """Make a request to the Livisi Smart Home controller."""
108
+ url = f"http://{self.host}:{WEBSERVICE_PORT}/{path}"
109
+ auth_headers = {
110
+ "authorization": f"Bearer {self.token}",
111
+ "Content-type": "application/json",
112
+ "Accept": "*/*",
113
+ }
114
+ return await self._async_request(method, url, payload, auth_headers)
115
+
116
+ def _create_web_session(self, concurrent_connections: int = 1):
117
+ """Create a custom web session which limits concurrent connections."""
118
+ connector = TCPConnector(
119
+ limit=concurrent_connections,
120
+ limit_per_host=concurrent_connections,
121
+ force_close=True,
122
+ )
123
+ web_session = ClientSession(connector=connector)
124
+ return web_session
125
+
126
+ async def _async_retrieve_token(self) -> None:
127
+ """Set the JWT from the LIVISI Smart Home Controller."""
128
+ access_data: dict = {}
129
+
130
+ if self._password is None:
131
+ raise LivisiException("No password set")
132
+
133
+ login_credentials = {
134
+ "username": "admin",
135
+ "password": self._password,
136
+ "grant_type": "password",
137
+ }
138
+ headers = {
139
+ "Authorization": "Basic Y2xpZW50SWQ6Y2xpZW50UGFzcw==",
140
+ "Content-type": "application/json",
141
+ "Accept": "application/json",
142
+ }
143
+
144
+ try:
145
+ LOGGER.debug("Updating access token")
146
+ access_data = await self._async_send_request(
147
+ "post",
148
+ url=f"http://{self.host}:{WEBSERVICE_PORT}/auth/token",
149
+ payload=login_credentials,
150
+ headers=headers,
151
+ )
152
+ LOGGER.debug("Updated access token")
153
+ self.token = access_data.get("access_token")
154
+ if self.token is None:
155
+ errorcode = access_data.get("errorcode")
156
+ errordesc = access_data.get("description", "Unknown Error")
157
+ if errorcode in (2003, 2009):
158
+ raise WrongCredentialException
159
+ # log full response for debugging
160
+ LOGGER.error("SHC response does not contain access token")
161
+ LOGGER.error(access_data)
162
+ raise LivisiException(f"No token received from SHC: {errordesc}")
163
+ except ClientError as error:
164
+ if len(access_data) == 0:
165
+ raise IncorrectIpAddressException from error
166
+ raise ShcUnreachableException from error
167
+
168
+ async def _async_request(
169
+ self, method, url: str, payload=None, headers=None
170
+ ) -> dict:
171
+ """Send a request to the Livisi Smart Home controller and handle requesting new token."""
172
+ response = await self._async_send_request(method, url, payload, headers)
173
+
174
+ if response is not None and "errorcode" in response:
175
+ errorcode = response.get("errorcode")
176
+ # reconnect on expired token
177
+ if errorcode == 2007:
178
+ await self._async_retrieve_token()
179
+ response = await self._async_send_request(method, url, payload, headers)
180
+ if response is not None and "errorcode" in response:
181
+ LOGGER.error(
182
+ "Livisi sent error code %d after token request",
183
+ response.get("errorcode"),
184
+ )
185
+ raise ErrorCodeException(response["errorcode"])
186
+ else:
187
+ LOGGER.error(
188
+ "Error code %d (%s) on url %s",
189
+ errorcode,
190
+ ERROR_CODES.get(errorcode, "unknown"),
191
+ url,
192
+ )
193
+ raise ErrorCodeException(response["errorcode"])
194
+
195
+ return response
196
+
197
+ async def _async_send_request(
198
+ self, method, url: str, payload=None, headers=None
199
+ ) -> dict:
200
+ try:
201
+ if payload is not None:
202
+ data = json.dumps(payload).encode("utf-8")
203
+ if headers is None:
204
+ headers = {}
205
+ headers["Content-Type"] = "application/json"
206
+ headers["Content-Encoding"] = "utf-8"
207
+ else:
208
+ data = None
209
+
210
+ async with self._web_session.request(
211
+ method,
212
+ url,
213
+ json=payload,
214
+ headers=headers,
215
+ ssl=False,
216
+ timeout=REQUEST_TIMEOUT,
217
+ ) as res:
218
+ try:
219
+ data = await res.json()
220
+ if data is None and res.status != 200:
221
+ raise LivisiException(
222
+ f"No data received from SHC, response code {res.status} ({res.reason})"
223
+ )
224
+ except ClientResponseError as exc:
225
+ raise LivisiException(
226
+ f"Invalid response from SHC, response code {res.status} ({res.reason})"
227
+ ) from exc
228
+ return data
229
+ except TimeoutError as exc:
230
+ raise ShcUnreachableException("Timeout waiting for shc") from exc
231
+ except ClientConnectorError as exc:
232
+ raise ShcUnreachableException("Failed to connect to shc") from exc
233
+
234
+ async def _async_get_controller(self) -> LivisiController:
235
+ """Get Livisi Smart Home controller data."""
236
+ shc_info = await self.async_send_authorized_request("get", path="status")
237
+ controller = parse_dataclass(shc_info, LivisiController)
238
+ controller.is_v2 = shc_info.get("controllerType") == V2_NAME
239
+ controller.is_v1 = shc_info.get("controllerType") == V1_NAME
240
+ return controller
241
+
242
+ async def async_get_devices(
243
+ self,
244
+ ) -> list[LivisiDevice]:
245
+ """Send requests for getting all required data."""
246
+
247
+ # retrieve messages first, this will also refresh the token if
248
+ # needed so subsequent parallel requests don't fail
249
+ messages = await self.async_send_authorized_request("get", path="message")
250
+
251
+ (
252
+ low_battery_devices,
253
+ update_available_devices,
254
+ unreachable_devices,
255
+ updated_devices,
256
+ ) = self.parse_messages(messages)
257
+
258
+ devices, capabilities, rooms = await asyncio.gather(
259
+ self.async_send_authorized_request("get", path="device"),
260
+ self.async_send_authorized_request("get", path="capability"),
261
+ self.async_send_authorized_request("get", path="location"),
262
+ return_exceptions=True,
263
+ )
264
+
265
+ for result, path in zip(
266
+ (devices, capabilities, rooms),
267
+ ("device", "capability", "location"),
268
+ ):
269
+ if isinstance(result, Exception):
270
+ LOGGER.warn(f"Error loading {path}")
271
+ raise result # Re-raise the exception immediately
272
+
273
+ controller_id = next(
274
+ (x.get("id") for x in devices if x.get("type") in CONTROLLER_DEVICE_TYPES),
275
+ None,
276
+ )
277
+ if controller_id is not None:
278
+ try:
279
+ shc_state = await self.async_send_authorized_request(
280
+ "get", path=f"device/{controller_id}/state"
281
+ )
282
+ if self.controller.is_v1:
283
+ shc_state = shc_state["state"]
284
+ except Exception:
285
+ LOGGER.warning("Error getting shc state", exc_info=True)
286
+
287
+ capability_map = {}
288
+ capability_config = {}
289
+
290
+ room_map = {}
291
+
292
+ for room in rooms:
293
+ if "id" in room:
294
+ roomid = room["id"]
295
+ room_map[roomid] = room.get("config", {}).get("name")
296
+
297
+ for capability in capabilities:
298
+ if "device" in capability:
299
+ device_id = capability["device"].removeprefix("/device/")
300
+
301
+ if device_id not in capability_map:
302
+ capability_map[device_id] = {}
303
+ capability_config[device_id] = {}
304
+
305
+ cap_type = capability.get("type")
306
+ if cap_type is not None:
307
+ capability_map[device_id][cap_type] = capability["id"]
308
+ if "config" in capability:
309
+ capability_config[device_id][cap_type] = capability["config"]
310
+
311
+ devicelist = []
312
+
313
+ for device in devices:
314
+ device_id = device.get("id")
315
+ device["capabilities"] = capability_map.get(device_id, {})
316
+ device["capability_config"] = capability_config.get(device_id, {})
317
+ device["cls"] = device.get("class")
318
+ device["battery_low"] = device_id in low_battery_devices
319
+ device["update_available"] = device_id in update_available_devices
320
+ device["updated"] = device_id in updated_devices
321
+ device["unreachable"] = device_id in unreachable_devices
322
+ if device.get("location") is not None:
323
+ roomid = device["location"].removeprefix("/location/")
324
+ device["room"] = room_map.get(roomid)
325
+
326
+ if device["type"] in CONTROLLER_DEVICE_TYPES:
327
+ device["state"] = shc_state
328
+
329
+ devicelist.append(parse_dataclass(device, LivisiDevice))
330
+
331
+ LOGGER.debug("Loaded %d devices", len(devices))
332
+
333
+ return devicelist
334
+
335
+ def parse_messages(self, messages):
336
+ """Parse message data from shc."""
337
+ low_battery_devices = set()
338
+ update_available_devices = set()
339
+ unreachable_devices = set()
340
+ updated_devices = set()
341
+
342
+ for message in messages:
343
+ if isinstance(message, str):
344
+ LOGGER.warning("Invalid message")
345
+ LOGGER.warning(messages)
346
+ continue
347
+
348
+ msgtype = message.get("type", "")
349
+ msgtimestamp = parse_timestamp(message.get("timestamp", ""))
350
+ if msgtimestamp is None:
351
+ continue
352
+
353
+ device_ids = [
354
+ d.removeprefix("/device/") for d in message.get("devices", [])
355
+ ]
356
+ if len(device_ids) == 0:
357
+ source = message.get("source", "")
358
+ device_ids = [source.replace("/device/", "")]
359
+ if msgtype == "DeviceLowBattery":
360
+ for device_id in device_ids:
361
+ low_battery_devices.add(device_id)
362
+ elif msgtype == "DeviceUpdateAvailable":
363
+ for device_id in device_ids:
364
+ update_available_devices.add(device_id)
365
+ elif msgtype == "ProductUpdated" or msgtype == "ShcUpdateCompleted":
366
+ for device_id in device_ids:
367
+ updated_devices.add(device_id)
368
+ elif msgtype == "DeviceUnreachable":
369
+ for device_id in device_ids:
370
+ unreachable_devices.add(device_id)
371
+ return (
372
+ low_battery_devices,
373
+ update_available_devices,
374
+ unreachable_devices,
375
+ updated_devices,
376
+ )
377
+
378
+ async def async_get_value(
379
+ self, capability: str, property: str, key: str = "value"
380
+ ) -> Any | None:
381
+ """Get current value of the capability."""
382
+ state = await self.async_get_state(capability, property)
383
+ if state is None:
384
+ return None
385
+ return state.get(key, None)
386
+
387
+ async def async_get_state(self, capability: str, property: str) -> dict | None:
388
+ """Get state of a capability."""
389
+
390
+ if capability is None:
391
+ return None
392
+
393
+ requestUrl = f"capability/{capability}/state"
394
+ try:
395
+ response = await self.async_send_authorized_request("get", requestUrl)
396
+ if response is None:
397
+ return None
398
+ if not isinstance(response, dict):
399
+ return None
400
+ return response.get(property, None)
401
+ except Exception:
402
+ LOGGER.warning(
403
+ f"Error getting device state (url: {requestUrl})", exc_info=True
404
+ )
405
+ return None
406
+
407
+ async def async_set_state(
408
+ self,
409
+ capability_id: str,
410
+ *,
411
+ key: str = None,
412
+ value: bool | float = None,
413
+ namespace: str = "core.RWE",
414
+ ) -> bool:
415
+ """Set the state of a capability."""
416
+ params = {}
417
+ if key is not None:
418
+ params = {key: {"type": "Constant", "value": value}}
419
+
420
+ return await self.async_send_capability_command(
421
+ capability_id, "SetState", namespace=namespace, params=params
422
+ )
423
+
424
+ async def _async_send_command(
425
+ self,
426
+ target: str,
427
+ command_type: str,
428
+ *,
429
+ namespace: str = "core.RWE",
430
+ params: dict = None,
431
+ ) -> bool:
432
+ """Send a command to a target."""
433
+
434
+ if params is None:
435
+ params = {}
436
+
437
+ set_state_payload: dict[str, Any] = {
438
+ "id": uuid.uuid4().hex,
439
+ "type": command_type,
440
+ "namespace": namespace,
441
+ "target": target,
442
+ "params": params,
443
+ }
444
+ try:
445
+ response = await self.async_send_authorized_request(
446
+ "post", "action", payload=set_state_payload
447
+ )
448
+ if response is None:
449
+ return False
450
+ return response.get("resultCode") == "Success"
451
+ except ServerDisconnectedError:
452
+ # Funny thing: The SHC restarts immediatly upon processing the restart command, it doesn't even answer to the request
453
+ # In order to not throw an error we need to catch and assume the request was successfull.
454
+ if command_type == COMMAND_RESTART:
455
+ return True
456
+ raise
457
+
458
+ async def async_send_device_command(
459
+ self,
460
+ device_id: str,
461
+ command_type: str,
462
+ *,
463
+ namespace: str = "core.RWE",
464
+ params: dict = None,
465
+ ) -> bool:
466
+ """Send a command to a device."""
467
+
468
+ return await self._async_send_command(
469
+ target=f"/device/{device_id}",
470
+ command_type=command_type,
471
+ namespace=namespace,
472
+ params=params,
473
+ )
474
+
475
+ async def async_send_capability_command(
476
+ self,
477
+ capability_id: str,
478
+ command_type: str,
479
+ *,
480
+ namespace: str = "core.RWE",
481
+ params: dict = None,
482
+ ) -> bool:
483
+ """Send a command to a capability."""
484
+
485
+ return await self._async_send_command(
486
+ target=f"/capability/{capability_id}",
487
+ command_type=command_type,
488
+ namespace=namespace,
489
+ params=params,
490
+ )
491
+
492
+ @property
493
+ def livisi_connection_data(self):
494
+ """Return the connection data."""
495
+ return self._livisi_connection_data
496
+
497
+ @livisi_connection_data.setter
498
+ def livisi_connection_data(self, new_value):
499
+ self._livisi_connection_data = new_value
500
+
501
+ @property
502
+ def token(self):
503
+ """Return the token."""
504
+ return self._token
505
+
506
+ @token.setter
507
+ def token(self, new_value):
508
+ self._token = new_value
livisi/livisi_const.py ADDED
@@ -0,0 +1,30 @@
1
+ """Constants for the Livisi Smart Home integration."""
2
+
3
+ import logging
4
+ from typing import Final
5
+
6
+ LOGGER = logging.getLogger(__package__)
7
+
8
+ V2_NAME = "Avatar"
9
+ V1_NAME = "Classic"
10
+ V2_WEBSOCKET_PORT: Final = 9090
11
+ CLASSIC_WEBSOCKET_PORT: Final = 8080
12
+ WEBSERVICE_PORT: Final = 8080
13
+ REQUEST_TIMEOUT: Final = 2000
14
+
15
+ CONTROLLER_DEVICE_TYPES: Final = ["SHC", "SHCA"]
16
+
17
+ BATTERY_LOW: Final = "batteryLow"
18
+ UPDATE_AVAILABLE: Final = "DeviceUpdateAvailable"
19
+
20
+ LIVISI_EVENT_STATE_CHANGED = "StateChanged"
21
+ LIVISI_EVENT_BUTTON_PRESSED = "ButtonPressed"
22
+ LIVISI_EVENT_MOTION_DETECTED = "MotionDetected"
23
+
24
+ IS_REACHABLE: Final = "isReachable"
25
+
26
+ EVENT_BUTTON_PRESSED = "button_pressed"
27
+ EVENT_BUTTON_LONG_PRESSED = "button_long_pressed"
28
+ EVENT_MOTION_DETECTED = "motion_detected"
29
+
30
+ COMMAND_RESTART = "Restart"
@@ -0,0 +1,17 @@
1
+ """Code to represent a livisi device."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+
8
+ @dataclass
9
+ class LivisiController:
10
+ """Stores the livisi controller data."""
11
+
12
+ controller_type: str
13
+ serial_number: str
14
+ os_version: str
15
+
16
+ is_v2: bool
17
+ is_v1: bool