lametric-py 0.1.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.
lametric/__init__.py ADDED
@@ -0,0 +1,127 @@
1
+ """Public package exports for lametric-py."""
2
+
3
+ from importlib.metadata import PackageNotFoundError, version
4
+
5
+ from .cloud import CloudDevice, CloudUser, LaMetricCloud
6
+ from .const import (
7
+ AlarmSound,
8
+ BrightnessMode,
9
+ CanvasFillType,
10
+ CanvasPostProcessEffectType,
11
+ CanvasPostProcessType,
12
+ CanvasRenderMode,
13
+ DeviceModels,
14
+ DeviceModes,
15
+ DisplayType,
16
+ IconType,
17
+ NotificationPriority,
18
+ NotificationSound,
19
+ NotificationType,
20
+ ScreensaverModes,
21
+ SoundCategory,
22
+ )
23
+ from .device import LaMetricDevice
24
+ from .device_apps import App, Parameter, Widget
25
+ from .device_configs import (
26
+ CanvasFadingPixelsEffectParameters,
27
+ CanvasPostProcess,
28
+ CanvasPostProcessParameters,
29
+ ScreensaverConfig,
30
+ ScreensaverConfigParams,
31
+ StreamConfig,
32
+ )
33
+ from .device_notifications import (
34
+ BuiltinSound,
35
+ GoalFrame,
36
+ GoalFrameData,
37
+ Notification,
38
+ NotificationData,
39
+ SimpleFrame,
40
+ SpikeChartFrame,
41
+ WebSound,
42
+ )
43
+ from .device_states import (
44
+ Canvas,
45
+ CanvasArea,
46
+ CanvasSize,
47
+ DeviceAudioState,
48
+ DeviceBluetoothState,
49
+ DeviceDisplayState,
50
+ DeviceSoftwareUpdate,
51
+ DeviceState,
52
+ DeviceWiFiState,
53
+ IntRange,
54
+ Screensaver,
55
+ StreamState,
56
+ TimeBasedScreensaverMode,
57
+ WhenDarkScreensaverMode,
58
+ )
59
+ from .exceptions import (
60
+ LaMetricApiError,
61
+ LaMetricAuthenticationError,
62
+ LaMetricConnectionError,
63
+ LaMetricUnsupportedError,
64
+ )
65
+
66
+ try:
67
+ __version__ = version("lametric-py")
68
+ except PackageNotFoundError:
69
+ __version__ = "0.0.0"
70
+
71
+ __all__ = [
72
+ "AlarmSound",
73
+ "App",
74
+ "BrightnessMode",
75
+ "BuiltinSound",
76
+ "Canvas",
77
+ "CanvasArea",
78
+ "CanvasFadingPixelsEffectParameters",
79
+ "CanvasFillType",
80
+ "CanvasPostProcess",
81
+ "CanvasPostProcessEffectType",
82
+ "CanvasPostProcessParameters",
83
+ "CanvasPostProcessType",
84
+ "CanvasRenderMode",
85
+ "CanvasSize",
86
+ "CloudDevice",
87
+ "CloudUser",
88
+ "DeviceAudioState",
89
+ "DeviceBluetoothState",
90
+ "DeviceDisplayState",
91
+ "DeviceModes",
92
+ "DeviceModels",
93
+ "DeviceSoftwareUpdate",
94
+ "DeviceState",
95
+ "DeviceWiFiState",
96
+ "DisplayType",
97
+ "GoalFrame",
98
+ "GoalFrameData",
99
+ "IconType",
100
+ "IntRange",
101
+ "LaMetricApiError",
102
+ "LaMetricAuthenticationError",
103
+ "LaMetricCloud",
104
+ "LaMetricConnectionError",
105
+ "LaMetricDevice",
106
+ "LaMetricUnsupportedError",
107
+ "Notification",
108
+ "NotificationData",
109
+ "NotificationPriority",
110
+ "NotificationSound",
111
+ "NotificationType",
112
+ "Parameter",
113
+ "Screensaver",
114
+ "ScreensaverConfig",
115
+ "ScreensaverConfigParams",
116
+ "ScreensaverModes",
117
+ "SimpleFrame",
118
+ "SoundCategory",
119
+ "SpikeChartFrame",
120
+ "StreamConfig",
121
+ "StreamState",
122
+ "TimeBasedScreensaverMode",
123
+ "WebSound",
124
+ "WhenDarkScreensaverMode",
125
+ "Widget",
126
+ "__version__",
127
+ ]
lametric/cloud.py ADDED
@@ -0,0 +1,168 @@
1
+ """Async client for the LaMetric cloud API."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import socket
7
+ from dataclasses import dataclass
8
+ from ipaddress import IPv4Address
9
+ from logging import Logger
10
+ from typing import (
11
+ Any,
12
+ Self,
13
+ )
14
+
15
+ import aiohttp
16
+ import backoff
17
+ from aiohttp import hdrs
18
+ from mashumaro.mixins.orjson import DataClassORJSONMixin
19
+ from yarl import URL
20
+
21
+ from .exceptions import (
22
+ LaMetricApiError,
23
+ LaMetricAuthenticationError,
24
+ LaMetricConnectionError,
25
+ )
26
+
27
+
28
+ @dataclass(kw_only=True)
29
+ class CloudUser(DataClassORJSONMixin):
30
+ """Authenticated LaMetric cloud user returned by the API."""
31
+
32
+ email: str
33
+ name: str
34
+ apps_count: int
35
+ private_device_count: int
36
+ private_apps_count: int
37
+
38
+
39
+ @dataclass(kw_only=True)
40
+ class CloudDevice(DataClassORJSONMixin):
41
+ """Cloud-managed device metadata visible to the authenticated user."""
42
+
43
+ id: int
44
+ name: str
45
+ state: str
46
+ serial_number: str
47
+ api_key: str
48
+ ipv4_internal: IPv4Address
49
+ mac: str
50
+ wifi_ssid: str
51
+
52
+
53
+ @dataclass
54
+ class LaMetricCloud:
55
+ """Client for LaMetric cloud endpoints using bearer-token authentication."""
56
+
57
+ token: str
58
+ request_timeout: int = 3
59
+ session: aiohttp.client.ClientSession | None = None
60
+ logger: Logger | None = None
61
+ _close_session: bool = False
62
+
63
+ @backoff.on_exception(
64
+ backoff.expo, LaMetricConnectionError, max_tries=3, logger=logger
65
+ )
66
+ async def _handle_api_request(
67
+ self,
68
+ uri: str,
69
+ ) -> Any:
70
+ """Execute an authenticated cloud API request and return parsed JSON."""
71
+
72
+ url = URL.build(scheme="https", host="developer.lametric.com", path=uri)
73
+
74
+ if self.session is None:
75
+ self.session = aiohttp.ClientSession()
76
+ self._close_session = True
77
+
78
+ try:
79
+ async with asyncio.timeout(self.request_timeout):
80
+ response = await self.session.request(
81
+ hdrs.METH_GET,
82
+ url,
83
+ headers={
84
+ "Authorization": f"Bearer {self.token}",
85
+ "Accept": "application/json",
86
+ },
87
+ raise_for_status=True,
88
+ )
89
+
90
+ content_type = response.headers.get("Content-Type", "")
91
+
92
+ if "application/json" not in content_type:
93
+ raise LaMetricApiError(
94
+ response.status,
95
+ {"content_type": content_type, "content": await response.text()},
96
+ )
97
+
98
+ return await response.json()
99
+
100
+ except TimeoutError as error:
101
+ raise LaMetricConnectionError(f"Request to {url} timed out") from error
102
+
103
+ except aiohttp.ClientResponseError as error:
104
+ if error.status in (401, 403):
105
+ raise LaMetricAuthenticationError(
106
+ f"Authentication failed for {url}: {error.message}"
107
+ ) from error
108
+
109
+ if error.status == 404:
110
+ raise LaMetricApiError(
111
+ f"API endpoint not found at {url}: {error.message}"
112
+ ) from error
113
+
114
+ if error.status == 405:
115
+ raise LaMetricApiError(
116
+ f"Method not allowed for {url}: {error.message}"
117
+ ) from error
118
+
119
+ raise LaMetricApiError(
120
+ f"API request to {url} failed with status: {error.message}"
121
+ ) from error
122
+
123
+ except (aiohttp.ClientError, socket.gaierror) as error:
124
+ raise LaMetricConnectionError(
125
+ f"Connection error while connecting to {url}: {error}"
126
+ ) from error
127
+
128
+ @property
129
+ async def current_user(self) -> CloudUser:
130
+ """Return the currently authenticated cloud user."""
131
+
132
+ response = await self._handle_api_request(uri="/api/v2/me")
133
+
134
+ return CloudUser.from_dict(response)
135
+
136
+ @property
137
+ async def devices(self) -> list[CloudDevice]:
138
+ """Return all devices associated with the authenticated account."""
139
+
140
+ response = await self._handle_api_request(uri="/api/v2/users/me/devices")
141
+
142
+ return [CloudDevice.from_dict(device) for device in response]
143
+
144
+ async def get_device(self, device_id: int) -> CloudDevice:
145
+ """Return a single cloud device by its numeric ID."""
146
+
147
+ response = await self._handle_api_request(
148
+ uri=f"/api/v2/users/me/devices/{device_id}"
149
+ )
150
+
151
+ return CloudDevice.from_dict(response)
152
+
153
+ async def close(self) -> None:
154
+ """Close the internally created HTTP session, if one is in use."""
155
+
156
+ if self._close_session and self.session is not None:
157
+ await self.session.close()
158
+ self.session = None
159
+
160
+ async def __aenter__(self) -> Self:
161
+ """Return the client for use as an async context manager."""
162
+
163
+ return self
164
+
165
+ async def __aexit__(self, *_exc_info: object) -> None:
166
+ """Close the client session when leaving an async context."""
167
+
168
+ await self.close()
lametric/const.py ADDED
@@ -0,0 +1,159 @@
1
+ """Enumerations for LaMetric device API constants."""
2
+
3
+ from enum import StrEnum
4
+
5
+
6
+ class DeviceModels(StrEnum):
7
+ """Known LaMetric hardware model identifiers."""
8
+
9
+ LEGACY_TIME = "LM 37X8"
10
+ TIME = "sa8"
11
+ SKY = "sa5"
12
+
13
+
14
+ class DeviceModes(StrEnum):
15
+ """Operating modes supported by a LaMetric device."""
16
+
17
+ AUTO = "auto"
18
+ MANUAL = "manual"
19
+ SCHEDULE = "schedule"
20
+ KIOSK = "kiosk"
21
+
22
+
23
+ class DisplayType(StrEnum):
24
+ """Display panel capabilities reported by the API."""
25
+
26
+ MONOCHROME = "monochrome"
27
+ GRAYSCALE = "grayscale"
28
+ COLOR = "color"
29
+ MIXED = "mixed"
30
+ FULL_RGB = "full_rgb"
31
+
32
+
33
+ class BrightnessMode(StrEnum):
34
+ """Brightness control mode for the display."""
35
+
36
+ AUTO = "auto"
37
+ MANUAL = "manual"
38
+
39
+
40
+ class ScreensaverModes(StrEnum):
41
+ """Screensaver activation modes."""
42
+
43
+ WHEN_DARK = "when_dark"
44
+ TIME_BASED = "time_based"
45
+
46
+
47
+ class NotificationType(StrEnum):
48
+ """Source of a notification as reported by the device."""
49
+
50
+ INTERNAL = "internal"
51
+ EXTERNAL = "external"
52
+
53
+
54
+ class NotificationPriority(StrEnum):
55
+ """Display priority that determines queue order and interruption behaviour."""
56
+
57
+ INFO = "info"
58
+ WARNING = "warning"
59
+ CRITICAL = "critical"
60
+
61
+
62
+ class IconType(StrEnum):
63
+ """Icon decoration style shown alongside a notification."""
64
+
65
+ NONE = "none"
66
+ INFO = "info"
67
+ ALERT = "alert"
68
+
69
+
70
+ class SoundCategory(StrEnum):
71
+ """Top-level category used to group built-in device sounds."""
72
+
73
+ NOTIFICATIONS = "notifications"
74
+ ALARMS = "alarms"
75
+
76
+
77
+ class AlarmSound(StrEnum):
78
+ """Built-in alarm sounds available on the device."""
79
+
80
+ ALARM1 = "alarm1"
81
+ ALARM2 = "alarm2"
82
+ ALARM3 = "alarm3"
83
+ ALARM4 = "alarm4"
84
+ ALARM5 = "alarm5"
85
+ ALARM6 = "alarm6"
86
+ ALARM7 = "alarm7"
87
+ ALARM8 = "alarm8"
88
+ ALARM9 = "alarm9"
89
+ ALARM10 = "alarm10"
90
+ ALARM11 = "alarm11"
91
+ ALARM12 = "alarm12"
92
+ ALARM13 = "alarm13"
93
+
94
+
95
+ class NotificationSound(StrEnum):
96
+ """Built-in notification sounds available on the device."""
97
+
98
+ BICYCLE = "bicycle"
99
+ CAR = "car"
100
+ CASH = "cash"
101
+ CAT = "cat"
102
+ DOG = "dog"
103
+ DOG2 = "dog2"
104
+ ENERGY = "energy"
105
+ KNOCK_KNOCK = "knock-knock"
106
+ LETTER_EMAIL = "letter_email"
107
+ LOSE1 = "lose1"
108
+ LOSE2 = "lose2"
109
+ NEGATIVE1 = "negative1"
110
+ NEGATIVE2 = "negative2"
111
+ NEGATIVE3 = "negative3"
112
+ NEGATIVE4 = "negative4"
113
+ NEGATIVE5 = "negative5"
114
+ NOTIFICATION = "notification"
115
+ NOTIFICATION2 = "notification2"
116
+ NOTIFICATION3 = "notification3"
117
+ NOTIFICATION4 = "notification4"
118
+ OPEN_DOOR = "open_door"
119
+ POSITIVE1 = "positive1"
120
+ POSITIVE2 = "positive2"
121
+ POSITIVE3 = "positive3"
122
+ POSITIVE4 = "positive4"
123
+ POSITIVE5 = "positive5"
124
+ POSITIVE6 = "positive6"
125
+ STATISTIC = "statistic"
126
+ THUNDER = "thunder"
127
+ WATER1 = "water1"
128
+ WATER2 = "water2"
129
+ WIN = "win"
130
+ WIN2 = "win2"
131
+ WIND = "wind"
132
+ WIND_SHORT = "wind_short"
133
+
134
+
135
+ class CanvasFillType(StrEnum):
136
+ """How the source image is scaled to fit the canvas."""
137
+
138
+ SCALE = "scale"
139
+ TILE = "tile"
140
+
141
+
142
+ class CanvasRenderMode(StrEnum):
143
+ """Pixel layout mode used when rendering to the canvas."""
144
+
145
+ PIXEL = "pixel"
146
+ TRIANGLE = "triangle"
147
+
148
+
149
+ class CanvasPostProcessType(StrEnum):
150
+ """Whether a post-processing effect is applied to the canvas output."""
151
+
152
+ NONE = "none"
153
+ EFFECT = "effect"
154
+
155
+
156
+ class CanvasPostProcessEffectType(StrEnum):
157
+ """Visual effect types available for canvas post-processing."""
158
+
159
+ FADING_PIXELS = "fading_pixels"