hOn/custom_components/hon/__init__.py

58 lines
1.8 KiB
Python
Raw Normal View History

2023-02-19 01:58:21 +00:00
import logging
2023-06-25 15:33:30 +00:00
from pathlib import Path
2023-02-19 01:58:21 +00:00
2023-11-20 14:56:24 +00:00
import voluptuous as vol # type: ignore[import-untyped]
2023-02-19 01:58:21 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers import config_validation as cv, aiohttp_client
from homeassistant.helpers.typing import HomeAssistantType
2023-06-25 15:33:30 +00:00
from pyhon import Hon
2023-03-03 17:23:30 +00:00
2024-02-10 00:02:26 +00:00
from .const import DOMAIN, PLATFORMS, MOBILE_ID, CONF_REFRESH_TOKEN
2023-02-19 01:58:21 +00:00
_LOGGER = logging.getLogger(__name__)
HON_SCHEMA = vol.Schema(
{
vol.Required(CONF_EMAIL): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
}
)
CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.Schema(vol.All(cv.ensure_list, [HON_SCHEMA]))},
extra=vol.ALLOW_EXTRA,
)
2023-07-24 19:37:48 +00:00
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
2023-02-19 01:58:21 +00:00
session = aiohttp_client.async_get_clientsession(hass)
if (config_dir := hass.config.config_dir) is None:
raise ValueError("Missing Config Dir")
2023-04-10 17:51:16 +00:00
hon = await Hon(
2024-02-10 00:02:26 +00:00
entry.data[CONF_EMAIL],
entry.data[CONF_PASSWORD],
mobile_id=MOBILE_ID,
2023-06-25 15:33:30 +00:00
session=session,
2024-02-10 00:02:26 +00:00
refresh_token=entry.data[CONF_REFRESH_TOKEN],
test_data_path=Path(config_dir),
2023-04-10 17:51:16 +00:00
).create()
2023-02-19 01:58:21 +00:00
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = hon
hass.data[DOMAIN]["coordinators"] = {}
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
2023-07-24 19:37:48 +00:00
return True
2023-03-04 23:54:57 +00:00
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
2024-02-10 00:02:26 +00:00
entry.data[CONF_REFRESH_TOKEN] = hass.data[DOMAIN][entry.unique_id].api.auth.refresh_token
2023-03-04 23:54:57 +00:00
unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload:
if not hass.data[DOMAIN]:
hass.data.pop(DOMAIN, None)
return unload