Use config_entry.runtime_data in Honeywell (#132297)

* Use entry.runtime_data

* switch

* create new type

* Extend ConfigEntry

* simplify runtime_data, clean up data types

* More config_entry types

* Yet more missing type changes
pull/132306/head
mkmer 2024-12-04 15:54:12 -05:00 committed by GitHub
parent 437111453b
commit 950563cf32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 30 deletions

View File

@ -26,10 +26,12 @@ PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH]
MIGRATE_OPTIONS_KEYS = {CONF_COOL_AWAY_TEMPERATURE, CONF_HEAT_AWAY_TEMPERATURE}
type HoneywellConfigEntry = ConfigEntry[HoneywellData]
@callback
def _async_migrate_data_to_options(
hass: HomeAssistant, config_entry: ConfigEntry
hass: HomeAssistant, config_entry: HoneywellConfigEntry
) -> None:
if not MIGRATE_OPTIONS_KEYS.intersection(config_entry.data):
return
@ -45,7 +47,9 @@ def _async_migrate_data_to_options(
)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: HoneywellConfigEntry
) -> bool:
"""Set up the Honeywell thermostat."""
_async_migrate_data_to_options(hass, config_entry)
@ -84,8 +88,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if len(devices) == 0:
_LOGGER.debug("No devices found")
return False
data = HoneywellData(config_entry.entry_id, client, devices)
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = data
config_entry.runtime_data = HoneywellData(config_entry.entry_id, client, devices)
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
@ -93,19 +96,18 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
async def update_listener(
hass: HomeAssistant, config_entry: HoneywellConfigEntry
) -> None:
"""Update listener."""
await hass.config_entries.async_reload(config_entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, config_entry: HoneywellConfigEntry
) -> bool:
"""Unload the config and platforms."""
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
)
if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
@dataclass

View File

@ -31,7 +31,6 @@ from homeassistant.components.climate import (
HVACAction,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
@ -40,7 +39,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.unit_conversion import TemperatureConverter
from . import HoneywellData
from . import HoneywellConfigEntry, HoneywellData
from .const import (
_LOGGER,
CONF_COOL_AWAY_TEMPERATURE,
@ -97,13 +96,15 @@ SCAN_INTERVAL = datetime.timedelta(seconds=30)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: HoneywellConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Honeywell thermostat."""
cool_away_temp = entry.options.get(CONF_COOL_AWAY_TEMPERATURE)
heat_away_temp = entry.options.get(CONF_HEAT_AWAY_TEMPERATURE)
data: HoneywellData = hass.data[DOMAIN][entry.entry_id]
data = entry.runtime_data
_async_migrate_unique_id(hass, data.devices)
async_add_entities(
[
@ -131,7 +132,7 @@ def _async_migrate_unique_id(
def remove_stale_devices(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HoneywellConfigEntry,
devices: dict[str, SomeComfortDevice],
) -> None:
"""Remove stale devices from device registry."""

View File

@ -4,19 +4,17 @@ from __future__ import annotations
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from . import HoneywellData
from .const import DOMAIN
from . import HoneywellConfigEntry
async def async_get_config_entry_diagnostics(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HoneywellConfigEntry,
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
honeywell: HoneywellData = hass.data[DOMAIN][config_entry.entry_id]
honeywell = config_entry.runtime_data
return {
f"Device {device}": {

View File

@ -14,14 +14,13 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import HoneywellData
from . import HoneywellConfigEntry
from .const import DOMAIN
OUTDOOR_TEMPERATURE_STATUS_KEY = "outdoor_temperature"
@ -81,11 +80,11 @@ SENSOR_TYPES: tuple[HoneywellSensorEntityDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HoneywellConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Honeywell thermostat."""
data: HoneywellData = hass.data[DOMAIN][config_entry.entry_id]
data = config_entry.runtime_data
async_add_entities(
HoneywellSensor(device, description)

View File

@ -12,13 +12,12 @@ from homeassistant.components.switch import (
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
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 HoneywellData
from . import HoneywellConfigEntry, HoneywellData
from .const import DOMAIN
EMERGENCY_HEAT_KEY = "emergency_heat"
@ -34,11 +33,11 @@ SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HoneywellConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Honeywell switches."""
data: HoneywellData = hass.data[DOMAIN][config_entry.entry_id]
data = config_entry.runtime_data
async_add_entities(
HoneywellSwitch(data, device, description)
for device in data.devices.values()