lghorizon 0.9.0.dev2__py3-none-any.whl → 0.9.0.dev4__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.
@@ -1,123 +0,0 @@
1
- import asyncio
2
- import json
3
- import logging
4
-
5
- import paho.mqtt.client as mqtt
6
- from typing import Any, Callable, Coroutine
7
- from .helpers import make_id
8
- from .lghorizon_models import LGHorizonAuth
9
-
10
- _logger = logging.getLogger(__name__)
11
-
12
-
13
- class LGHorizonMqttClient:
14
- """LGHorizon MQTT client."""
15
-
16
- _mqtt_broker_url: str = ""
17
- _mqtt_client: mqtt.Client
18
- _auth: LGHorizonAuth
19
- _mqtt_token: str = ""
20
- client_id: str = ""
21
- _on_connected_callback: Callable[[], Coroutine[Any, Any, Any]]
22
- _on_message_callback: Callable[[dict, str], Coroutine[Any, Any, Any]]
23
-
24
- @property
25
- def is_connected(self):
26
- """Is client connected."""
27
- return self._mqtt_client.is_connected
28
-
29
- def __init__(
30
- self,
31
- auth: LGHorizonAuth,
32
- on_connected_callback: Callable[[], Coroutine[Any, Any, Any]],
33
- on_message_callback: Callable[[dict, str], Coroutine[Any, Any, Any]],
34
- ):
35
- """Initialize the MQTT client."""
36
- self._auth = auth
37
- self._on_connected_callback = on_connected_callback
38
- self._on_message_callback = on_message_callback
39
- self._loop = asyncio.get_event_loop()
40
-
41
- @classmethod
42
- async def create(
43
- cls,
44
- auth: LGHorizonAuth,
45
- on_connected_callback: Callable[[], Coroutine[Any, Any, Any]],
46
- on_message_callback: Callable[[dict, str], Coroutine[Any, Any, Any]],
47
- ):
48
- """Create the MQTT client."""
49
- instance = cls(auth, on_connected_callback, on_message_callback)
50
- service_config = await auth.get_service_config()
51
- mqtt_broker_url = await service_config.get_service_url("mqttBroker")
52
- instance._mqtt_broker_url = mqtt_broker_url.replace("wss://", "").replace(
53
- ":443/mqtt", ""
54
- )
55
- instance.client_id = await make_id()
56
- instance._mqtt_client = mqtt.Client(
57
- client_id=instance.client_id,
58
- transport="websockets",
59
- )
60
-
61
- instance._mqtt_client.ws_set_options(
62
- headers={"Sec-WebSocket-Protocol": "mqtt, mqttv3.1, mqttv3.11"}
63
- )
64
- instance._mqtt_token = await auth.get_mqtt_token()
65
- instance._mqtt_client.username_pw_set(auth.household_id, instance._mqtt_token)
66
- instance._mqtt_client.tls_set()
67
- instance._mqtt_client.enable_logger(_logger)
68
- instance._mqtt_client.on_connect = instance._on_connect
69
- instance._on_connected_callback = on_connected_callback
70
- instance._on_message_callback = on_message_callback
71
- return instance
72
-
73
- def _on_connect(self, client, userdata, flags, result_code): # pylint: disable=unused-argument
74
- if result_code == 0:
75
- self._mqtt_client.on_message = self._on_message
76
- if self._on_connected_callback:
77
- asyncio.run_coroutine_threadsafe(
78
- self._on_connected_callback(), self._loop
79
- )
80
- elif result_code == 5:
81
- self._mqtt_client.username_pw_set(self._auth.household_id, self._mqtt_token)
82
- asyncio.run_coroutine_threadsafe(self.connect(), self._loop)
83
- else:
84
- _logger.error(
85
- "Cannot connect to MQTT server with resultCode: %s", result_code
86
- )
87
-
88
- def _on_message(self, client, userdata, message): # pylint: disable=unused-argument
89
- """Wrapper for handling MQTT messages in a thread-safe manner."""
90
- asyncio.run_coroutine_threadsafe(
91
- self._on_client_message(client, userdata, message), self._loop
92
- )
93
-
94
- async def connect(self) -> None:
95
- """Connect the client."""
96
- self._mqtt_client.connect(self._mqtt_broker_url, 443)
97
- self._mqtt_client.loop_start()
98
-
99
- async def subscribe(self, topic: str) -> None:
100
- """Subscribe to a MQTT topic."""
101
- self._mqtt_client.subscribe(topic)
102
-
103
- async def publish_message(self, topic: str, json_payload: str) -> None:
104
- """Publish a MQTT message."""
105
- self._mqtt_client.publish(topic, json_payload, qos=2)
106
-
107
- async def disconnect(self) -> None:
108
- """Disconnect the client."""
109
- if self._mqtt_client.is_connected():
110
- self._mqtt_client.disconnect()
111
-
112
- async def _on_client_message(self, client, userdata, message): # pylint: disable=unused-argument
113
- """Handle messages received by mqtt client."""
114
- json_payload = await self._loop.run_in_executor(
115
- None, json.loads, message.payload
116
- )
117
- _logger.debug(
118
- "Received MQTT message \n\ntopic: %s\npayload:\n\n%s\n",
119
- message.topic,
120
- json.dumps(json_payload, indent=2),
121
- )
122
- if self._on_message_callback:
123
- await self._on_message_callback(json_payload, message.topic)
@@ -1,41 +0,0 @@
1
- from .lghorizon_models import (
2
- LGHorizonRecordingList,
3
- LGHorizonRecordingSingle,
4
- LGHorizonRecordingSeason,
5
- LGHorizonRecordingShow,
6
- LGHorizonRecordingType,
7
- )
8
-
9
-
10
- class LGHorizonRecordingFactory:
11
- """Factory to create LGHorizonRecording objects."""
12
-
13
- async def create_recordings(self, recording_json: dict) -> LGHorizonRecordingList:
14
- """Create a LGHorizonRecording object based on the recording type."""
15
- recording_list = []
16
- for recording in recording_json["data"]:
17
- recording_type = LGHorizonRecordingType[
18
- recording.get("type", "unknown").upper()
19
- ]
20
- match recording_type:
21
- case LGHorizonRecordingType.SINGLE:
22
- recording_single = LGHorizonRecordingSingle(recording)
23
- recording_list.append(recording_single)
24
- case LGHorizonRecordingType.SEASON:
25
- recording_season = LGHorizonRecordingSeason(recording)
26
- recording_list.append(recording_season)
27
- case LGHorizonRecordingType.SHOW:
28
- recording_show = LGHorizonRecordingShow(recording)
29
- recording_list.append(recording_show)
30
- case LGHorizonRecordingType.UNKNOWN:
31
- pass
32
-
33
- return LGHorizonRecordingList(recording_list)
34
-
35
- async def create_episodes(self, episode_json: dict) -> LGHorizonRecordingList:
36
- """Create a LGHorizonRecording list based for episodes."""
37
- recording_list = []
38
- for recording in episode_json["data"]:
39
- recording_single = LGHorizonRecordingSingle(recording)
40
- recording_list.append(recording_single)
41
- return LGHorizonRecordingList(recording_list)
@@ -1,17 +0,0 @@
1
- lghorizon/__init__.py,sha256=tufkUK4FW-xJ0PsBgrJF-22-wJaPcpdEnW7iQ7LNh60,42
2
- lghorizon/const.py,sha256=HINlbyevEN9ZRnfIBbSGNc6i9J8WkIgpqkLZrwyqpGQ,5307
3
- lghorizon/exceptions.py,sha256=-6v55KDTogBldGAg1wV9Mrxm5L5BsaVguhBgVMOeJHk,404
4
- lghorizon/helpers.py,sha256=SGlEN6V0kh2vqw1qCKmM1KhfeO-UvPyyQmnThgFLFhs,272
5
- lghorizon/lghorizon_api.py,sha256=4K0aAklA61eXcZWPUgKe5LkNEu24QbMtVbThbAjaxR4,11767
6
- lghorizon/lghorizon_device.py,sha256=jaxEjTjIx1zI9rBsv9gUwScRsVVNZrJMeM4ygxllBIo,12198
7
- lghorizon/lghorizon_device_state_processor.py,sha256=R-aLtmS50nqJ3CZ7R5Qdg9w4lvZ3xIWiMskh4ls2sTA,12062
8
- lghorizon/lghorizon_message_factory.py,sha256=1ZqgoGCERI-fhFh9ralemeHjPcpaPJS2AAkoGIbp9YI,1496
9
- lghorizon/lghorizon_models.py,sha256=bncfG_gfJaG604xceg-svg8_EUDsplRXSSknqgOqh6k,41141
10
- lghorizon/lghorizon_mqtt_client.py,sha256=bUWhTM8BJ1V5CbgP8ueJrm-pmopxBB7v_hLHKSwLxUU,4704
11
- lghorizon/lghorizon_recording_factory.py,sha256=qIVfnfIvXliUv-Gy1LJXYmE6rFg90WhOQn_3P4ji2T0,1755
12
- lghorizon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- lghorizon-0.9.0.dev2.dist-info/licenses/LICENSE,sha256=6Dh2tur1gMX3r3rITjVwUONBEJxyyPZDY8p6DZXtimE,1059
14
- lghorizon-0.9.0.dev2.dist-info/METADATA,sha256=p5JLL7vvwgLxfwixTfAc1G1ANwf7Rl3CNEujpaJJPUs,1287
15
- lghorizon-0.9.0.dev2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
16
- lghorizon-0.9.0.dev2.dist-info/top_level.txt,sha256=usii76_AxGfPI6gjrrh-NyZxcQQuF1B8_Q9kd7sID8Q,10
17
- lghorizon-0.9.0.dev2.dist-info/RECORD,,