Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make type hints compatible with Python 3.6 #797

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions broadlink/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions broadlink/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 = "",
Expand Down
8 changes: 4 additions & 4 deletions broadlink/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Helper functions and classes."""
import typing as t
from typing import Dict, List, Sequence


class CRC16:
Expand All @@ -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]
Expand All @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions broadlink/hub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Support for hubs."""
import struct
import json
from typing import Optional

from . import exceptions as e
from .device import Device
Expand Down Expand Up @@ -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:
Expand All @@ -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 = {}
Expand Down
55 changes: 28 additions & 27 deletions broadlink/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import enum
import json
import struct
from typing import Optional

from . import exceptions as e
from .device import Device
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand Down
10 changes: 5 additions & 5 deletions broadlink/remote.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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("<I", resp[1:5])[0] / 1000.0
return is_found, frequency

def find_rf_packet(self, frequency: float | None = None) -> None:
def find_rf_packet(self, frequency: Optional[float] = None) -> None:
"""Enter radiofrequency learning mode."""
payload = bytearray()
if frequency:
Expand Down
53 changes: 27 additions & 26 deletions broadlink/switch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Support for switches."""
import json
import struct
from typing import Optional

from . import exceptions as e
from .device import Device
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand Down
6 changes: 3 additions & 3 deletions cli/broadlink_cli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,15 +16,15 @@ 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}"
for i, pulse in enumerate(pulses)
)


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]

Expand Down
Loading