-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathlight.py
More file actions
264 lines (229 loc) · 9.74 KB
/
light.py
File metadata and controls
264 lines (229 loc) · 9.74 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""Support for lights."""
import enum
import json
import struct
from typing import Optional
from . import exceptions as e
from .device import Device
class lb1_base(Device):
"""Base for lb1 style devices."""
def get_state(self) -> dict:
"""Return the power state of the device.
Example: `{'red': 128, 'blue': 255, 'green': 128, 'pwr': 1, 'brightness': 75, 'colortemp': 2700, 'hue': 240, 'saturation': 50, 'transitionduration': 1500, 'maxworktime': 0, 'bulb_colormode': 1, 'bulb_scenes': '["@01686464,0,0,0", "#ffffff,10,0,#000000,190,0,0", "2700+100,0,0,0", "#ff0000,500,2500,#00FF00,500,2500,#0000FF,500,2500,0", "@01686464,100,2400,@01686401,100,2400,0", "@01686464,100,2400,@01686401,100,2400,@005a6464,100,2400,@005a6401,100,2400,0", "@01686464,10,0,@00000000,190,0,0", "@01686464,200,0,@005a6464,200,0,0"]', 'bulb_scene': '', 'bulb_sceneidx': 255}`
"""
packet = self._encode(1, {})
response = self.send_packet(0x6A, packet)
e.check_error(response[0x22:0x24])
return self._decode(response)
def _encode(self, flag: int, state: dict) -> bytes:
"""Encode a JSON packet."""
# flag: 1 for reading, 2 for writing.
packet = bytearray(14)
data = json.dumps(state, separators=(",", ":")).encode()
p_len = 12 + len(data)
struct.pack_into(
"<HHHHBBI", packet, 0, p_len, 0xA5A5, 0x5A5A, 0, flag, 0x0B, len(data)
)
packet.extend(data)
checksum = sum(packet[0x02:], 0xBEAF) & 0xFFFF
packet[0x06:0x08] = checksum.to_bytes(2, "little")
return packet
def _decode(self, response: bytes) -> dict:
"""Decode a JSON packet."""
payload = self.decrypt(response[0x38:])
js_len = struct.unpack_from("<I", payload, 0xA)[0]
state = json.loads(payload[0xE:0xE+js_len])
return state
class lb1(lb1_base):
"""Controls a Broadlink LB1."""
TYPE = "LB1"
@enum.unique
class ColorMode(enum.IntEnum):
"""Enumerates color modes."""
RGB = 0
WHITE = 1
SCENE = 2
def set_state(
self,
pwr: Optional[bool] = None,
red: Optional[int] = None,
blue: Optional[int] = None,
green: Optional[int] = None,
brightness: Optional[int] = None,
colortemp: Optional[int] = None,
hue: Optional[int] = None,
saturation: Optional[int] = None,
transitionduration: Optional[int] = None,
maxworktime: Optional[int] = None,
bulb_colormode: Optional[int] = None,
bulb_scenes: Optional[str] = None,
bulb_scene: Optional[str] = None,
bulb_sceneidx: Optional[int] = None,
) -> dict:
"""Set the power state of the device."""
state = {}
if pwr is not None:
state["pwr"] = int(bool(pwr))
if red is not None:
state["red"] = int(red)
if blue is not None:
state["blue"] = int(blue)
if green is not None:
state["green"] = int(green)
if brightness is not None:
state["brightness"] = int(brightness)
if colortemp is not None:
state["colortemp"] = int(colortemp)
if hue is not None:
state["hue"] = int(hue)
if saturation is not None:
state["saturation"] = int(saturation)
if transitionduration is not None:
state["transitionduration"] = int(transitionduration)
if maxworktime is not None:
state["maxworktime"] = int(maxworktime)
if bulb_colormode is not None:
state["bulb_colormode"] = int(bulb_colormode)
if bulb_scenes is not None:
state["bulb_scenes"] = str(bulb_scenes)
if bulb_scene is not None:
state["bulb_scene"] = str(bulb_scene)
if bulb_sceneidx is not None:
state["bulb_sceneidx"] = int(bulb_sceneidx)
packet = self._encode(2, state)
response = self.send_packet(0x6A, packet)
e.check_error(response[0x22:0x24])
return self._decode(response)
class lb2(Device):
"""Controls a Broadlink LB26/LB27."""
TYPE = "LB2"
@enum.unique
class ColorMode(enum.IntEnum):
"""Enumerates color modes."""
RGB = 0
WHITE = 1
SCENE = 2
def get_state(self) -> dict:
"""Return the power state of the device.
Example: `{'red': 128, 'blue': 255, 'green': 128, 'pwr': 1, 'brightness': 75, 'colortemp': 2700, 'hue': 240, 'saturation': 50, 'transitionduration': 1500, 'maxworktime': 0, 'bulb_colormode': 1, 'bulb_scenes': '["@01686464,0,0,0", "#ffffff,10,0,#000000,190,0,0", "2700+100,0,0,0", "#ff0000,500,2500,#00FF00,500,2500,#0000FF,500,2500,0", "@01686464,100,2400,@01686401,100,2400,0", "@01686464,100,2400,@01686401,100,2400,@005a6464,100,2400,@005a6401,100,2400,0", "@01686464,10,0,@00000000,190,0,0", "@01686464,200,0,@005a6464,200,0,0"]', 'bulb_scene': ''}`
"""
packet = self._encode(1, {})
response = self.send_packet(0x6A, packet)
e.check_error(response[0x22:0x24])
return self._decode(response)
def set_state(
self,
pwr: Optional[bool] = None,
red: Optional[int] = None,
blue: Optional[int] = None,
green: Optional[int] = None,
brightness: Optional[int] = None,
colortemp: Optional[int] = None,
hue: Optional[int] = None,
saturation: Optional[int] = None,
transitionduration: Optional[int] = None,
maxworktime: Optional[int] = None,
bulb_colormode: Optional[int] = None,
bulb_scenes: Optional[str] = None,
bulb_scene: Optional[str] = None,
) -> dict:
"""Set the power state of the device."""
state = {}
if pwr is not None:
state["pwr"] = int(bool(pwr))
if red is not None:
state["red"] = int(red)
if blue is not None:
state["blue"] = int(blue)
if green is not None:
state["green"] = int(green)
if brightness is not None:
state["brightness"] = int(brightness)
if colortemp is not None:
state["colortemp"] = int(colortemp)
if hue is not None:
state["hue"] = int(hue)
if saturation is not None:
state["saturation"] = int(saturation)
if transitionduration is not None:
state["transitionduration"] = int(transitionduration)
if maxworktime is not None:
state["maxworktime"] = int(maxworktime)
if bulb_colormode is not None:
state["bulb_colormode"] = int(bulb_colormode)
if bulb_scenes is not None:
state["bulb_scenes"] = str(bulb_scenes)
if bulb_scene is not None:
state["bulb_scene"] = str(bulb_scene)
packet = self._encode(2, state)
response = self.send_packet(0x6A, packet)
e.check_error(response[0x22:0x24])
return self._decode(response)
def _encode(self, flag: int, state: dict) -> bytes:
"""Encode a JSON packet."""
# flag: 1 for reading, 2 for writing.
packet = bytearray(12)
data = json.dumps(state, separators=(",", ":")).encode()
struct.pack_into(
"<HHHBBI", packet, 0, 0xA5A5, 0x5A5A, 0, flag, 0x0B, len(data)
)
packet.extend(data)
checksum = sum(packet, 0xBEAF) & 0xFFFF
packet[0x04:0x06] = checksum.to_bytes(2, "little")
return packet
def _decode(self, response: bytes) -> dict:
"""Decode a JSON packet."""
payload = self.decrypt(response[0x38:])
js_len = struct.unpack_from("<I", payload, 0x08)[0]
state = json.loads(payload[0x0C:0x0C+js_len])
return state
class fl1(lb1_base):
"""Controls a Luceco Castra Floodlight.
These set_state items control the floodlight
"mode_switch": 0 - detect motion; 5 - manual control; 6 - dusk to dawn; 7 - night glow
"brightness": 0 - 100 percent
"Delay time": 10 - 1800 seconds
"lightpwr": in manual mode, 0 - off; 1 - on
"Brightness control": 5 - 2000 set external brightness below which light turns on
"sensitivity_en": 0 - 3 set how much motion triggers light
"""
TYPE = "FL1"
def fix_state(self, state: dict) -> dict:
"""change state names that contain spaces """
if "Brightness control" in state:
state["brightness_control"] = state["Brightness control"]
del state["Brightness control"]
if "Delay time" in state:
state["delay_time"] = state["Delay time"]
del state["Delay time"]
return state
def get_state(self) -> dict:
return self.fix_state(super().get_state())
def set_state(
self,
mode_switch: Optional[int] = None,
lightpwr: Optional[bool] = None,
brightness: Optional[int] = None,
brightness_control: Optional[int] = None,
delay_time: Optional[int] = None,
sensitivity_en: Optional[int] = None,
) -> dict:
"""Set the power state of the device."""
state = {}
if mode_switch is not None:
state["mode_switch"] = int(mode_switch)
if lightpwr is not None:
state["lightpwr"] = int(bool(lightpwr))
state["mode_switch"] = 5
if brightness is not None:
state["brightness"] = int(brightness)
if brightness_control is not None:
state["Brightness control"] = int(brightness_control)
if delay_time is not None:
state["Delay time"] = int(delay_time)
if sensitivity_en is not None:
state["sensitivity_en"] = int(sensitivity_en)
packet = self._encode(2, state)
response = self.send_packet(0x6A, packet)
e.check_error(response[0x22:0x24])
return self.fix_state(self._decode(response))