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 #!/usr/bin/env python3
"""The python-broadlink library.""" """The python-broadlink library."""
import socket import socket
import typing as t from typing import Generator, List, Optional, Tuple, Union
from . import exceptions as e from . import exceptions as e
from .const import DEFAULT_BCAST_ADDR, DEFAULT_PORT, DEFAULT_TIMEOUT from .const import DEFAULT_BCAST_ADDR, DEFAULT_PORT, DEFAULT_TIMEOUT
@ -212,8 +212,8 @@ SUPPORTED_TYPES = {
def gendevice( def gendevice(
dev_type: int, dev_type: int,
host: t.Tuple[str, int], host: Tuple[str, int],
mac: t.Union[bytes, str], mac: Union[bytes, str],
name: str = "", name: str = "",
is_locked: bool = False, is_locked: bool = False,
) -> Device: ) -> Device:
@ -265,10 +265,10 @@ def hello(
def discover( def discover(
timeout: int = DEFAULT_TIMEOUT, timeout: int = DEFAULT_TIMEOUT,
local_ip_address: str = None, local_ip_address: Optional[str] = None,
discover_ip_address: str = DEFAULT_BCAST_ADDR, discover_ip_address: str = DEFAULT_BCAST_ADDR,
discover_ip_port: int = DEFAULT_PORT, discover_ip_port: int = DEFAULT_PORT,
) -> t.List[Device]: ) -> List[Device]:
"""Discover devices connected to the local network.""" """Discover devices connected to the local network."""
responses = scan( responses = scan(
timeout, local_ip_address, discover_ip_address, discover_ip_port timeout, local_ip_address, discover_ip_address, discover_ip_port
@ -278,10 +278,10 @@ def discover(
def xdiscover( def xdiscover(
timeout: int = DEFAULT_TIMEOUT, 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_address: str = DEFAULT_BCAST_ADDR,
discover_ip_port: int = DEFAULT_PORT, discover_ip_port: int = DEFAULT_PORT,
) -> t.Generator[Device, None, None]: ) -> Generator[Device, None, None]:
"""Discover devices connected to the local network. """Discover devices connected to the local network.
This function returns a generator that yields devices instantly. This function returns a generator that yields devices instantly.

View File

@ -3,7 +3,7 @@ import socket
import threading import threading
import random import random
import time import time
import typing as t from typing import Generator, Optional, Tuple, Union
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
@ -17,15 +17,15 @@ from .const import (
) )
from .protocol import Datetime 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( def scan(
timeout: int = DEFAULT_TIMEOUT, 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_address: str = DEFAULT_BCAST_ADDR,
discover_ip_port: int = DEFAULT_PORT, discover_ip_port: int = DEFAULT_PORT,
) -> t.Generator[HelloResponse, None, None]: ) -> Generator[HelloResponse, None, None]:
"""Broadcast a hello message and yield responses.""" """Broadcast a hello message and yield responses."""
conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@ -100,8 +100,8 @@ class Device:
def __init__( def __init__(
self, self,
host: t.Tuple[str, int], host: Tuple[str, int],
mac: t.Union[bytes, str], mac: Union[bytes, str],
devtype: int, devtype: int,
timeout: int = DEFAULT_TIMEOUT, timeout: int = DEFAULT_TIMEOUT,
name: str = "", name: str = "",

View File

@ -1,5 +1,5 @@
"""Helper functions and classes.""" """Helper functions and classes."""
import typing as t from typing import Dict, List, Sequence
class CRC16: class CRC16:
@ -8,10 +8,10 @@ class CRC16:
CRC tables are cached for performance. CRC tables are cached for performance.
""" """
_cache: t.Dict[int, t.List[int]] = {} _cache: Dict[int, List[int]] = {}
@classmethod @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.""" """Return the CRC-16 table for a polynomial."""
try: try:
crc_table = cls._cache[polynomial] crc_table = cls._cache[polynomial]
@ -31,7 +31,7 @@ class CRC16:
@classmethod @classmethod
def calculate( def calculate(
cls, cls,
sequence: t.Sequence[int], sequence: Sequence[int],
polynomial: int = 0xA001, # CRC-16-ANSI. polynomial: int = 0xA001, # CRC-16-ANSI.
init_value: int = 0xFFFF, init_value: int = 0xFFFF,
) -> int: ) -> int:

View File

@ -1,6 +1,7 @@
"""Support for hubs.""" """Support for hubs."""
import struct import struct
import json import json
from typing import Optional
from . import exceptions as e from . import exceptions as e
from .device import Device from .device import Device
@ -42,7 +43,7 @@ class s3(Device):
return sub_devices 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.""" """Return the power state of the device."""
state = {} state = {}
if did is not None: if did is not None:
@ -55,10 +56,10 @@ class s3(Device):
def set_state( def set_state(
self, self,
did: str | None = None, did: Optional[str] = None,
pwr1: bool | None = None, pwr1: Optional[bool] = None,
pwr2: bool | None = None, pwr2: Optional[bool] = None,
pwr3: bool | None = None, pwr3: Optional[bool] = None,
) -> dict: ) -> dict:
"""Set the power state of the device.""" """Set the power state of the device."""
state = {} state = {}

View File

@ -2,6 +2,7 @@
import enum import enum
import json import json
import struct import struct
from typing import Optional
from . import exceptions as e from . import exceptions as e
from .device import Device from .device import Device
@ -32,20 +33,20 @@ class lb1(Device):
def set_state( def set_state(
self, self,
pwr: bool | None = None, pwr: Optional[bool] = None,
red: int | None = None, red: Optional[int] = None,
blue: int | None = None, blue: Optional[int] = None,
green: int | None = None, green: Optional[int] = None,
brightness: int | None = None, brightness: Optional[int] = None,
colortemp: int | None = None, colortemp: Optional[int] = None,
hue: int | None = None, hue: Optional[int] = None,
saturation: int | None = None, saturation: Optional[int] = None,
transitionduration: int | None = None, transitionduration: Optional[int] = None,
maxworktime: int | None = None, maxworktime: Optional[int] = None,
bulb_colormode: int | None = None, bulb_colormode: Optional[int] = None,
bulb_scenes: str | None = None, bulb_scenes: Optional[str] = None,
bulb_scene: str | None = None, bulb_scene: Optional[str] = None,
bulb_sceneidx: int | None = None, bulb_sceneidx: Optional[int] = None,
) -> dict: ) -> dict:
"""Set the power state of the device.""" """Set the power state of the device."""
state = {} state = {}
@ -130,19 +131,19 @@ class lb2(Device):
def set_state( def set_state(
self, self,
pwr: bool | None = None, pwr: Optional[bool] = None,
red: int | None = None, red: Optional[int] = None,
blue: int | None = None, blue: Optional[int] = None,
green: int | None = None, green: Optional[int] = None,
brightness: int | None = None, brightness: Optional[int] = None,
colortemp: int | None = None, colortemp: Optional[int] = None,
hue: int | None = None, hue: Optional[int] = None,
saturation: int | None = None, saturation: Optional[int] = None,
transitionduration: int | None = None, transitionduration: Optional[int] = None,
maxworktime: int | None = None, maxworktime: Optional[int] = None,
bulb_colormode: int | None = None, bulb_colormode: Optional[int] = None,
bulb_scenes: str | None = None, bulb_scenes: Optional[str] = None,
bulb_scene: str | None = None, bulb_scene: Optional[str] = None,
) -> dict: ) -> dict:
"""Set the power state of the device.""" """Set the power state of the device."""
state = {} state = {}

View File

@ -1,12 +1,12 @@
"""Support for universal remotes.""" """Support for universal remotes."""
import struct import struct
import typing as t from typing import List, Optional, Tuple
from . import exceptions as e from . import exceptions as e
from .device import Device 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.""" """Convert a microsecond duration sequence into a Broadlink IR packet."""
result = bytearray(4) result = bytearray(4)
result[0x00] = 0x26 result[0x00] = 0x26
@ -25,7 +25,7 @@ def pulses_to_data(pulses: t.List[int], tick: float = 32.84) -> bytes:
return result 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.""" """Parse a Broadlink packet into a microsecond duration sequence."""
result = [] result = []
index = 4 index = 4
@ -88,14 +88,14 @@ class rmpro(rmmini):
"""Sweep frequency.""" """Sweep frequency."""
self._send(0x19) 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.""" """Return True if the frequency was identified successfully."""
resp = self._send(0x1A) resp = self._send(0x1A)
is_found = bool(resp[0]) is_found = bool(resp[0])
frequency = struct.unpack("<I", resp[1:5])[0] / 1000.0 frequency = struct.unpack("<I", resp[1:5])[0] / 1000.0
return is_found, frequency 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.""" """Enter radiofrequency learning mode."""
payload = bytearray() payload = bytearray()
if frequency: if frequency:

View File

@ -1,6 +1,7 @@
"""Support for switches.""" """Support for switches."""
import json import json
import struct import struct
from typing import Optional
from . import exceptions as e from . import exceptions as e
from .device import Device from .device import Device
@ -127,12 +128,12 @@ class sp4(Device):
def set_state( def set_state(
self, self,
pwr: bool | None = None, pwr: Optional[bool] = None,
ntlight: bool | None = None, ntlight: Optional[bool] = None,
indicator: bool | None = None, indicator: Optional[bool] = None,
ntlbrightness: int | None = None, ntlbrightness: Optional[int] = None,
maxworktime: int | None = None, maxworktime: Optional[int] = None,
childlock: bool | None = None, childlock: Optional[bool] = None,
) -> dict: ) -> dict:
"""Set state of device.""" """Set state of device."""
state = {} state = {}
@ -255,13 +256,13 @@ class bg1(Device):
def set_state( def set_state(
self, self,
pwr: bool | None = None, pwr: Optional[bool] = None,
pwr1: bool | None = None, pwr1: Optional[bool] = None,
pwr2: bool | None = None, pwr2: Optional[bool] = None,
maxworktime: int | None = None, maxworktime: Optional[int] = None,
maxworktime1: int | None = None, maxworktime1: Optional[int] = None,
maxworktime2: int | None = None, maxworktime2: Optional[int] = None,
idcbrightness: int | None = None, idcbrightness: Optional[int] = None,
) -> dict: ) -> dict:
"""Set the power state of the device.""" """Set the power state of the device."""
state = {} state = {}
@ -322,19 +323,19 @@ class ehc31(bg1):
def set_state( def set_state(
self, self,
pwr: bool | None = None, pwr: Optional[bool] = None,
pwr1: bool | None = None, pwr1: Optional[bool] = None,
pwr2: bool | None = None, pwr2: Optional[bool] = None,
pwr3: bool | None = None, pwr3: Optional[bool] = None,
maxworktime1: int | None = None, maxworktime1: Optional[int] = None,
maxworktime2: int | None = None, maxworktime2: Optional[int] = None,
maxworktime3: int | None = None, maxworktime3: Optional[int] = None,
idcbrightness: int | None = None, idcbrightness: Optional[int] = None,
childlock: bool | None = None, childlock: Optional[bool] = None,
childlock1: bool | None = None, childlock1: Optional[bool] = None,
childlock2: bool | None = None, childlock2: Optional[bool] = None,
childlock3: bool | None = None, childlock3: Optional[bool] = None,
childlock4: bool | None = None, childlock4: Optional[bool] = None,
) -> dict: ) -> dict:
"""Set the power state of the device.""" """Set the power state of the device."""
state = {} state = {}

View File

@ -2,7 +2,7 @@
import argparse import argparse
import base64 import base64
import time import time
import typing as t from typing import List
import broadlink import broadlink
from broadlink.const import DEFAULT_PORT from broadlink.const import DEFAULT_PORT
@ -16,7 +16,7 @@ def auto_int(x):
return int(x, 0) return int(x, 0)
def format_pulses(pulses: t.List[int]) -> str: def format_pulses(pulses: List[int]) -> str:
"""Format pulses.""" """Format pulses."""
return " ".join( return " ".join(
f"+{pulse}" if i % 2 == 0 else f"-{pulse}" 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.""" """Parse pulses."""
return [abs(int(s)) for s in data] return [abs(int(s)) for s in data]