-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWLedController.py
203 lines (147 loc) · 6.68 KB
/
WLedController.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
import json
import math
import sys
from datetime import datetime
from enum import Enum
import requests.exceptions
import exceptions
class Specifier(Enum):
none = ""
state = "state"
info = "info"
effects = "effects"
palettes = "palettes"
live = "live"
class NightlightMode(Enum):
instant = 0
fade = 1
color_fade = 2
sunrise = 3
class WLedController:
_api_url: str
_json_url: str
_log: bool
_MIN_BYTE: int
_MAX_BYTE: int
_MAX_TWO_BYTE: int
_MAX_FX: int
def __init__(self, wled_socket: str, log: bool = True) -> None:
self._api_url = f"http://{wled_socket}/"
self._json_url = self._api_url + "json/"
self._log = log
self._MIN_BYTE = 0
self._MAX_BYTE = 255
self._MAX_TWO_BYTE = 65535
self._MAX_FX = 117
try:
self.get_status()
except:
raise exceptions.UnreachableSocketException(wled_socket)
# private assisting methods
@staticmethod
def _get_request(request: str) -> requests.models.Response:
return requests.get(request)
@staticmethod
def _post_request(request: str, arguments: str) -> requests.models.Response:
return requests.post(request, arguments)
@staticmethod
def _timestamp() -> str:
return str(datetime.now())[:-7]
@staticmethod
def _build_data(*args: tuple[str: any]):
return "{" + ", ".join([f'"{arg[0]}": {arg[1]}' for arg in args]) + "}"
def _logging(self, msg: str):
print(f"{self._timestamp()}: {msg}")
# config settings
def activate_log(self) -> None:
self._log = True
def deactivate_log(self) -> None:
self._log = False
# get requests
def get_version(self) -> int:
return int(self._get_request(self._api_url + "version").text)
def get_free_heap(self) -> int:
return int(self._get_request(self._api_url + "freeheap").text)
def get_uptime(self) -> int:
return int(self._get_request(self._api_url + "uptime").text)
def get_timer(self) -> int:
return self.get_status(Specifier.state, "nl")["rem"]
def get_status(self, specification: Specifier = Specifier.none, main_key: str = None) -> dict[str: dict[str: any]]:
response = self._get_request(self._json_url + specification.value).json()
if main_key:
try:
return response[main_key]
except KeyError:
raise exceptions.InvalidMainKeyException(main_key)
else:
return response
# nonparametric functions
def reboot(self) -> int:
return self._get_request(self._api_url + "reset").status_code
def activate(self) -> None:
self.set_arguments(self._build_data(("on", "true")))
def deactivate(self) -> None:
self.set_arguments(self._build_data(("on", "false")))
def toggle(self) -> None:
self.set_arguments(self._build_data(("on", '"t"')))
def activate_timer(self) -> None:
self.set_arguments(self._build_data(("nl.on", 'true')))
def deactivate_timer(self) -> None:
self.set_arguments(self._build_data(("nl.on", 'false')))
def activate_live(self) -> None:
self.set_arguments(self._build_data(("live", 'true')))
def deactivate_live(self) -> None:
self.set_arguments(self._build_data(("live", 'false')))
# parametric functions
def set_arguments(self, arguments: str) -> None:
try:
status = list(json.loads(self._post_request(self._json_url, arguments).text))[0]
if self._log:
self._logging(f"{arguments} --> {status}")
if status == "error":
raise exceptions.InvalidArgumentException(arguments)
except:
if self._log:
self._logging(f"major error occured with argument {arguments}")
raise Exception(f"'{arguments}' resulted in an Error.")
def set_brightness(self, brightness: int) -> None:
if brightness < self._MIN_BYTE or brightness > self._MAX_BYTE:
raise exceptions.ValueOutOfBoundsException(brightness)
self.set_arguments(self._build_data(("bri", brightness)))
def set_transition(self, milliseconds: int) -> None:
milliseconds = milliseconds / 100
if milliseconds < self._MIN_BYTE or milliseconds > self._MAX_TWO_BYTE:
raise exceptions.ValueOutOfBoundsException(milliseconds, stop=self._MAX_TWO_BYTE)
self.set_arguments(self._build_data(("transition", math.floor(milliseconds))))
def set_preset(self, preset: int) -> None:
if preset < -1 or preset > 250:
raise exceptions.ValueOutOfBoundsException(preset, start=-1, stop=250)
self.set_arguments(self._build_data(("ps", preset)))
def set_timer(self, minutes: int) -> None:
if minutes < self._MIN_BYTE or minutes > self._MAX_BYTE:
raise exceptions.ValueOutOfBoundsException(minutes)
self.set_arguments(self._build_data(("nl.dur", minutes)))
def set_timer_mode(self, mode: NightlightMode) -> None:
self.set_arguments(self._build_data(("nl.mode", mode.value)))
def set_timer_brightness(self, target_brightness: int) -> None:
if target_brightness < self._MIN_BYTE or target_brightness > self._MAX_BYTE:
raise exceptions.ValueOutOfBoundsException(target_brightness)
self.set_arguments(self._build_data(("nl.tbri", target_brightness)))
def set_colors(self, *colors: list[int, int, int]) -> None:
for color in colors:
if min(color) < self._MIN_BYTE or max(color) > self._MAX_BYTE:
raise exceptions.ValueOutOfBoundsException(color, [self._MIN_BYTE, self._MIN_BYTE, self._MIN_BYTE],
[self._MAX_BYTE, self._MAX_BYTE, self._MAX_BYTE])
self.set_arguments(self._build_data(("seg", self._build_data(("col", str(list(colors)))))))
def set_effect(self, effect: int) -> None:
if effect < self._MIN_BYTE or effect > self._MAX_FX:
raise exceptions.ValueOutOfBoundsException(effect, stop=self._MAX_FX)
self.set_arguments(self._build_data(("seg", self._build_data(("fx", effect)))))
def set_effect_speed(self, speed: int) -> None:
if speed < self._MIN_BYTE or speed > self._MAX_BYTE:
raise exceptions.ValueOutOfBoundsException(speed)
self.set_arguments(self._build_data(("seg", self._build_data(("sx", speed)))))
def set_effect_intensity(self, intensity: int) -> None:
if intensity < self._MIN_BYTE or intensity > self._MAX_BYTE:
raise exceptions.ValueOutOfBoundsException(intensity)
self.set_arguments(self._build_data(("seg", self._build_data(("ix", intensity)))))