1
0
Fork 0

Make type hints compatible with Python 3.6 (#797)

This commit is contained in:
Felipe Martins Diel 2024-04-17 03:20:13 -03:00 committed by GitHub
parent eb56e7a46f
commit 0a9acab2b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 86 additions and 83 deletions

View File

@ -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 @@ SUPPORTED_TYPES = {
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.

View File

@ -3,7 +3,7 @@ import socket
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 .const import (
)
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 = "",

View File

@ -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 @@ class CRC16:
@classmethod
def calculate(
cls,
sequence: t.Sequence[int],
sequence: Sequence[int],
polynomial: int = 0xA001, # CRC-16-ANSI.
init_value: int = 0xFFFF,
) -> int:

View File

@ -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 @@ class s3(Device):
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 @@ class s3(Device):
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 = {}

View File

@ -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 @@ class lb1(Device):
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 @@ class lb2(Device):
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 = {}

View File

@ -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 @@ class rmpro(rmmini):
"""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:

View File

@ -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 @@ class sp4(Device):
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 @@ class bg1(Device):
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 = {}

View File

@ -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]