Skip to content

Commit 0a9acab

Browse files
authored
Make type hints compatible with Python 3.6 (#797)
1 parent eb56e7a commit 0a9acab

File tree

8 files changed

+86
-83
lines changed

8 files changed

+86
-83
lines changed

broadlink/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
"""The python-broadlink library."""
33
import socket
4-
import typing as t
4+
from typing import Generator, List, Optional, Tuple, Union
55

66
from . import exceptions as e
77
from .const import DEFAULT_BCAST_ADDR, DEFAULT_PORT, DEFAULT_TIMEOUT
@@ -212,8 +212,8 @@
212212

213213
def gendevice(
214214
dev_type: int,
215-
host: t.Tuple[str, int],
216-
mac: t.Union[bytes, str],
215+
host: Tuple[str, int],
216+
mac: Union[bytes, str],
217217
name: str = "",
218218
is_locked: bool = False,
219219
) -> Device:
@@ -265,10 +265,10 @@ def hello(
265265

266266
def discover(
267267
timeout: int = DEFAULT_TIMEOUT,
268-
local_ip_address: str = None,
268+
local_ip_address: Optional[str] = None,
269269
discover_ip_address: str = DEFAULT_BCAST_ADDR,
270270
discover_ip_port: int = DEFAULT_PORT,
271-
) -> t.List[Device]:
271+
) -> List[Device]:
272272
"""Discover devices connected to the local network."""
273273
responses = scan(
274274
timeout, local_ip_address, discover_ip_address, discover_ip_port
@@ -278,10 +278,10 @@ def discover(
278278

279279
def xdiscover(
280280
timeout: int = DEFAULT_TIMEOUT,
281-
local_ip_address: str | None = None,
281+
local_ip_address: Optional[str] = None,
282282
discover_ip_address: str = DEFAULT_BCAST_ADDR,
283283
discover_ip_port: int = DEFAULT_PORT,
284-
) -> t.Generator[Device, None, None]:
284+
) -> Generator[Device, None, None]:
285285
"""Discover devices connected to the local network.
286286
287287
This function returns a generator that yields devices instantly.

broadlink/device.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import threading
44
import random
55
import time
6-
import typing as t
6+
from typing import Generator, Optional, Tuple, Union
77

88
from cryptography.hazmat.backends import default_backend
99
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@@ -17,15 +17,15 @@
1717
)
1818
from .protocol import Datetime
1919

20-
HelloResponse = t.Tuple[int, t.Tuple[str, int], str, str, bool]
20+
HelloResponse = Tuple[int, Tuple[str, int], str, str, bool]
2121

2222

2323
def scan(
2424
timeout: int = DEFAULT_TIMEOUT,
25-
local_ip_address: str | None = None,
25+
local_ip_address: Optional[str] = None,
2626
discover_ip_address: str = DEFAULT_BCAST_ADDR,
2727
discover_ip_port: int = DEFAULT_PORT,
28-
) -> t.Generator[HelloResponse, None, None]:
28+
) -> Generator[HelloResponse, None, None]:
2929
"""Broadcast a hello message and yield responses."""
3030
conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
3131
conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -100,8 +100,8 @@ class Device:
100100

101101
def __init__(
102102
self,
103-
host: t.Tuple[str, int],
104-
mac: t.Union[bytes, str],
103+
host: Tuple[str, int],
104+
mac: Union[bytes, str],
105105
devtype: int,
106106
timeout: int = DEFAULT_TIMEOUT,
107107
name: str = "",

broadlink/helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Helper functions and classes."""
2-
import typing as t
2+
from typing import Dict, List, Sequence
33

44

55
class CRC16:
@@ -8,10 +8,10 @@ class CRC16:
88
CRC tables are cached for performance.
99
"""
1010

11-
_cache: t.Dict[int, t.List[int]] = {}
11+
_cache: Dict[int, List[int]] = {}
1212

1313
@classmethod
14-
def get_table(cls, polynomial: int) -> t.List[int]:
14+
def get_table(cls, polynomial: int) -> List[int]:
1515
"""Return the CRC-16 table for a polynomial."""
1616
try:
1717
crc_table = cls._cache[polynomial]
@@ -31,7 +31,7 @@ def get_table(cls, polynomial: int) -> t.List[int]:
3131
@classmethod
3232
def calculate(
3333
cls,
34-
sequence: t.Sequence[int],
34+
sequence: Sequence[int],
3535
polynomial: int = 0xA001, # CRC-16-ANSI.
3636
init_value: int = 0xFFFF,
3737
) -> int:

broadlink/hub.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Support for hubs."""
22
import struct
33
import json
4+
from typing import Optional
45

56
from . import exceptions as e
67
from .device import Device
@@ -42,7 +43,7 @@ def get_subdevices(self, step: int = 5) -> list:
4243

4344
return sub_devices
4445

45-
def get_state(self, did: str | None = None) -> dict:
46+
def get_state(self, did: Optional[str] = None) -> dict:
4647
"""Return the power state of the device."""
4748
state = {}
4849
if did is not None:
@@ -55,10 +56,10 @@ def get_state(self, did: str | None = None) -> dict:
5556

5657
def set_state(
5758
self,
58-
did: str | None = None,
59-
pwr1: bool | None = None,
60-
pwr2: bool | None = None,
61-
pwr3: bool | None = None,
59+
did: Optional[str] = None,
60+
pwr1: Optional[bool] = None,
61+
pwr2: Optional[bool] = None,
62+
pwr3: Optional[bool] = None,
6263
) -> dict:
6364
"""Set the power state of the device."""
6465
state = {}

broadlink/light.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import enum
33
import json
44
import struct
5+
from typing import Optional
56

67
from . import exceptions as e
78
from .device import Device
@@ -32,20 +33,20 @@ def get_state(self) -> dict:
3233

3334
def set_state(
3435
self,
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,
36+
pwr: Optional[bool] = None,
37+
red: Optional[int] = None,
38+
blue: Optional[int] = None,
39+
green: Optional[int] = None,
40+
brightness: Optional[int] = None,
41+
colortemp: Optional[int] = None,
42+
hue: Optional[int] = None,
43+
saturation: Optional[int] = None,
44+
transitionduration: Optional[int] = None,
45+
maxworktime: Optional[int] = None,
46+
bulb_colormode: Optional[int] = None,
47+
bulb_scenes: Optional[str] = None,
48+
bulb_scene: Optional[str] = None,
49+
bulb_sceneidx: Optional[int] = None,
4950
) -> dict:
5051
"""Set the power state of the device."""
5152
state = {}
@@ -130,19 +131,19 @@ def get_state(self) -> dict:
130131

131132
def set_state(
132133
self,
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,
134+
pwr: Optional[bool] = None,
135+
red: Optional[int] = None,
136+
blue: Optional[int] = None,
137+
green: Optional[int] = None,
138+
brightness: Optional[int] = None,
139+
colortemp: Optional[int] = None,
140+
hue: Optional[int] = None,
141+
saturation: Optional[int] = None,
142+
transitionduration: Optional[int] = None,
143+
maxworktime: Optional[int] = None,
144+
bulb_colormode: Optional[int] = None,
145+
bulb_scenes: Optional[str] = None,
146+
bulb_scene: Optional[str] = None,
146147
) -> dict:
147148
"""Set the power state of the device."""
148149
state = {}

broadlink/remote.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Support for universal remotes."""
22
import struct
3-
import typing as t
3+
from typing import List, Optional, Tuple
44

55
from . import exceptions as e
66
from .device import Device
77

88

9-
def pulses_to_data(pulses: t.List[int], tick: float = 32.84) -> bytes:
9+
def pulses_to_data(pulses: 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: float = 32.84) -> bytes:
2525
return result
2626

2727

28-
def data_to_pulses(data: bytes, tick: float = 32.84) -> t.List[int]:
28+
def data_to_pulses(data: bytes, tick: float = 32.84) -> List[int]:
2929
"""Parse a Broadlink packet into a microsecond duration sequence."""
3030
result = []
3131
index = 4
@@ -88,14 +88,14 @@ def sweep_frequency(self) -> None:
8888
"""Sweep frequency."""
8989
self._send(0x19)
9090

91-
def check_frequency(self) -> t.Tuple[bool, float]:
91+
def check_frequency(self) -> Tuple[bool, float]:
9292
"""Return True if the frequency was identified successfully."""
9393
resp = self._send(0x1A)
9494
is_found = bool(resp[0])
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) -> None:
98+
def find_rf_packet(self, frequency: Optional[float] = None) -> None:
9999
"""Enter radiofrequency learning mode."""
100100
payload = bytearray()
101101
if frequency:

broadlink/switch.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Support for switches."""
22
import json
33
import struct
4+
from typing import Optional
45

56
from . import exceptions as e
67
from .device import Device
@@ -127,12 +128,12 @@ def set_nightlight(self, ntlight: bool) -> None:
127128

128129
def set_state(
129130
self,
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,
131+
pwr: Optional[bool] = None,
132+
ntlight: Optional[bool] = None,
133+
indicator: Optional[bool] = None,
134+
ntlbrightness: Optional[int] = None,
135+
maxworktime: Optional[int] = None,
136+
childlock: Optional[bool] = None,
136137
) -> dict:
137138
"""Set state of device."""
138139
state = {}
@@ -255,13 +256,13 @@ def get_state(self) -> dict:
255256

256257
def set_state(
257258
self,
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,
259+
pwr: Optional[bool] = None,
260+
pwr1: Optional[bool] = None,
261+
pwr2: Optional[bool] = None,
262+
maxworktime: Optional[int] = None,
263+
maxworktime1: Optional[int] = None,
264+
maxworktime2: Optional[int] = None,
265+
idcbrightness: Optional[int] = None,
265266
) -> dict:
266267
"""Set the power state of the device."""
267268
state = {}
@@ -322,19 +323,19 @@ class ehc31(bg1):
322323

323324
def set_state(
324325
self,
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,
326+
pwr: Optional[bool] = None,
327+
pwr1: Optional[bool] = None,
328+
pwr2: Optional[bool] = None,
329+
pwr3: Optional[bool] = None,
330+
maxworktime1: Optional[int] = None,
331+
maxworktime2: Optional[int] = None,
332+
maxworktime3: Optional[int] = None,
333+
idcbrightness: Optional[int] = None,
334+
childlock: Optional[bool] = None,
335+
childlock1: Optional[bool] = None,
336+
childlock2: Optional[bool] = None,
337+
childlock3: Optional[bool] = None,
338+
childlock4: Optional[bool] = None,
338339
) -> dict:
339340
"""Set the power state of the device."""
340341
state = {}

cli/broadlink_cli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import argparse
33
import base64
44
import time
5-
import typing as t
5+
from typing import List
66

77
import broadlink
88
from broadlink.const import DEFAULT_PORT
@@ -16,15 +16,15 @@ def auto_int(x):
1616
return int(x, 0)
1717

1818

19-
def format_pulses(pulses: t.List[int]) -> str:
19+
def format_pulses(pulses: List[int]) -> str:
2020
"""Format pulses."""
2121
return " ".join(
2222
f"+{pulse}" if i % 2 == 0 else f"-{pulse}"
2323
for i, pulse in enumerate(pulses)
2424
)
2525

2626

27-
def parse_pulses(data: t.List[str]) -> t.List[int]:
27+
def parse_pulses(data: List[str]) -> List[int]:
2828
"""Parse pulses."""
2929
return [abs(int(s)) for s in data]
3030

0 commit comments

Comments
 (0)