Use runtime_data in tesla_wall_connector (#167893)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
epenet
2026-04-10 15:38:04 +02:00
committed by GitHub
parent ba09a54a37
commit a6a716571d
4 changed files with 28 additions and 26 deletions

View File

@@ -5,21 +5,25 @@ from __future__ import annotations
from tesla_wall_connector import WallConnector
from tesla_wall_connector.exceptions import WallConnectorError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
from .coordinator import WallConnectorCoordinator, WallConnectorData, get_poll_interval
from .coordinator import (
WallConnectorConfigEntry,
WallConnectorCoordinator,
WallConnectorData,
get_poll_interval,
)
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, entry: WallConnectorConfigEntry
) -> bool:
"""Set up Tesla Wall Connector from a config entry."""
hass.data.setdefault(DOMAIN, {})
hostname = entry.data[CONF_HOST]
wall_connector = WallConnector(host=hostname, session=async_get_clientsession(hass))
@@ -32,7 +36,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = WallConnectorCoordinator(hass, entry, hostname, wall_connector)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = WallConnectorData(
entry.runtime_data = WallConnectorData(
wall_connector_client=wall_connector,
hostname=hostname,
part_number=version_data.part_number,
@@ -48,15 +52,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def update_listener(hass: HomeAssistant, entry: WallConnectorConfigEntry) -> None:
"""Handle options update."""
wall_connector_data: WallConnectorData = hass.data[DOMAIN][entry.entry_id]
wall_connector_data.update_coordinator.update_interval = get_poll_interval(entry)
entry.runtime_data.update_coordinator.update_interval = get_poll_interval(entry)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, entry: WallConnectorConfigEntry
) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@@ -8,13 +8,12 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN, WALLCONNECTOR_DATA_VITALS
from .coordinator import WallConnectorData
from .const import WALLCONNECTOR_DATA_VITALS
from .coordinator import WallConnectorConfigEntry, WallConnectorData
from .entity import WallConnectorEntity, WallConnectorLambdaValueGetterMixin
_LOGGER = logging.getLogger(__name__)
@@ -47,11 +46,11 @@ WALL_CONNECTOR_SENSORS = [
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: WallConnectorConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Create the Wall Connector sensor devices."""
wall_connector_data = hass.data[DOMAIN][config_entry.entry_id]
wall_connector_data = config_entry.runtime_data
all_entities = [
WallConnectorBinarySensorEntity(wall_connector_data, description)

View File

@@ -26,6 +26,8 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
type WallConnectorConfigEntry = ConfigEntry[WallConnectorData]
@dataclass
class WallConnectorData:
@@ -49,12 +51,12 @@ def get_poll_interval(entry: ConfigEntry) -> timedelta:
class WallConnectorCoordinator(DataUpdateCoordinator[dict]):
"""Class to manage fetching Tesla Wall Connector data."""
config_entry: ConfigEntry
config_entry: WallConnectorConfigEntry
def __init__(
self,
hass: HomeAssistant,
entry: ConfigEntry,
entry: WallConnectorConfigEntry,
hostname: str,
wall_connector: WallConnector,
) -> None:

View File

@@ -9,7 +9,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
EntityCategory,
UnitOfElectricCurrent,
@@ -22,8 +21,8 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN, WALLCONNECTOR_DATA_LIFETIME, WALLCONNECTOR_DATA_VITALS
from .coordinator import WallConnectorData
from .const import WALLCONNECTOR_DATA_LIFETIME, WALLCONNECTOR_DATA_VITALS
from .coordinator import WallConnectorConfigEntry, WallConnectorData
from .entity import WallConnectorEntity, WallConnectorLambdaValueGetterMixin
_LOGGER = logging.getLogger(__name__)
@@ -196,11 +195,11 @@ WALL_CONNECTOR_SENSORS = [
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: WallConnectorConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Create the Wall Connector sensor devices."""
wall_connector_data = hass.data[DOMAIN][config_entry.entry_id]
wall_connector_data = config_entry.runtime_data
all_entities = [
WallConnectorSensorEntity(wall_connector_data, description)