Skip to content

Commit c497956

Browse files
authored
Fix type hints (#794)
1 parent 1e11558 commit c497956

File tree

8 files changed

+95
-79
lines changed

8 files changed

+95
-79
lines changed

broadlink/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def discover(
269269

270270
def xdiscover(
271271
timeout: int = DEFAULT_TIMEOUT,
272-
local_ip_address: str = None,
272+
local_ip_address: str | None = None,
273273
discover_ip_address: str = DEFAULT_BCAST_ADDR,
274274
discover_ip_port: int = DEFAULT_PORT,
275275
) -> t.Generator[Device, None, None]:

broadlink/cover.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Support for covers."""
22
import time
3+
from typing import Sequence
34

45
from . import exceptions as e
56
from .device import Device
@@ -63,7 +64,7 @@ class dooya2(Device):
6364

6465
TYPE = "DT360E-2"
6566

66-
def _send(self, operation: int, data: bytes = b""):
67+
def _send(self, operation: int, data: Sequence = b""):
6768
"""Send a command to the device."""
6869
packet = bytearray(12)
6970
packet[0x02] = 0xA5
@@ -120,7 +121,7 @@ class wser(Device):
120121

121122
TYPE = "WSER"
122123

123-
def _send(self, operation: int, data: bytes = b""):
124+
def _send(self, operation: int, data: Sequence = b""):
124125
"""Send a command to the device."""
125126
packet = bytearray(12)
126127
packet[0x02] = 0xA5

broadlink/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
def scan(
2424
timeout: int = DEFAULT_TIMEOUT,
25-
local_ip_address: str = None,
25+
local_ip_address: str | None = None,
2626
discover_ip_address: str = DEFAULT_BCAST_ADDR,
2727
discover_ip_port: int = DEFAULT_PORT,
2828
) -> t.Generator[HelloResponse, None, None]:

broadlink/hub.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_subdevices(self, step: int = 5) -> list:
4242

4343
return sub_devices
4444

45-
def get_state(self, did: str = None) -> dict:
45+
def get_state(self, did: str | None = None) -> dict:
4646
"""Return the power state of the device."""
4747
state = {}
4848
if did is not None:
@@ -55,10 +55,10 @@ def get_state(self, did: str = None) -> dict:
5555

5656
def set_state(
5757
self,
58-
did: str = None,
59-
pwr1: bool = None,
60-
pwr2: bool = None,
61-
pwr3: bool = None,
58+
did: str | None = None,
59+
pwr1: bool | None = None,
60+
pwr2: bool | None = None,
61+
pwr3: bool | None = None,
6262
) -> dict:
6363
"""Set the power state of the device."""
6464
state = {}
@@ -81,7 +81,9 @@ def _encode(self, flag: int, state: dict) -> bytes:
8181
# flag: 1 for reading, 2 for writing.
8282
packet = bytearray(12)
8383
data = json.dumps(state, separators=(",", ":")).encode()
84-
struct.pack_into("<HHHBBI", packet, 0, 0xA5A5, 0x5A5A, 0, flag, 0x0B, len(data))
84+
struct.pack_into(
85+
"<HHHBBI", packet, 0, 0xA5A5, 0x5A5A, 0, flag, 0x0B, len(data)
86+
)
8587
packet.extend(data)
8688
checksum = sum(packet, 0xBEAF) & 0xFFFF
8789
packet[0x04:0x06] = checksum.to_bytes(2, "little")
@@ -91,5 +93,5 @@ def _decode(self, response: bytes) -> dict:
9193
"""Decode a JSON packet."""
9294
payload = self.decrypt(response[0x38:])
9395
js_len = struct.unpack_from("<I", payload, 0x08)[0]
94-
state = json.loads(payload[0x0C : 0x0C + js_len])
96+
state = json.loads(payload[0x0C:0x0C+js_len])
9597
return state

broadlink/light.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ def get_state(self) -> dict:
3232

3333
def set_state(
3434
self,
35-
pwr: bool = None,
36-
red: int = None,
37-
blue: int = None,
38-
green: int = None,
39-
brightness: int = None,
40-
colortemp: int = None,
41-
hue: int = None,
42-
saturation: int = None,
43-
transitionduration: int = None,
44-
maxworktime: int = None,
45-
bulb_colormode: int = None,
46-
bulb_scenes: str = None,
47-
bulb_scene: str = None,
48-
bulb_sceneidx: int = None,
35+
pwr: bool | None = None,
36+
red: int | None = None,
37+
blue: int | None = None,
38+
green: int | None = None,
39+
brightness: int | None = None,
40+
colortemp: int | None = None,
41+
hue: int | None = None,
42+
saturation: int | None = None,
43+
transitionduration: int | None = None,
44+
maxworktime: int | None = None,
45+
bulb_colormode: int | None = None,
46+
bulb_scenes: str | None = None,
47+
bulb_scene: str | None = None,
48+
bulb_sceneidx: int | None = None,
4949
) -> dict:
5050
"""Set the power state of the device."""
5151
state = {}
@@ -101,7 +101,7 @@ def _decode(self, response: bytes) -> dict:
101101
"""Decode a JSON packet."""
102102
payload = self.decrypt(response[0x38:])
103103
js_len = struct.unpack_from("<I", payload, 0xA)[0]
104-
state = json.loads(payload[0xE : 0xE + js_len])
104+
state = json.loads(payload[0xE:0xE+js_len])
105105
return state
106106

107107

@@ -130,19 +130,19 @@ def get_state(self) -> dict:
130130

131131
def set_state(
132132
self,
133-
pwr: bool = None,
134-
red: int = None,
135-
blue: int = None,
136-
green: int = None,
137-
brightness: int = None,
138-
colortemp: int = None,
139-
hue: int = None,
140-
saturation: int = None,
141-
transitionduration: int = None,
142-
maxworktime: int = None,
143-
bulb_colormode: int = None,
144-
bulb_scenes: str = None,
145-
bulb_scene: str = None,
133+
pwr: bool | None = None,
134+
red: int | None = None,
135+
blue: int | None = None,
136+
green: int | None = None,
137+
brightness: int | None = None,
138+
colortemp: int | None = None,
139+
hue: int | None = None,
140+
saturation: int | None = None,
141+
transitionduration: int | None = None,
142+
maxworktime: int | None = None,
143+
bulb_colormode: int | None = None,
144+
bulb_scenes: str | None = None,
145+
bulb_scene: str | None = None,
146146
) -> dict:
147147
"""Set the power state of the device."""
148148
state = {}
@@ -183,7 +183,9 @@ def _encode(self, flag: int, state: dict) -> bytes:
183183
# flag: 1 for reading, 2 for writing.
184184
packet = bytearray(12)
185185
data = json.dumps(state, separators=(",", ":")).encode()
186-
struct.pack_into("<HHHBBI", packet, 0, 0xA5A5, 0x5A5A, 0, flag, 0x0B, len(data))
186+
struct.pack_into(
187+
"<HHHBBI", packet, 0, 0xA5A5, 0x5A5A, 0, flag, 0x0B, len(data)
188+
)
187189
packet.extend(data)
188190
checksum = sum(packet, 0xBEAF) & 0xFFFF
189191
packet[0x04:0x06] = checksum.to_bytes(2, "little")
@@ -193,5 +195,5 @@ def _decode(self, response: bytes) -> dict:
193195
"""Decode a JSON packet."""
194196
payload = self.decrypt(response[0x38:])
195197
js_len = struct.unpack_from("<I", payload, 0x08)[0]
196-
state = json.loads(payload[0x0C : 0x0C + js_len])
198+
state = json.loads(payload[0x0C:0x0C+js_len])
197199
return state

broadlink/remote.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .device import Device
77

88

9-
def pulses_to_data(pulses: t.List[int], tick: int = 32.84) -> None:
9+
def pulses_to_data(pulses: t.List[int], tick: float = 32.84) -> bytes:
1010
"""Convert a microsecond duration sequence into a Broadlink IR packet."""
1111
result = bytearray(4)
1212
result[0x00] = 0x26
@@ -25,7 +25,7 @@ def pulses_to_data(pulses: t.List[int], tick: int = 32.84) -> None:
2525
return result
2626

2727

28-
def data_to_pulses(data: bytes, tick: int = 32.84) -> t.List[int]:
28+
def data_to_pulses(data: bytes, tick: float = 32.84) -> t.List[int]:
2929
"""Parse a Broadlink packet into a microsecond duration sequence."""
3030
result = []
3131
index = 4
@@ -38,8 +38,8 @@ def data_to_pulses(data: bytes, tick: int = 32.84) -> t.List[int]:
3838
if chunk == 0:
3939
try:
4040
chunk = 256 * data[index] + data[index + 1]
41-
except IndexError:
42-
raise ValueError("Malformed data.")
41+
except IndexError as err:
42+
raise ValueError("Malformed data.") from err
4343
index += 2
4444

4545
result.append(int(chunk * tick))
@@ -95,7 +95,7 @@ def check_frequency(self) -> t.Tuple[bool, float]:
9595
frequency = struct.unpack("<I", resp[1:5])[0] / 1000.0
9696
return is_found, frequency
9797

98-
def find_rf_packet(self, frequency: float = None) -> None:
98+
def find_rf_packet(self, frequency: float | None = None) -> None:
9999
"""Enter radiofrequency learning mode."""
100100
payload = bytearray()
101101
if frequency:
@@ -129,7 +129,7 @@ def _send(self, command: int, data: bytes = b"") -> bytes:
129129
e.check_error(resp[0x22:0x24])
130130
payload = self.decrypt(resp[0x38:])
131131
p_len = struct.unpack("<H", payload[:0x2])[0]
132-
return payload[0x6 : p_len + 2]
132+
return payload[0x6:p_len+2]
133133

134134

135135
class rm4mini(rmminib):

broadlink/sensor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Support for sensors."""
2+
from typing import Sequence
3+
24
from . import exceptions as e
35
from .device import Device
46

@@ -45,7 +47,7 @@ class a2(Device):
4547

4648
TYPE = "A2"
4749

48-
def _send(self, operation: int, data: bytes = b""):
50+
def _send(self, operation: int, data: Sequence = b""):
4951
"""Send a command to the device."""
5052
packet = bytearray(12)
5153
packet[0x02] = 0xA5

broadlink/switch.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ def set_nightlight(self, ntlight: bool) -> None:
127127

128128
def set_state(
129129
self,
130-
pwr: bool = None,
131-
ntlight: bool = None,
132-
indicator: bool = None,
133-
ntlbrightness: int = None,
134-
maxworktime: int = None,
135-
childlock: bool = None,
130+
pwr: bool | None = None,
131+
ntlight: bool | None = None,
132+
indicator: bool | None = None,
133+
ntlbrightness: int | None = None,
134+
maxworktime: int | None = None,
135+
childlock: bool | None = None,
136136
) -> dict:
137137
"""Set state of device."""
138138
state = {}
@@ -186,7 +186,7 @@ def _decode(self, response: bytes) -> dict:
186186
e.check_error(response[0x22:0x24])
187187
payload = self.decrypt(response[0x38:])
188188
js_len = struct.unpack_from("<I", payload, 0x08)[0]
189-
state = json.loads(payload[0x0C : 0x0C + js_len])
189+
state = json.loads(payload[0x0C:0x0C+js_len])
190190
return state
191191

192192

@@ -234,7 +234,7 @@ def _decode(self, response: bytes) -> dict:
234234
e.check_error(response[0x22:0x24])
235235
payload = self.decrypt(response[0x38:])
236236
js_len = struct.unpack_from("<I", payload, 0xA)[0]
237-
state = json.loads(payload[0x0E : 0x0E + js_len])
237+
state = json.loads(payload[0x0E:0x0E+js_len])
238238
return state
239239

240240

@@ -255,13 +255,13 @@ def get_state(self) -> dict:
255255

256256
def set_state(
257257
self,
258-
pwr: bool = None,
259-
pwr1: bool = None,
260-
pwr2: bool = None,
261-
maxworktime: int = None,
262-
maxworktime1: int = None,
263-
maxworktime2: int = None,
264-
idcbrightness: int = None,
258+
pwr: bool | None = None,
259+
pwr1: bool | None = None,
260+
pwr2: bool | None = None,
261+
maxworktime: int | None = None,
262+
maxworktime1: int | None = None,
263+
maxworktime2: int | None = None,
264+
idcbrightness: int | None = None,
265265
) -> dict:
266266
"""Set the power state of the device."""
267267
state = {}
@@ -291,7 +291,16 @@ def _encode(self, flag: int, state: dict) -> bytes:
291291
data = json.dumps(state).encode()
292292
length = 12 + len(data)
293293
struct.pack_into(
294-
"<HHHHBBI", packet, 0, length, 0xA5A5, 0x5A5A, 0x0000, flag, 0x0B, len(data)
294+
"<HHHHBBI",
295+
packet,
296+
0,
297+
length,
298+
0xA5A5,
299+
0x5A5A,
300+
0x0000,
301+
flag,
302+
0x0B,
303+
len(data),
295304
)
296305
packet.extend(data)
297306
checksum = sum(packet[0x2:], 0xBEAF) & 0xFFFF
@@ -302,7 +311,7 @@ def _decode(self, response: bytes) -> dict:
302311
"""Decode a message."""
303312
payload = self.decrypt(response[0x38:])
304313
js_len = struct.unpack_from("<I", payload, 0x0A)[0]
305-
state = json.loads(payload[0x0E : 0x0E + js_len])
314+
state = json.loads(payload[0x0E:0x0E+js_len])
306315
return state
307316

308317

@@ -313,19 +322,19 @@ class ehc31(bg1):
313322

314323
def set_state(
315324
self,
316-
pwr: bool = None,
317-
pwr1: bool = None,
318-
pwr2: bool = None,
319-
pwr3: bool = None,
320-
maxworktime1: int = None,
321-
maxworktime2: int = None,
322-
maxworktime3: int = None,
323-
idcbrightness: int = None,
324-
childlock: bool = None,
325-
childlock1: bool = None,
326-
childlock2: bool = None,
327-
childlock3: bool = None,
328-
childlock4: bool = None,
325+
pwr: bool | None = None,
326+
pwr1: bool | None = None,
327+
pwr2: bool | None = None,
328+
pwr3: bool | None = None,
329+
maxworktime1: int | None = None,
330+
maxworktime2: int | None = None,
331+
maxworktime3: int | None = None,
332+
idcbrightness: int | None = None,
333+
childlock: bool | None = None,
334+
childlock1: bool | None = None,
335+
childlock2: bool | None = None,
336+
childlock3: bool | None = None,
337+
childlock4: bool | None = None,
329338
) -> dict:
330339
"""Set the power state of the device."""
331340
state = {}
@@ -449,7 +458,7 @@ def get_state(self) -> dict:
449458

450459
def get_value(start, end, factors):
451460
value = sum(
452-
int(payload_str[i - 2 : i]) * factor
461+
int(payload_str[i-2:i]) * factor
453462
for i, factor in zip(range(start, end, -2), factors)
454463
)
455464
return value

0 commit comments

Comments
 (0)