diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 98d04d92..d3135501 100644 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """The python-broadlink library.""" import socket -import typing as t +from typing import Generator, List, Optional, Tuple, Union from . import exceptions as e from .const import DEFAULT_BCAST_ADDR, DEFAULT_PORT, DEFAULT_TIMEOUT @@ -212,8 +212,8 @@ def gendevice( dev_type: int, - host: t.Tuple[str, int], - mac: t.Union[bytes, str], + host: Tuple[str, int], + mac: Union[bytes, str], name: str = "", is_locked: bool = False, ) -> Device: @@ -265,10 +265,10 @@ def hello( def discover( timeout: int = DEFAULT_TIMEOUT, - local_ip_address: str = None, + local_ip_address: Optional[str] = None, discover_ip_address: str = DEFAULT_BCAST_ADDR, discover_ip_port: int = DEFAULT_PORT, -) -> t.List[Device]: +) -> List[Device]: """Discover devices connected to the local network.""" responses = scan( timeout, local_ip_address, discover_ip_address, discover_ip_port @@ -278,10 +278,10 @@ def discover( def xdiscover( timeout: int = DEFAULT_TIMEOUT, - local_ip_address: str | None = None, + local_ip_address: Optional[str] = None, discover_ip_address: str = DEFAULT_BCAST_ADDR, discover_ip_port: int = DEFAULT_PORT, -) -> t.Generator[Device, None, None]: +) -> Generator[Device, None, None]: """Discover devices connected to the local network. This function returns a generator that yields devices instantly. diff --git a/broadlink/device.py b/broadlink/device.py index 287b3542..5a10bc01 100644 --- a/broadlink/device.py +++ b/broadlink/device.py @@ -3,7 +3,7 @@ import threading import random import time -import typing as t +from typing import Generator, Optional, Tuple, Union from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes @@ -17,15 +17,15 @@ ) from .protocol import Datetime -HelloResponse = t.Tuple[int, t.Tuple[str, int], str, str, bool] +HelloResponse = Tuple[int, Tuple[str, int], str, str, bool] def scan( timeout: int = DEFAULT_TIMEOUT, - local_ip_address: str | None = None, + local_ip_address: Optional[str] = None, discover_ip_address: str = DEFAULT_BCAST_ADDR, discover_ip_port: int = DEFAULT_PORT, -) -> t.Generator[HelloResponse, None, None]: +) -> Generator[HelloResponse, None, None]: """Broadcast a hello message and yield responses.""" conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -100,8 +100,8 @@ class Device: def __init__( self, - host: t.Tuple[str, int], - mac: t.Union[bytes, str], + host: Tuple[str, int], + mac: Union[bytes, str], devtype: int, timeout: int = DEFAULT_TIMEOUT, name: str = "", diff --git a/broadlink/helpers.py b/broadlink/helpers.py index 6ee54991..e7b3d4c9 100644 --- a/broadlink/helpers.py +++ b/broadlink/helpers.py @@ -1,5 +1,5 @@ """Helper functions and classes.""" -import typing as t +from typing import Dict, List, Sequence class CRC16: @@ -8,10 +8,10 @@ class CRC16: CRC tables are cached for performance. """ - _cache: t.Dict[int, t.List[int]] = {} + _cache: Dict[int, List[int]] = {} @classmethod - def get_table(cls, polynomial: int) -> t.List[int]: + def get_table(cls, polynomial: int) -> List[int]: """Return the CRC-16 table for a polynomial.""" try: crc_table = cls._cache[polynomial] @@ -31,7 +31,7 @@ def get_table(cls, polynomial: int) -> t.List[int]: @classmethod def calculate( cls, - sequence: t.Sequence[int], + sequence: Sequence[int], polynomial: int = 0xA001, # CRC-16-ANSI. init_value: int = 0xFFFF, ) -> int: diff --git a/broadlink/hub.py b/broadlink/hub.py index 38894090..0fd4ae53 100644 --- a/broadlink/hub.py +++ b/broadlink/hub.py @@ -1,6 +1,7 @@ """Support for hubs.""" import struct import json +from typing import Optional from . import exceptions as e from .device import Device @@ -42,7 +43,7 @@ def get_subdevices(self, step: int = 5) -> list: return sub_devices - def get_state(self, did: str | None = None) -> dict: + def get_state(self, did: Optional[str] = None) -> dict: """Return the power state of the device.""" state = {} if did is not None: @@ -55,10 +56,10 @@ def get_state(self, did: str | None = None) -> dict: def set_state( self, - did: str | None = None, - pwr1: bool | None = None, - pwr2: bool | None = None, - pwr3: bool | None = None, + did: Optional[str] = None, + pwr1: Optional[bool] = None, + pwr2: Optional[bool] = None, + pwr3: Optional[bool] = None, ) -> dict: """Set the power state of the device.""" state = {} diff --git a/broadlink/light.py b/broadlink/light.py index 0dd0d206..1ae87e8f 100644 --- a/broadlink/light.py +++ b/broadlink/light.py @@ -2,6 +2,7 @@ import enum import json import struct +from typing import Optional from . import exceptions as e from .device import Device @@ -32,20 +33,20 @@ def get_state(self) -> dict: def set_state( self, - pwr: bool | None = None, - red: int | None = None, - blue: int | None = None, - green: int | None = None, - brightness: int | None = None, - colortemp: int | None = None, - hue: int | None = None, - saturation: int | None = None, - transitionduration: int | None = None, - maxworktime: int | None = None, - bulb_colormode: int | None = None, - bulb_scenes: str | None = None, - bulb_scene: str | None = None, - bulb_sceneidx: int | None = None, + 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 = {} @@ -130,19 +131,19 @@ def get_state(self) -> dict: def set_state( self, - pwr: bool | None = None, - red: int | None = None, - blue: int | None = None, - green: int | None = None, - brightness: int | None = None, - colortemp: int | None = None, - hue: int | None = None, - saturation: int | None = None, - transitionduration: int | None = None, - maxworktime: int | None = None, - bulb_colormode: int | None = None, - bulb_scenes: str | None = None, - bulb_scene: str | None = None, + 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 = {} diff --git a/broadlink/remote.py b/broadlink/remote.py index 64b7c35d..60c54ce2 100644 --- a/broadlink/remote.py +++ b/broadlink/remote.py @@ -1,12 +1,12 @@ """Support for universal remotes.""" import struct -import typing as t +from typing import List, Optional, Tuple from . import exceptions as e from .device import Device -def pulses_to_data(pulses: t.List[int], tick: float = 32.84) -> bytes: +def pulses_to_data(pulses: List[int], tick: float = 32.84) -> bytes: """Convert a microsecond duration sequence into a Broadlink IR packet.""" result = bytearray(4) result[0x00] = 0x26 @@ -25,7 +25,7 @@ def pulses_to_data(pulses: t.List[int], tick: float = 32.84) -> bytes: return result -def data_to_pulses(data: bytes, tick: float = 32.84) -> t.List[int]: +def data_to_pulses(data: bytes, tick: float = 32.84) -> List[int]: """Parse a Broadlink packet into a microsecond duration sequence.""" result = [] index = 4 @@ -88,14 +88,14 @@ def sweep_frequency(self) -> None: """Sweep frequency.""" self._send(0x19) - def check_frequency(self) -> t.Tuple[bool, float]: + def check_frequency(self) -> Tuple[bool, float]: """Return True if the frequency was identified successfully.""" resp = self._send(0x1A) is_found = bool(resp[0]) frequency = struct.unpack(" None: + def find_rf_packet(self, frequency: Optional[float] = None) -> None: """Enter radiofrequency learning mode.""" payload = bytearray() if frequency: diff --git a/broadlink/switch.py b/broadlink/switch.py index a49cb1c0..8393f6b1 100644 --- a/broadlink/switch.py +++ b/broadlink/switch.py @@ -1,6 +1,7 @@ """Support for switches.""" import json import struct +from typing import Optional from . import exceptions as e from .device import Device @@ -127,12 +128,12 @@ def set_nightlight(self, ntlight: bool) -> None: def set_state( self, - pwr: bool | None = None, - ntlight: bool | None = None, - indicator: bool | None = None, - ntlbrightness: int | None = None, - maxworktime: int | None = None, - childlock: bool | None = None, + pwr: Optional[bool] = None, + ntlight: Optional[bool] = None, + indicator: Optional[bool] = None, + ntlbrightness: Optional[int] = None, + maxworktime: Optional[int] = None, + childlock: Optional[bool] = None, ) -> dict: """Set state of device.""" state = {} @@ -255,13 +256,13 @@ def get_state(self) -> dict: def set_state( self, - pwr: bool | None = None, - pwr1: bool | None = None, - pwr2: bool | None = None, - maxworktime: int | None = None, - maxworktime1: int | None = None, - maxworktime2: int | None = None, - idcbrightness: int | None = None, + pwr: Optional[bool] = None, + pwr1: Optional[bool] = None, + pwr2: Optional[bool] = None, + maxworktime: Optional[int] = None, + maxworktime1: Optional[int] = None, + maxworktime2: Optional[int] = None, + idcbrightness: Optional[int] = None, ) -> dict: """Set the power state of the device.""" state = {} @@ -322,19 +323,19 @@ class ehc31(bg1): def set_state( self, - pwr: bool | None = None, - pwr1: bool | None = None, - pwr2: bool | None = None, - pwr3: bool | None = None, - maxworktime1: int | None = None, - maxworktime2: int | None = None, - maxworktime3: int | None = None, - idcbrightness: int | None = None, - childlock: bool | None = None, - childlock1: bool | None = None, - childlock2: bool | None = None, - childlock3: bool | None = None, - childlock4: bool | None = None, + pwr: Optional[bool] = None, + pwr1: Optional[bool] = None, + pwr2: Optional[bool] = None, + pwr3: Optional[bool] = None, + maxworktime1: Optional[int] = None, + maxworktime2: Optional[int] = None, + maxworktime3: Optional[int] = None, + idcbrightness: Optional[int] = None, + childlock: Optional[bool] = None, + childlock1: Optional[bool] = None, + childlock2: Optional[bool] = None, + childlock3: Optional[bool] = None, + childlock4: Optional[bool] = None, ) -> dict: """Set the power state of the device.""" state = {} diff --git a/cli/broadlink_cli b/cli/broadlink_cli index 35317ee4..7913e332 100755 --- a/cli/broadlink_cli +++ b/cli/broadlink_cli @@ -2,7 +2,7 @@ import argparse import base64 import time -import typing as t +from typing import List import broadlink from broadlink.const import DEFAULT_PORT @@ -16,7 +16,7 @@ def auto_int(x): return int(x, 0) -def format_pulses(pulses: t.List[int]) -> str: +def format_pulses(pulses: List[int]) -> str: """Format pulses.""" return " ".join( f"+{pulse}" if i % 2 == 0 else f"-{pulse}" @@ -24,7 +24,7 @@ def format_pulses(pulses: t.List[int]) -> str: ) -def parse_pulses(data: t.List[str]) -> t.List[int]: +def parse_pulses(data: List[str]) -> List[int]: """Parse pulses.""" return [abs(int(s)) for s in data]