idotmatrix-api-client 0.2.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.
Files changed (63) hide show
  1. idotmatrix/__init__.py +15 -0
  2. idotmatrix/apps/__init__.py +0 -0
  3. idotmatrix/apps/digital_picture_frame.py +541 -0
  4. idotmatrix/client.py +284 -0
  5. idotmatrix/connection_manager.py +702 -0
  6. idotmatrix/const.py +30 -0
  7. idotmatrix/fonts/16-ErbosDraco1StOpenNbpRegular-l5wX.ttf +0 -0
  8. idotmatrix/fonts/16-MinecraftStandard.otf +0 -0
  9. idotmatrix/fonts/16-MinecraftStandardBoldOblique.otf +0 -0
  10. idotmatrix/fonts/16-PixelOperator8-Bold.ttf +0 -0
  11. idotmatrix/fonts/16-PixelOperator8.ttf +0 -0
  12. idotmatrix/fonts/16-PixelifySans-Regular.ttf +0 -0
  13. idotmatrix/fonts/16-PressStart2P-Regular.ttf +0 -0
  14. idotmatrix/fonts/16-dogica.ttf +0 -0
  15. idotmatrix/fonts/16-dogicabold.ttf +0 -0
  16. idotmatrix/fonts/16-dogicapixel.ttf +0 -0
  17. idotmatrix/fonts/16-dogicapixelbold.ttf +0 -0
  18. idotmatrix/fonts/16_32-HomeVideo-BLG6G.ttf +0 -0
  19. idotmatrix/fonts/16_32-HomeVideoBold-R90Dv.ttf +0 -0
  20. idotmatrix/fonts/16_32-MayfairNbpBold-gAA4.ttf +0 -0
  21. idotmatrix/fonts/32-Jersey10-Regular.ttf +0 -0
  22. idotmatrix/fonts/CC-BY-SA-4.0-License.txt +428 -0
  23. idotmatrix/fonts/ErbosDraco-License.txt +3 -0
  24. idotmatrix/fonts/HomeVideo-License.txt +2 -0
  25. idotmatrix/fonts/Jersey10-Regular-License.txt +93 -0
  26. idotmatrix/fonts/MayfairNbpBold-License.txt +3 -0
  27. idotmatrix/fonts/MinecraftStandard-License.txt +5 -0
  28. idotmatrix/fonts/PixelOperator-License.txt +121 -0
  29. idotmatrix/fonts/PixelifySans-License.txt +93 -0
  30. idotmatrix/fonts/PressStart2P-Regular-License.txt +93 -0
  31. idotmatrix/fonts/Rain-DRM3-License.txt +44 -0
  32. idotmatrix/fonts/Rain-DRM3.otf +0 -0
  33. idotmatrix/fonts/dogica_license.txt +94 -0
  34. idotmatrix/modules/__init__.py +64 -0
  35. idotmatrix/modules/chronograph.py +50 -0
  36. idotmatrix/modules/clock.py +101 -0
  37. idotmatrix/modules/common.py +176 -0
  38. idotmatrix/modules/countdown.py +70 -0
  39. idotmatrix/modules/eco.py +63 -0
  40. idotmatrix/modules/effect.py +79 -0
  41. idotmatrix/modules/fullscreen_color.py +55 -0
  42. idotmatrix/modules/gif.py +403 -0
  43. idotmatrix/modules/graffiti.py +71 -0
  44. idotmatrix/modules/image.py +383 -0
  45. idotmatrix/modules/music_sync.py +67 -0
  46. idotmatrix/modules/scoreboard.py +38 -0
  47. idotmatrix/modules/system.py +65 -0
  48. idotmatrix/modules/text.py +232 -0
  49. idotmatrix/protocol.py +52 -0
  50. idotmatrix/screensize.py +8 -0
  51. idotmatrix/transport.py +137 -0
  52. idotmatrix/util/__init__.py +0 -0
  53. idotmatrix/util/color_utils.py +64 -0
  54. idotmatrix/util/command_ack.py +23 -0
  55. idotmatrix/util/device_info.py +38 -0
  56. idotmatrix/util/device_material.py +16 -0
  57. idotmatrix/util/file_watch.py +57 -0
  58. idotmatrix/util/image_utils.py +139 -0
  59. idotmatrix/util/upload_ack.py +42 -0
  60. idotmatrix_api_client-0.2.0.dist-info/METADATA +375 -0
  61. idotmatrix_api_client-0.2.0.dist-info/RECORD +63 -0
  62. idotmatrix_api_client-0.2.0.dist-info/WHEEL +4 -0
  63. idotmatrix_api_client-0.2.0.dist-info/licenses/LICENSE +674 -0
