diff --git a/homeassistant/components/electrasmart/__init__.py b/homeassistant/components/electrasmart/__init__.py index b8e5eb1bdd8..27cebc9aee9 100644 --- a/homeassistant/components/electrasmart/__init__.py +++ b/homeassistant/components/electrasmart/__init__.py @@ -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) diff --git a/homeassistant/components/electrasmart/climate.py b/homeassistant/components/electrasmart/climate.py index 04e4742554b..84def436dfb 100644 --- a/homeassistant/components/electrasmart/climate.py +++ b/homeassistant/components/electrasmart/climate.py @@ -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(