diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1b9fadc..43d1385 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,7 +24,7 @@ repos: name: pylint (library code) types: [python] args: - - --disable=consider-using-f-string + - --disable=consider-using-f-string,duplicate-code exclude: "^(docs/|examples/|tests/|setup.py$)" - id: pylint name: pylint (example code) diff --git a/adafruit_funhouse/__init__.py b/adafruit_funhouse/__init__.py index d237d98..8830bf1 100755 --- a/adafruit_funhouse/__init__.py +++ b/adafruit_funhouse/__init__.py @@ -34,6 +34,12 @@ from adafruit_funhouse.graphics import Graphics from adafruit_funhouse.peripherals import Peripherals +try: + from typing import Optional, Dict, Union, Callable, Sequence, List + from adafruit_dotstar import DotStar +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FunHouse.git" @@ -65,17 +71,17 @@ class FunHouse(PortalBase): def __init__( self, *, - url=None, - headers=None, - json_path=None, - regexp_path=None, - default_bg=0, - status_dotstar=None, - json_transform=None, - rotation=270, - scale=1, - debug=False, - ): + url: Optional[str] = None, + headers: Dict[str, str] = None, + json_path: Optional[Union[List[str], List[List[str]]]] = None, + regexp_path: Optional[Sequence[str]] = None, + default_bg: int = 0, + status_dotstar: Optional[DotStar] = None, + json_transform: Optional[Union[Callable, List[Callable]]] = None, + rotation: int = 270, + scale: int = 1, + debug: bool = False, + ) -> None: network = Network( status_dotstar=status_dotstar, @@ -105,7 +111,7 @@ def __init__( gc.collect() - def enter_light_sleep(self, sleep_time): + def enter_light_sleep(self, sleep_time: float) -> None: """ Enter light sleep and resume the program after a certain period of time. diff --git a/adafruit_funhouse/graphics.py b/adafruit_funhouse/graphics.py index a44716f..24ea359 100755 --- a/adafruit_funhouse/graphics.py +++ b/adafruit_funhouse/graphics.py @@ -45,7 +45,14 @@ class Graphics(GraphicsBase): """ # pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements, too-few-public-methods - def __init__(self, *, default_bg=0, rotation=270, scale=1, debug=False): + def __init__( + self, + *, + default_bg: int = 0, + rotation: int = 270, + scale: int = 1, + debug: bool = False + ) -> None: self._debug = debug self.display = board.DISPLAY self.display.rotation = rotation diff --git a/adafruit_funhouse/network.py b/adafruit_funhouse/network.py index d97100e..5ad14c9 100755 --- a/adafruit_funhouse/network.py +++ b/adafruit_funhouse/network.py @@ -33,6 +33,12 @@ from adafruit_portalbase.network import NetworkBase from adafruit_portalbase.wifi_esp32s2 import WiFi +try: + from typing import Optional, Union, Callable + from adafruit_dotstar import DotStar +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FunHouse.git" @@ -54,10 +60,10 @@ class Network(NetworkBase): def __init__( self, *, - status_dotstar=None, - extract_values=True, - debug=False, - ): + status_dotstar: Optional[DotStar] = None, + extract_values: bool = True, + debug: bool = False, + ) -> None: super().__init__( WiFi(status_led=status_dotstar), extract_values=extract_values, @@ -65,7 +71,7 @@ def __init__( ) self._mqtt_client = None - def init_io_mqtt(self): + def init_io_mqtt(self) -> IO_MQTT: """Initialize MQTT for Adafruit IO""" try: aio_username = self._secrets["aio_username"] @@ -80,12 +86,12 @@ def init_io_mqtt(self): # pylint: disable=too-many-arguments def init_mqtt( self, - broker, - port=8883, - username=None, - password=None, - use_io=False, - ): + broker: str, + port: int = 8883, + username: str = None, + password: str = None, + use_io: bool = False, + ) -> Union[MQTT.MQTT, IO_MQTT]: """Initialize MQTT""" self.connect() self._mqtt_client = MQTT.MQTT( @@ -103,12 +109,14 @@ def init_mqtt( # pylint: enable=too-many-arguments - def _get_mqtt_client(self): + def _get_mqtt_client(self) -> Union[MQTT.MQTT, IO_MQTT]: if self._mqtt_client is not None: return self._mqtt_client raise RuntimeError("Please initialize MQTT before using") - def mqtt_loop(self, *args, suppress_mqtt_errors=True, **kwargs): + def mqtt_loop( + self, *args: int, suppress_mqtt_errors: bool = True, **kwargs: int + ) -> None: """Run the MQTT Loop""" self._get_mqtt_client() if suppress_mqtt_errors: @@ -123,7 +131,12 @@ def mqtt_loop(self, *args, suppress_mqtt_errors=True, **kwargs): if self._mqtt_client is not None: self._mqtt_client.loop(*args, **kwargs) - def mqtt_publish(self, *args, suppress_mqtt_errors=True, **kwargs): + def mqtt_publish( + self, + *args: Union[str, int, float], + suppress_mqtt_errors: bool = True, + **kwargs: Union[str, int, float] + ) -> None: """Publish to MQTT""" self._get_mqtt_client() if suppress_mqtt_errors: @@ -136,14 +149,16 @@ def mqtt_publish(self, *args, suppress_mqtt_errors=True, **kwargs): if self._mqtt_client is not None: self._mqtt_client.publish(*args, **kwargs) - def mqtt_connect(self, *args, **kwargs): + def mqtt_connect( + self, *args: Union[bool, str, int], **kwargs: Union[bool, str, int] + ) -> None: """Connect to MQTT""" self._get_mqtt_client() if self._mqtt_client is not None: self._mqtt_client.connect(*args, **kwargs) @property - def on_mqtt_connect(self): + def on_mqtt_connect(self) -> Optional[Callable]: """ Get or Set the MQTT Connect Handler @@ -153,12 +168,12 @@ def on_mqtt_connect(self): return None @on_mqtt_connect.setter - def on_mqtt_connect(self, value): + def on_mqtt_connect(self, value: Callable) -> None: self._get_mqtt_client() self._mqtt_client.on_connect = value @property - def on_mqtt_disconnect(self): + def on_mqtt_disconnect(self) -> Optional[Callable]: """ Get or Set the MQTT Disconnect Handler @@ -168,11 +183,11 @@ def on_mqtt_disconnect(self): return None @on_mqtt_disconnect.setter - def on_mqtt_disconnect(self, value): + def on_mqtt_disconnect(self, value: Callable) -> None: self._get_mqtt_client().on_disconnect = value @property - def on_mqtt_subscribe(self): + def on_mqtt_subscribe(self) -> Optional[Callable]: """ Get or Set the MQTT Subscribe Handler @@ -182,11 +197,11 @@ def on_mqtt_subscribe(self): return None @on_mqtt_subscribe.setter - def on_mqtt_subscribe(self, value): + def on_mqtt_subscribe(self, value: Callable) -> None: self._get_mqtt_client().on_subscribe = value @property - def on_mqtt_unsubscribe(self): + def on_mqtt_unsubscribe(self) -> Optional[Callable]: """ Get or Set the MQTT Unsubscribe Handler @@ -196,11 +211,11 @@ def on_mqtt_unsubscribe(self): return None @on_mqtt_unsubscribe.setter - def on_mqtt_unsubscribe(self, value): + def on_mqtt_unsubscribe(self, value: Callable) -> None: self._get_mqtt_client().on_unsubscribe = value @property - def on_mqtt_message(self): + def on_mqtt_message(self) -> Optional[Callable]: """ Get or Set the MQTT Message Handler @@ -210,11 +225,11 @@ def on_mqtt_message(self): return None @on_mqtt_message.setter - def on_mqtt_message(self, value): + def on_mqtt_message(self, value: Callable) -> None: self._get_mqtt_client().on_message = value @property - def enabled(self): + def enabled(self) -> bool: """ Get or Set whether the WiFi is enabled @@ -222,5 +237,5 @@ def enabled(self): return self._wifi.enabled @enabled.setter - def enabled(self, value): + def enabled(self, value: bool) -> None: self._wifi.enabled = bool(value) diff --git a/adafruit_funhouse/peripherals.py b/adafruit_funhouse/peripherals.py index 3ea876a..5ff36ae 100755 --- a/adafruit_funhouse/peripherals.py +++ b/adafruit_funhouse/peripherals.py @@ -27,6 +27,7 @@ """ +from typing import Optional import board from digitalio import DigitalInOut, Direction, Pull from analogio import AnalogIn @@ -44,7 +45,7 @@ class Peripherals: """Peripherals Helper Class for the FunHouse Library""" # pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements - def __init__(self): + def __init__(self) -> None: # Dotstars self.dotstars = adafruit_dotstar.DotStar( board.DOTSTAR_CLOCK, board.DOTSTAR_DATA, 5, brightness=0.3 @@ -90,7 +91,7 @@ def __init__(self): self._pir.direction = Direction.INPUT @staticmethod - def play_tone(frequency, duration): + def play_tone(frequency: float, duration: float) -> None: """Automatically Enable/Disable the speaker and play a tone at the specified frequency for the specified duration It will attempt to play the sound up to 3 times in the case of @@ -108,12 +109,12 @@ def play_tone(frequency, duration): pass attempt += 1 - def set_dotstars(self, *values): + def set_dotstars(self, *values: int) -> None: """Set the dotstar values to the provided values""" for i, value in enumerate(values[: len(self.dotstars)]): self.dotstars[i] = value - def deinit(self): + def deinit(self) -> None: """Call deinit on all resources to free them""" self.dotstars.deinit() for button in self._buttons: @@ -125,56 +126,56 @@ def deinit(self): self._pir.deinit() @property - def button_down(self): + def button_down(self) -> bool: """ Return whether Down Button is pressed """ return self._buttons[0].value @property - def button_sel(self): + def button_sel(self) -> bool: """ Return whether Sel Button is pressed """ return self._buttons[1].value @property - def button_up(self): + def button_up(self) -> bool: """ Return whether Up Button is pressed """ return self._buttons[2].value @property - def any_button_pressed(self): + def any_button_pressed(self) -> bool: """ Return whether any button is pressed """ return True in [button.value for button in enumerate(self._buttons)] @property - def captouch6(self): + def captouch6(self) -> bool: """ Return whether CT6 Touch Pad is touched """ return self._ctp[0].value @property - def captouch7(self): + def captouch7(self) -> bool: """ Return whether CT7 Touch Pad is touched """ return self._ctp[1].value @property - def captouch8(self): + def captouch8(self) -> bool: """ Return whether CT8 Touch Pad is touched """ return self._ctp[2].value @property - def slider(self): + def slider(self) -> Optional[float]: """ Return the slider position value in the range of 0.0-1.0 or None if not touched """ @@ -186,7 +187,7 @@ def slider(self): return cap_map.index(val) / 8 if val in cap_map else None @property - def light(self): + def light(self) -> int: """ Return the value of the light sensor. The neopixel_disable property must be false to get a value. @@ -206,39 +207,39 @@ def light(self): return self._light.value @property - def temperature(self): + def temperature(self) -> float: """ Return the temperature in degrees Celsius """ return self._aht20.temperature @property - def relative_humidity(self): + def relative_humidity(self) -> float: """ Return the relative humidity as a percentage (0 - 100) """ return self._aht20.relative_humidity @property - def pressure(self): + def pressure(self) -> float: """ Return the barometric pressure in hPa, or equivalently in mBar """ return self._dps310.pressure @property - def led(self): + def led(self) -> bool: """ Return or set the value of the LED """ return self._led.value @led.setter - def led(self, value): + def led(self, value: bool) -> None: self._led.value = bool(value) @property - def pir_sensor(self): + def pir_sensor(self) -> bool: """ Return the value of the PIR Sensor """