Use runtime_data in electrasmart (#136696)

This commit is contained in:
epenet 2025-01-28 10:58:10 +01:00 committed by GitHub
parent b1fec51e2f
commit 5d55dcf392
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 18 deletions

View File

@ -2,8 +2,6 @@
from __future__ import annotations
from typing import cast
from electrasmart.api import ElectraAPI, ElectraApiError
from homeassistant.config_entries import ConfigEntry
@ -12,36 +10,40 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_IMEI, DOMAIN
from .const import CONF_IMEI
PLATFORMS: list[Platform] = [Platform.CLIMATE]
type ElectraSmartConfigEntry = ConfigEntry[ElectraAPI]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, entry: ElectraSmartConfigEntry
) -> bool:
"""Set up Electra Smart Air Conditioner from a config entry."""
hass.data.setdefault(DOMAIN, {})
entry.async_on_unload(entry.add_update_listener(update_listener))
hass.data[DOMAIN][entry.entry_id] = ElectraAPI(
api = ElectraAPI(
async_get_clientsession(hass), entry.data[CONF_IMEI], entry.data[CONF_TOKEN]
)
try:
await cast(ElectraAPI, hass.data[DOMAIN][entry.entry_id]).fetch_devices()
await api.fetch_devices()
except ElectraApiError as exp:
raise ConfigEntryNotReady(f"Error communicating with API: {exp}") from exp
entry.async_on_unload(entry.add_update_listener(update_listener))
entry.runtime_data = api
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, entry: ElectraSmartConfigEntry
) -> 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)
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
async def update_listener(
hass: HomeAssistant, config_entry: ElectraSmartConfigEntry
) -> None:
"""Update listener."""
await hass.config_entries.async_reload(config_entry.entry_id)

View File

@ -24,13 +24,13 @@ from homeassistant.components.climate import (
ClimateEntityFeature,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ElectraSmartConfigEntry
from .const import (
API_DELAY,
CONSECUTIVE_FAILURE_THRESHOLD,
@ -89,10 +89,12 @@ PARALLEL_UPDATES = 0
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: ElectraSmartConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add Electra AC devices."""
api: ElectraAPI = hass.data[DOMAIN][entry.entry_id]
api = entry.runtime_data
_LOGGER.debug("Discovered %i Electra devices", len(api.devices))
async_add_entities(