feat: implement setup/unload lifecycle with options-reload listener

This commit is contained in:
Joshua King
2026-09-13 14:59:23 -04:00
parent f787d04a6b
commit 6da5f1936e
2 changed files with 263 additions and 0 deletions
+37
View File
@@ -1,6 +1,9 @@
"""The NestQuest integration."""
from __future__ import annotations
import inspect
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from homeassistant.config_entries import ConfigEntry
@@ -11,6 +14,15 @@ from .const import DOMAIN, LOGGER
_LOGGER = LOGGER
@dataclass
class NestQuestRuntimeData:
"""Runtime data stored on a NestQuest config entry."""
entry_id: str
options: dict[str, Any]
remove_update_listener: Callable[[], Any]
async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
"""Set up the NestQuest integration. YAML configuration is not used, returns True."""
return True
@@ -19,9 +31,34 @@ async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up NestQuest from a config entry."""
hass.data.setdefault(DOMAIN, {})
async def _async_update_listener(
listener_hass: HomeAssistant, listener_entry: ConfigEntry
) -> None:
"""Reload the entry when its options change."""
await listener_hass.config_entries.async_reload(listener_entry.entry_id)
runtime_data = NestQuestRuntimeData(
entry_id=entry.entry_id,
options=dict(entry.options),
remove_update_listener=entry.add_update_listener(_async_update_listener),
)
entry.runtime_data = runtime_data
hass.data[DOMAIN][entry.entry_id] = runtime_data
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
runtime_data = hass.data.setdefault(DOMAIN, {}).pop(entry.entry_id, None)
if runtime_data is None:
runtime_data = getattr(entry, "runtime_data", None)
if runtime_data is not None:
remove_update_listener = getattr(runtime_data, "remove_update_listener", None)
if remove_update_listener is not None:
result = remove_update_listener()
if inspect.isawaitable(result):
await result
if getattr(entry, "runtime_data", None) is not None:
entry.runtime_data = None
return True