Fix type issues [firmata] (#67093)

This commit is contained in:
Marc Mueller 2022-02-23 08:57:06 +01:00 committed by GitHub
parent 636d791b37
commit c11663344d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 49 deletions

View file

@ -190,7 +190,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={}, connections=set(),
identifiers={(DOMAIN, board.name)}, identifiers={(DOMAIN, board.name)},
manufacturer=FIRMATA_MANUFACTURER, manufacturer=FIRMATA_MANUFACTURER,
name=board.name, name=board.name,

View file

@ -1,6 +1,9 @@
"""Code to handle a Firmata board.""" """Code to handle a Firmata board."""
from __future__ import annotations
from collections.abc import Mapping
import logging import logging
from typing import Union from typing import Literal, Union
from pymata_express.pymata_express import PymataExpress from pymata_express.pymata_express import PymataExpress
from pymata_express.pymata_express_serial import serial from pymata_express.pymata_express_serial import serial
@ -32,18 +35,18 @@ FirmataPinType = Union[int, str]
class FirmataBoard: class FirmataBoard:
"""Manages a single Firmata board.""" """Manages a single Firmata board."""
def __init__(self, config: dict) -> None: def __init__(self, config: Mapping) -> None:
"""Initialize the board.""" """Initialize the board."""
self.config = config self.config = config
self.api = None self.api: PymataExpress = None
self.firmware_version = None self.firmware_version: str | None = None
self.protocol_version = None self.protocol_version = None
self.name = self.config[CONF_NAME] self.name = self.config[CONF_NAME]
self.switches = [] self.switches = []
self.lights = [] self.lights = []
self.binary_sensors = [] self.binary_sensors = []
self.sensors = [] self.sensors = []
self.used_pins = [] self.used_pins: list[FirmataPinType] = []
if CONF_SWITCHES in self.config: if CONF_SWITCHES in self.config:
self.switches = self.config[CONF_SWITCHES] self.switches = self.config[CONF_SWITCHES]
@ -118,8 +121,10 @@ board %s: %s",
self.used_pins.append(pin) self.used_pins.append(pin)
return True return True
def get_pin_type(self, pin: FirmataPinType) -> tuple: def get_pin_type(self, pin: FirmataPinType) -> tuple[Literal[0, 1], int]:
"""Return the type and Firmata location of a pin on the board.""" """Return the type and Firmata location of a pin on the board."""
pin_type: Literal[0, 1]
firmata_pin: int
if isinstance(pin, str): if isinstance(pin, str):
pin_type = PIN_TYPE_ANALOG pin_type = PIN_TYPE_ANALOG
firmata_pin = int(pin[1:]) firmata_pin = int(pin[1:])
@ -130,7 +135,7 @@ board %s: %s",
return (pin_type, firmata_pin) return (pin_type, firmata_pin)
async def get_board(data: dict) -> PymataExpress: async def get_board(data: Mapping) -> PymataExpress:
"""Create a Pymata board object.""" """Create a Pymata board object."""
board_data = {} board_data = {}

View file

@ -1,4 +1,6 @@
"""Constants for the Firmata component.""" """Constants for the Firmata component."""
from typing import Final
from homeassistant.const import ( from homeassistant.const import (
CONF_BINARY_SENSORS, CONF_BINARY_SENSORS,
CONF_LIGHTS, CONF_LIGHTS,
@ -19,8 +21,8 @@ PIN_MODE_OUTPUT = "OUTPUT"
PIN_MODE_PWM = "PWM" PIN_MODE_PWM = "PWM"
PIN_MODE_INPUT = "INPUT" PIN_MODE_INPUT = "INPUT"
PIN_MODE_PULLUP = "PULLUP" PIN_MODE_PULLUP = "PULLUP"
PIN_TYPE_ANALOG = 1 PIN_TYPE_ANALOG: Final = 1
PIN_TYPE_DIGITAL = 0 PIN_TYPE_DIGITAL: Final = 0
CONF_SAMPLING_INTERVAL = "sampling_interval" CONF_SAMPLING_INTERVAL = "sampling_interval"
CONF_SERIAL_BAUD_RATE = "serial_baud_rate" CONF_SERIAL_BAUD_RATE = "serial_baud_rate"
CONF_SERIAL_PORT = "serial_port" CONF_SERIAL_PORT = "serial_port"

View file

@ -20,7 +20,7 @@ class FirmataEntity:
def device_info(self) -> DeviceInfo: def device_info(self) -> DeviceInfo:
"""Return device info.""" """Return device info."""
return DeviceInfo( return DeviceInfo(
connections={}, connections=set(),
identifiers={(DOMAIN, self._api.board.name)}, identifiers={(DOMAIN, self._api.board.name)},
manufacturer=FIRMATA_MANUFACTURER, manufacturer=FIRMATA_MANUFACTURER,
name=self._api.board.name, name=self._api.board.name,
@ -33,7 +33,7 @@ class FirmataPinEntity(FirmataEntity):
def __init__( def __init__(
self, self,
api: type[FirmataBoardPin], api: FirmataBoardPin,
config_entry: ConfigEntry, config_entry: ConfigEntry,
name: str, name: str,
pin: FirmataPinType, pin: FirmataPinType,

View file

@ -58,7 +58,7 @@ class FirmataLight(FirmataPinEntity, LightEntity):
def __init__( def __init__(
self, self,
api: type[FirmataBoardPin], api: FirmataBoardPin,
config_entry: ConfigEntry, config_entry: ConfigEntry,
name: str, name: str,
pin: FirmataPinType, pin: FirmataPinType,

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
import logging import logging
from typing import cast
from .board import FirmataBoard, FirmataPinType from .board import FirmataBoard, FirmataPinType
from .const import PIN_MODE_INPUT, PIN_MODE_PULLUP, PIN_TYPE_ANALOG from .const import PIN_MODE_INPUT, PIN_MODE_PULLUP, PIN_TYPE_ANALOG
@ -23,11 +24,12 @@ class FirmataBoardPin:
self._pin = pin self._pin = pin
self._pin_mode = pin_mode self._pin_mode = pin_mode
self._pin_type, self._firmata_pin = self.board.get_pin_type(self._pin) self._pin_type, self._firmata_pin = self.board.get_pin_type(self._pin)
self._state = None self._state: bool | int | None = None
self._analog_pin: int | None = None
if self._pin_type == PIN_TYPE_ANALOG: if self._pin_type == PIN_TYPE_ANALOG:
# Pymata wants the analog pin formatted as the # from "A#" # Pymata wants the analog pin formatted as the # from "A#"
self._analog_pin = int(self._pin[1:]) self._analog_pin = int(cast(str, self._pin)[1:])
def setup(self): def setup(self):
"""Set up a pin and make sure it is valid.""" """Set up a pin and make sure it is valid."""
@ -38,6 +40,8 @@ class FirmataBoardPin:
class FirmataBinaryDigitalOutput(FirmataBoardPin): class FirmataBinaryDigitalOutput(FirmataBoardPin):
"""Representation of a Firmata Digital Output Pin.""" """Representation of a Firmata Digital Output Pin."""
_state: bool
def __init__( def __init__(
self, self,
board: FirmataBoard, board: FirmataBoard,
@ -92,6 +96,8 @@ class FirmataBinaryDigitalOutput(FirmataBoardPin):
class FirmataPWMOutput(FirmataBoardPin): class FirmataPWMOutput(FirmataBoardPin):
"""Representation of a Firmata PWM/analog Output Pin.""" """Representation of a Firmata PWM/analog Output Pin."""
_state: int
def __init__( def __init__(
self, self,
board: FirmataBoard, board: FirmataBoard,
@ -139,12 +145,14 @@ class FirmataPWMOutput(FirmataBoardPin):
class FirmataBinaryDigitalInput(FirmataBoardPin): class FirmataBinaryDigitalInput(FirmataBoardPin):
"""Representation of a Firmata Digital Input Pin.""" """Representation of a Firmata Digital Input Pin."""
_state: bool
def __init__( def __init__(
self, board: FirmataBoard, pin: FirmataPinType, pin_mode: str, negate: bool self, board: FirmataBoard, pin: FirmataPinType, pin_mode: str, negate: bool
) -> None: ) -> None:
"""Initialize the digital input pin.""" """Initialize the digital input pin."""
self._negate = negate self._negate = negate
self._forward_callback = None self._forward_callback: Callable[[], None]
super().__init__(board, pin, pin_mode) super().__init__(board, pin, pin_mode)
async def start_pin(self, forward_callback: Callable[[], None]) -> None: async def start_pin(self, forward_callback: Callable[[], None]) -> None:
@ -206,12 +214,15 @@ class FirmataBinaryDigitalInput(FirmataBoardPin):
class FirmataAnalogInput(FirmataBoardPin): class FirmataAnalogInput(FirmataBoardPin):
"""Representation of a Firmata Analog Input Pin.""" """Representation of a Firmata Analog Input Pin."""
_analog_pin: int
_state: int
def __init__( def __init__(
self, board: FirmataBoard, pin: FirmataPinType, pin_mode: str, differential: int self, board: FirmataBoard, pin: FirmataPinType, pin_mode: str, differential: int
) -> None: ) -> None:
"""Initialize the analog input pin.""" """Initialize the analog input pin."""
self._differential = differential self._differential = differential
self._forward_callback = None self._forward_callback: Callable[[], None]
super().__init__(board, pin, pin_mode) super().__init__(board, pin, pin_mode)
async def start_pin(self, forward_callback: Callable[[], None]) -> None: async def start_pin(self, forward_callback: Callable[[], None]) -> None:

View file

@ -2273,30 +2273,6 @@ ignore_errors = true
[mypy-homeassistant.components.fireservicerota.switch] [mypy-homeassistant.components.fireservicerota.switch]
ignore_errors = true ignore_errors = true
[mypy-homeassistant.components.firmata]
ignore_errors = true
[mypy-homeassistant.components.firmata.binary_sensor]
ignore_errors = true
[mypy-homeassistant.components.firmata.board]
ignore_errors = true
[mypy-homeassistant.components.firmata.entity]
ignore_errors = true
[mypy-homeassistant.components.firmata.light]
ignore_errors = true
[mypy-homeassistant.components.firmata.pin]
ignore_errors = true
[mypy-homeassistant.components.firmata.sensor]
ignore_errors = true
[mypy-homeassistant.components.firmata.switch]
ignore_errors = true
[mypy-homeassistant.components.geniushub] [mypy-homeassistant.components.geniushub]
ignore_errors = true ignore_errors = true

View file

@ -48,14 +48,6 @@ IGNORED_MODULES: Final[list[str]] = [
"homeassistant.components.fireservicerota.binary_sensor", "homeassistant.components.fireservicerota.binary_sensor",
"homeassistant.components.fireservicerota.sensor", "homeassistant.components.fireservicerota.sensor",
"homeassistant.components.fireservicerota.switch", "homeassistant.components.fireservicerota.switch",
"homeassistant.components.firmata",
"homeassistant.components.firmata.binary_sensor",
"homeassistant.components.firmata.board",
"homeassistant.components.firmata.entity",
"homeassistant.components.firmata.light",
"homeassistant.components.firmata.pin",
"homeassistant.components.firmata.sensor",
"homeassistant.components.firmata.switch",
"homeassistant.components.geniushub", "homeassistant.components.geniushub",
"homeassistant.components.geniushub.binary_sensor", "homeassistant.components.geniushub.binary_sensor",
"homeassistant.components.geniushub.climate", "homeassistant.components.geniushub.climate",