Use runtime_data in firmata (#137630)

This commit is contained in:
epenet 2025-02-07 11:30:13 +01:00 committed by GitHub
parent ff42353e61
commit bdc847c7ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 25 deletions

View File

@ -122,6 +122,8 @@ CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.All(cv.ensure_list, [BOARD_CONFIG_SCHEMA])}, extra=vol.ALLOW_EXTRA
)
type FirmataConfigEntry = ConfigEntry[FirmataBoard]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Firmata domain."""
@ -158,11 +160,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: FirmataConfigEntry
) -> bool:
"""Set up a Firmata board for a config entry."""
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}
_LOGGER.debug(
"Setting up Firmata id %s, name %s, config %s",
config_entry.entry_id,
@ -175,13 +176,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if not await board.async_setup():
return False
hass.data[DOMAIN][config_entry.entry_id] = board
config_entry.runtime_data = board
async def handle_shutdown(event) -> None:
"""Handle shutdown of board when Home Assistant shuts down."""
# Ensure board was not already removed previously before shutdown
if config_entry.entry_id in hass.data[DOMAIN]:
await board.async_reset()
await board.async_reset()
config_entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_shutdown)
@ -208,7 +207,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, config_entry: FirmataConfigEntry
) -> bool:
"""Shutdown and close a Firmata board for a config entry."""
_LOGGER.debug("Closing Firmata board %s", config_entry.data[CONF_NAME])
results: list[bool] = []
@ -220,6 +221,6 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
results.append(
await hass.config_entries.async_unload_platforms(config_entry, platforms)
)
results.append(await hass.data[DOMAIN].pop(config_entry.entry_id).async_reset())
results.append(await config_entry.runtime_data.async_reset())
return False not in results

View File

@ -3,12 +3,12 @@
import logging
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_PIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_NEGATE_STATE, CONF_PIN_MODE, DOMAIN
from . import FirmataConfigEntry
from .const import CONF_NEGATE_STATE, CONF_PIN_MODE
from .entity import FirmataPinEntity
from .pin import FirmataBinaryDigitalInput, FirmataPinUsedException
@ -17,13 +17,13 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FirmataConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Firmata binary sensors."""
new_entities = []
board = hass.data[DOMAIN][config_entry.entry_id]
board = config_entry.runtime_data
for binary_sensor in board.binary_sensors:
pin = binary_sensor[CONF_PIN]
pin_mode = binary_sensor[CONF_PIN_MODE]

View File

@ -11,8 +11,9 @@ from homeassistant.const import CONF_MAXIMUM, CONF_MINIMUM, CONF_NAME, CONF_PIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FirmataConfigEntry
from .board import FirmataPinType
from .const import CONF_INITIAL_STATE, CONF_PIN_MODE, DOMAIN
from .const import CONF_INITIAL_STATE, CONF_PIN_MODE
from .entity import FirmataPinEntity
from .pin import FirmataBoardPin, FirmataPinUsedException, FirmataPWMOutput
@ -21,13 +22,13 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FirmataConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Firmata lights."""
new_entities = []
board = hass.data[DOMAIN][config_entry.entry_id]
board = config_entry.runtime_data
for light in board.lights:
pin = light[CONF_PIN]
pin_mode = light[CONF_PIN_MODE]

View File

@ -3,12 +3,12 @@
import logging
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_PIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_DIFFERENTIAL, CONF_PIN_MODE, DOMAIN
from . import FirmataConfigEntry
from .const import CONF_DIFFERENTIAL, CONF_PIN_MODE
from .entity import FirmataPinEntity
from .pin import FirmataAnalogInput, FirmataPinUsedException
@ -17,13 +17,13 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FirmataConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Firmata sensors."""
new_entities = []
board = hass.data[DOMAIN][config_entry.entry_id]
board = config_entry.runtime_data
for sensor in board.sensors:
pin = sensor[CONF_PIN]
pin_mode = sensor[CONF_PIN_MODE]

View File

@ -4,12 +4,12 @@ import logging
from typing import Any
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_PIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_INITIAL_STATE, CONF_NEGATE_STATE, CONF_PIN_MODE, DOMAIN
from . import FirmataConfigEntry
from .const import CONF_INITIAL_STATE, CONF_NEGATE_STATE, CONF_PIN_MODE
from .entity import FirmataPinEntity
from .pin import FirmataBinaryDigitalOutput, FirmataPinUsedException
@ -18,13 +18,13 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FirmataConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Firmata switches."""
new_entities = []
board = hass.data[DOMAIN][config_entry.entry_id]
board = config_entry.runtime_data
for switch in board.switches:
pin = switch[CONF_PIN]
pin_mode = switch[CONF_PIN_MODE]