Files

52 lines
1.8 KiB
Python

"""Home Assistant bridge for the standalone GREE Controller service."""
from __future__ import annotations
from dataclasses import dataclass
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .api import GreeControllerApiError, GreeControllerClient
from .const import CONF_TOKEN, CONF_URL, PLATFORMS
from .coordinator import GreeControllerCoordinator
from .entity_map import async_load_entity_map
@dataclass
class GreeControllerRuntimeData:
"""Runtime objects kept on the config entry."""
client: GreeControllerClient
coordinator: GreeControllerCoordinator
entity_map: dict[str, str]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up GREE Controller from a config entry."""
client = GreeControllerClient(
async_get_clientsession(hass),
entry.data[CONF_URL],
entry.data.get(CONF_TOKEN, ""),
)
try:
await client.devices()
except GreeControllerApiError as err:
if "authentication" in str(err).lower():
raise ConfigEntryAuthFailed(str(err)) from err
raise ConfigEntryNotReady(str(err)) from err
coordinator = GreeControllerCoordinator(hass, entry, client)
await coordinator.async_config_entry_first_refresh()
entity_map = await async_load_entity_map(hass)
entry.runtime_data = GreeControllerRuntimeData(client, coordinator, entity_map)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload the config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)