1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Add setup type hints to raspihats (#63988)

* Use DOMAIN to store hass.data

* Add setup type hints to binary_sensor

* Add setup type hints to switch

* Adjust callback registration

* Revert "Adjust callback registration"

This reverts commit 88c179f870.

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-01-14 16:43:52 +01:00 committed by GitHub
parent 2c0033254b
commit 3049f820b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 11 deletions

View File

@ -40,15 +40,15 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"https://github.com/home-assistant/architecture/blob/master/adr/0019-GPIO.md"
)
hass.data[I2C_HATS_MANAGER] = I2CHatsManager()
hass.data[DOMAIN][I2C_HATS_MANAGER] = I2CHatsManager()
def start_i2c_hats_keep_alive(event):
"""Start I2C-HATs keep alive."""
hass.data[I2C_HATS_MANAGER].start_keep_alive()
hass.data[DOMAIN][I2C_HATS_MANAGER].start_keep_alive()
def stop_i2c_hats_keep_alive(event):
"""Stop I2C-HATs keep alive."""
hass.data[I2C_HATS_MANAGER].stop_keep_alive()
hass.data[DOMAIN][I2C_HATS_MANAGER].stop_keep_alive()
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_i2c_hats_keep_alive)
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_i2c_hats_keep_alive)

View File

@ -1,4 +1,6 @@
"""Support for raspihats board binary sensors."""
from __future__ import annotations
import logging
import voluptuous as vol
@ -10,7 +12,10 @@ from homeassistant.const import (
CONF_NAME,
DEVICE_DEFAULT_NAME,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import (
CONF_BOARD,
@ -18,9 +23,11 @@ from . import (
CONF_I2C_HATS,
CONF_INDEX,
CONF_INVERT_LOGIC,
DOMAIN,
I2C_HAT_NAMES,
I2C_HATS_MANAGER,
I2CHatsException,
I2CHatsManager,
)
_LOGGER = logging.getLogger(__name__)
@ -54,15 +61,21 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
)
def setup_platform(hass, config, add_entities, discovery_info=None):
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the raspihats binary_sensor devices."""
I2CHatBinarySensor.I2C_HATS_MANAGER = hass.data[I2C_HATS_MANAGER]
I2CHatBinarySensor.I2C_HATS_MANAGER = hass.data[DOMAIN][I2C_HATS_MANAGER]
binary_sensors = []
i2c_hat_configs = config.get(CONF_I2C_HATS)
i2c_hat_configs = config.get(CONF_I2C_HATS, [])
for i2c_hat_config in i2c_hat_configs:
address = i2c_hat_config[CONF_ADDRESS]
board = i2c_hat_config[CONF_BOARD]
try:
assert I2CHatBinarySensor.I2C_HATS_MANAGER
I2CHatBinarySensor.I2C_HATS_MANAGER.register_board(board, address)
for channel_config in i2c_hat_config[CONF_CHANNELS]:
binary_sensors.append(
@ -84,7 +97,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class I2CHatBinarySensor(BinarySensorEntity):
"""Representation of a binary sensor that uses a I2C-HAT digital input."""
I2C_HATS_MANAGER = None
I2C_HATS_MANAGER: I2CHatsManager | None = None
def __init__(self, address, channel, name, invert_logic, device_class):
"""Initialize the raspihats sensor."""

View File

@ -1,12 +1,17 @@
"""Support for raspihats board switches."""
from __future__ import annotations
import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA
from homeassistant.const import CONF_ADDRESS, CONF_NAME, DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import (
CONF_BOARD,
@ -15,9 +20,11 @@ from . import (
CONF_INDEX,
CONF_INITIAL_STATE,
CONF_INVERT_LOGIC,
DOMAIN,
I2C_HAT_NAMES,
I2C_HATS_MANAGER,
I2CHatsException,
I2CHatsManager,
)
_LOGGER = logging.getLogger(__name__)
@ -48,15 +55,21 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
)
def setup_platform(hass, config, add_entities, discovery_info=None):
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the raspihats switch devices."""
I2CHatSwitch.I2C_HATS_MANAGER = hass.data[I2C_HATS_MANAGER]
I2CHatSwitch.I2C_HATS_MANAGER = hass.data[DOMAIN][I2C_HATS_MANAGER]
switches = []
i2c_hat_configs = config.get(CONF_I2C_HATS)
i2c_hat_configs = config.get(CONF_I2C_HATS, [])
for i2c_hat_config in i2c_hat_configs:
board = i2c_hat_config[CONF_BOARD]
address = i2c_hat_config[CONF_ADDRESS]
try:
assert I2CHatSwitch.I2C_HATS_MANAGER
I2CHatSwitch.I2C_HATS_MANAGER.register_board(board, address)
for channel_config in i2c_hat_config[CONF_CHANNELS]:
switches.append(
@ -79,7 +92,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class I2CHatSwitch(ToggleEntity):
"""Representation a switch that uses a I2C-HAT digital output."""
I2C_HATS_MANAGER = None
I2C_HATS_MANAGER: I2CHatsManager | None = None
def __init__(self, board, address, channel, name, invert_logic, initial_state):
"""Initialize switch."""