idotmatrix/client.py ADDED
@@ -0,0 +1,284 @@
1
+ from collections.abc import Callable
2
+ from functools import cached_property
3
+ from typing import Optional
4
+
5
+ from bleak.backends.device import BLEDevice
6
+
7
+ from idotmatrix.connection_manager import ConnectionManager, ConnectionListener
8
+ from idotmatrix.modules.chronograph import ChronographModule
9
+ from idotmatrix.modules.clock import ClockModule
10
+ from idotmatrix.modules.common import CommonModule
11
+ from idotmatrix.modules.countdown import CountdownModule
12
+ from idotmatrix.modules.eco import EcoModule
13
+ from idotmatrix.modules.effect import EffectModule
14
+ from idotmatrix.modules.fullscreen_color import FullscreenColorModule
15
+ from idotmatrix.modules.gif import GifModule
16
+ from idotmatrix.modules.graffiti import GraffitiModule
17
+ from idotmatrix.modules.image import ImageModule
18
+ from idotmatrix.modules.music_sync import MusicSyncModule
19
+ from idotmatrix.modules.scoreboard import ScoreboardModule
20
+ from idotmatrix.modules.system import SystemModule
21
+ from idotmatrix.modules.text import TextModule
22
+ from idotmatrix.screensize import ScreenSize
23
+ from idotmatrix.transport import Transport
24
+
25
+
26
+ class IDotMatrixClient:
27
+ """
28
+ Client for interacting with the iDotMatrix device.
29
+ """
30
+
31
+ def __init__(
32
+ self,
33
+ screen_size: Optional[ScreenSize] = None,
34
+ mac_address: Optional[str] = None,
35
+ max_ble_packet_size: Optional[int] = None,
36
+ ble_device: Optional[BLEDevice] = None,
37
+ transport_factory: Optional[Callable[[str | BLEDevice, Callable[[], None]], Transport]] = None,
38
+ install_signal_handlers: bool = True,
39
+ ):
40
+ """
41
+ Initializes the IDotMatrix client with the specified screen size and optional MAC address.
42
+
43
+ Args:
44
+ screen_size (Optional[ScreenSize]): The size of the screen, e.g., ScreenSize.SIZE_64x64. If not provided,
45
+ it is asked from the device when connecting (see `detect_screen_size`).
46
+ mac_address (Optional[str]): The Bluetooth MAC address of the iDotMatrix device. If not provided,
47
+ the client will attempt to discover devices.
48
+ max_ble_packet_size (Optional[int]): Overrides the detected maximum size of a single BLE write.
49
+ Only needed if the BLE stack misreports the negotiated MTU.
50
+ ble_device (Optional[BLEDevice]): The device to connect to, as found by a scanner. Instead of `mac_address`,
51
+ f.e. for the scanner of Home Assistant. See `ConnectionManager.set_device`.
52
+ transport_factory (Optional[Callable]): Creates the `Transport` that talks to the device, to connect in a
53
+ different way than `bleak` does by default. See `ConnectionManager`.
54
+ install_signal_handlers (bool): Whether to close the connection on SIGINT and SIGTERM. Turn it off if the
55
+ application handles these signals itself (f.e. Home Assistant).
56
+ """
57
+ self._connection_manager = ConnectionManager(
58
+ address=mac_address,
59
+ max_ble_packet_size=max_ble_packet_size,
60
+ transport_factory=transport_factory,
61
+ install_signal_handlers=install_signal_handlers,
62
+ )
63
+ self._uses_ble_device = ble_device is not None
64
+ if ble_device is not None:
65
+ mac_address = ble_device.address
66
+ self._connection_manager.set_device(ble_device)
67
+ self._screen_size = screen_size
68
+ self.mac_address = mac_address
69
+
70
+ @property
71
+ def screen_size(self) -> ScreenSize:
72
+ """
73
+ The size of the screen. If it was not given, it is only known after `connect` or `detect_screen_size`.
74
+
75
+ Raises:
76
+ RuntimeError: If the size is not known yet.
77
+ """
78
+ if self._screen_size is None:
79
+ raise RuntimeError(
80
+ "The screen size is not known yet, pass screen_size to IDotMatrixClient, or connect first to let "
81
+ "the client ask the device.")
82
+ return self._screen_size
83
+
84
+ @property
85
+ def screen_size_known(self) -> bool:
86
+ """Whether the size of the screen is known, f.e. because it was given or detected."""
87
+ return self._screen_size is not None
88
+
89
+ @screen_size.setter
90
+ def screen_size(self, screen_size: ScreenSize) -> None:
91
+ self._screen_size = screen_size
92
+
93
+ @cached_property
94
+ def chronograph(self) -> ChronographModule:
95
+ return ChronographModule(
96
+ connection_manager=self._connection_manager,
97
+ )
98
+
99
+ @cached_property
100
+ def clock(self) -> ClockModule:
101
+ return ClockModule(
102
+ connection_manager=self._connection_manager,
103
+ )
104
+
105
+ @cached_property
106
+ def common(self) -> CommonModule:
107
+ return CommonModule(
108
+ connection_manager=self._connection_manager,
109
+ )
110
+
111
+ @cached_property
112
+ def countdown(self) -> CountdownModule:
113
+ return CountdownModule(
114
+ connection_manager=self._connection_manager,
115
+ )
116
+
117
+ @cached_property
118
+ def eco(self) -> EcoModule:
119
+ return EcoModule(
120
+ connection_manager=self._connection_manager,
121
+ )
122
+
123
+ @cached_property
124
+ def effect(self) -> EffectModule:
125
+ return EffectModule(
126
+ connection_manager=self._connection_manager,
127
+ )
128
+
129
+ @cached_property
130
+ def color(self) -> FullscreenColorModule:
131
+ return FullscreenColorModule(
132
+ connection_manager=self._connection_manager,
133
+ )
134
+
135
+ @cached_property
136
+ def gif(self) -> GifModule:
137
+ return GifModule(
138
+ connection_manager=self._connection_manager,
139
+ screen_size=self.screen_size
140
+ )
141
+
142
+ @cached_property
143
+ def graffiti(self) -> GraffitiModule:
144
+ return GraffitiModule(
145
+ connection_manager=self._connection_manager,
146
+ )
147
+
148
+ @cached_property
149
+ def image(self) -> ImageModule:
150
+ return ImageModule(
151
+ connection_manager=self._connection_manager,
152
+ screen_size=self.screen_size
153
+ )
154
+
155
+ @cached_property
156
+ def music_sync(self) -> MusicSyncModule:
157
+ return MusicSyncModule(
158
+ connection_manager=self._connection_manager,
159
+ )
160
+
161
+ @cached_property
162
+ def scoreboard(self) -> ScoreboardModule:
163
+ return ScoreboardModule(
164
+ connection_manager=self._connection_manager,
165
+ )
166
+
167
+ @cached_property
168
+ def system(self) -> SystemModule:
169
+ return SystemModule(
170
+ connection_manager=self._connection_manager,
171
+ )
172
+
173
+ @cached_property
174
+ def text(self) -> TextModule:
175
+ return TextModule(
176
+ connection_manager=self._connection_manager,
177
+ )
178
+
179
+ async def connect(self):
180
+ """
181
+ Connect to the IDotMatrix device.
182
+ """
183
+ if self._uses_ble_device:
184
+ # connecting by address would replace the BLEDevice
185
+ await self._connection_manager.connect()
186
+ elif self.mac_address:
187
+ await self._connection_manager.connect_by_address(self.mac_address)
188
+ else:
189
+ await self._connection_manager.connect_by_discovery()
190
+
191
+ if self._screen_size is None:
192
+ await self.detect_screen_size()
193
+
194
+ async def detect_screen_size(self) -> ScreenSize:
195
+ """
196
+ Asks the device for the size of its screen and uses it from now on. This also sets the time of the device,
197
+ see `CommonModule.get_device_info`.
198
+
199
+ Returns:
200
+ ScreenSize: The size of the screen.
201
+ Raises:
202
+ RuntimeError: If the device does not answer, or has a screen that this library does not support.
203
+ """
204
+ info = await self.common.get_device_info()
205
+ if info is None:
206
+ raise RuntimeError("The device did not tell the size of its screen, pass screen_size to IDotMatrixClient.")
207
+ if info.screen_size is None:
208
+ raise RuntimeError(
209
+ f"The screen type {info.screen_type} of the device is not supported, firmware {info.firmware_version}.")
210
+ self._screen_size = info.screen_size
211
+ return info.screen_size
212
+
213
+ def set_ble_device(self, ble_device: BLEDevice) -> None:
214
+ """
215
+ Hands over a newer `BLEDevice` of the device, f.e. when the scanner of Home Assistant found it again. An
216
+ established connection is not interrupted, the new one is used for the next connection.
217
+ """
218
+ self._uses_ble_device = True
219
+ self.mac_address = ble_device.address
220
+ self._connection_manager.set_device(ble_device)
221
+
222
+ async def disconnect(self):
223
+ """
224
+ Disconnect from the IDotMatrix device.
225
+ """
226
+ await self._connection_manager.disconnect()
227
+
228
+ async def turn_on(self):
229
+ """
230
+ Turn on the IDotMatrix device.
231
+ """
232
+ await self.common.turn_on()
233
+
234
+ async def turn_off(self):
235
+ """
236
+ Turn off the IDotMatrix device.
237
+ """
238
+ await self.common.turn_off()
239
+
240
+ async def set_brightness(self, brightness_percent: int):
241
+ """
242
+ Set the brightness of the IDotMatrix device.
243
+
244
+ Args:
245
+ brightness_percent (int): Brightness level (5-100).
246
+ """
247
+ await self.common.set_brightness(brightness_percent=brightness_percent)
248
+
249
+ async def reset(self):
250
+ """
251
+ Reset the IDotMatrix device.
252
+ """
253
+ await self.common.reset()
254
+
255
+ def add_connection_listener(
256
+ self,
257
+ connection_listener: ConnectionListener,
258
+ ):
259
+ """
260
+ Add listeners for connection events.
261
+
262
+ Args:
263
+ connection_listener (ConnectionListener): The listener to be added.
264
+ """
265
+ self._connection_manager.add_connection_listener(connection_listener)
266
+
267
+ def set_auto_reconnect(self, auto_reconnect: bool):
268
+ """
269
+ Set whether the client should automatically reconnect to the device if the connection is lost.
270
+ Args:
271
+ auto_reconnect (bool): True to enable auto-reconnect, False to disable.
272
+ """
273
+ self._connection_manager.set_auto_reconnect(auto_reconnect=auto_reconnect)
274
+
275
+ def set_heartbeat(self, interval_seconds: Optional[float]):
276
+ """
277
+ Set whether a connection that died silently (f.e. because the device was switched off) is detected.
278
+ While the device is silent for the given time it is asked for a sign of life, and if it does not answer the
279
+ connection is closed, the connection listeners are informed, and the connection is restored if
280
+ `set_auto_reconnect` is enabled.
281
+ Args:
282
+ interval_seconds (Optional[float]): How long the device may be silent before it is checked, or None to disable.
283
+ """
284
+ self._connection_manager.set_heartbeat(interval_seconds=interval_seconds)