2026-09-10 16:50:59 -04:00
|
|
|
"""The NestQuest integration."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-09-13 15:02:42 -04:00
|
|
|
import inspect
|
|
|
|
|
from collections.abc import Callable
|
|
|
|
|
from dataclasses import dataclass
|
2026-09-10 16:50:59 -04:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
2026-09-11 16:39:41 -04:00
|
|
|
from .const import DOMAIN, LOGGER
|
2026-09-13 21:35:36 -04:00
|
|
|
from .db import NestQuestDatabase
|
|
|
|
|
from .store import async_get_db_path
|
2026-09-10 16:50:59 -04:00
|
|
|
|
2026-09-11 16:39:41 -04:00
|
|
|
_LOGGER = LOGGER
|
2026-09-10 16:50:59 -04:00
|
|
|
|
|
|
|
|
|
2026-09-13 15:02:42 -04:00
|
|
|
@dataclass
|
|
|
|
|
class NestQuestRuntimeData:
|
|
|
|
|
"""Runtime data stored on a NestQuest config entry."""
|
|
|
|
|
|
|
|
|
|
entry_id: str
|
|
|
|
|
options: dict[str, Any]
|
2026-09-13 21:35:36 -04:00
|
|
|
database: NestQuestDatabase
|
2026-09-13 15:02:42 -04:00
|
|
|
remove_update_listener: Callable[[], Any]
|
|
|
|
|
|
|
|
|
|
|
2026-09-10 16:50:59 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
|
"""Set up NestQuest from a config entry."""
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2026-09-13 21:35:36 -04:00
|
|
|
existing = hass.data[DOMAIN].get(entry.entry_id)
|
|
|
|
|
if existing is not None:
|
|
|
|
|
db = getattr(existing, "database", None)
|
|
|
|
|
if db is None:
|
|
|
|
|
db = await NestQuestDatabase(hass).open(await async_get_db_path(hass))
|
|
|
|
|
existing.database = db
|
|
|
|
|
entry.runtime_data = existing
|
|
|
|
|
return True
|
2026-09-13 15:02:42 -04:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2026-09-13 21:35:36 -04:00
|
|
|
database = await NestQuestDatabase(hass).open(await async_get_db_path(hass))
|
|
|
|
|
try:
|
|
|
|
|
remove_update_listener = entry.add_update_listener(_async_update_listener)
|
|
|
|
|
except BaseException:
|
|
|
|
|
await database.close()
|
|
|
|
|
raise
|
2026-09-13 15:02:42 -04:00
|
|
|
runtime_data = NestQuestRuntimeData(
|
|
|
|
|
entry_id=entry.entry_id,
|
|
|
|
|
options=dict(entry.options),
|
2026-09-13 21:35:36 -04:00
|
|
|
database=database,
|
|
|
|
|
remove_update_listener=remove_update_listener,
|
2026-09-13 15:02:42 -04:00
|
|
|
)
|
|
|
|
|
entry.runtime_data = runtime_data
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = runtime_data
|
2026-09-10 16:50:59 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
|
"""Unload a config entry."""
|
2026-09-13 19:18:10 -04:00
|
|
|
domain_data = hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
runtime_data = domain_data.pop(entry.entry_id, None)
|
|
|
|
|
if not domain_data:
|
|
|
|
|
hass.data.pop(DOMAIN, None)
|
2026-09-13 15:02:42 -04:00
|
|
|
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)
|
2026-09-13 21:35:36 -04:00
|
|
|
unload_error: BaseException | None = None
|
|
|
|
|
try:
|
|
|
|
|
if remove_update_listener is not None:
|
|
|
|
|
result = remove_update_listener()
|
|
|
|
|
if inspect.isawaitable(result):
|
|
|
|
|
await result
|
|
|
|
|
except BaseException as err:
|
|
|
|
|
unload_error = err
|
|
|
|
|
finally:
|
|
|
|
|
database = getattr(runtime_data, "database", None)
|
|
|
|
|
if database is not None:
|
|
|
|
|
await database.close()
|
|
|
|
|
if unload_error is not None:
|
|
|
|
|
if getattr(entry, "runtime_data", None) is not None:
|
|
|
|
|
entry.runtime_data = None
|
|
|
|
|
raise unload_error
|
2026-09-13 15:02:42 -04:00
|
|
|
if getattr(entry, "runtime_data", None) is not None:
|
|
|
|
|
entry.runtime_data = None
|
2026-09-10 16:50:59 -04:00
|
|
|
return True
|