mirror of
https://github.com/Andre0512/hon.git
synced 2025-06-05 01:03:42 -04:00
Rebuild to single data coordinator
This commit is contained in:
parent
8f1fc627e6
commit
a6c2c3e992
16 changed files with 148 additions and 203 deletions
|
@ -1,11 +1,13 @@
|
|||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol # type: ignore[import-untyped]
|
||||
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
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
from pyhon import Hon
|
||||
|
||||
from .const import DOMAIN, PLATFORMS, MOBILE_ID, CONF_REFRESH_TOKEN
|
||||
|
@ -29,23 +31,27 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
|||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
if (config_dir := hass.config.config_dir) is None:
|
||||
raise ValueError("Missing Config Dir")
|
||||
kwargs = {
|
||||
"email": entry.data[CONF_EMAIL],
|
||||
"password": entry.data[CONF_PASSWORD],
|
||||
"mobile_id": MOBILE_ID,
|
||||
"session": session,
|
||||
"test_data_path": Path(config_dir),
|
||||
}
|
||||
if refresh_token := entry.data.get(CONF_REFRESH_TOKEN):
|
||||
kwargs["refresh_token"] = refresh_token
|
||||
hon = await Hon(**kwargs).create()
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.unique_id] = hon
|
||||
hon = await Hon(
|
||||
email=entry.data[CONF_EMAIL],
|
||||
password=entry.data[CONF_PASSWORD],
|
||||
mobile_id=MOBILE_ID,
|
||||
session=session,
|
||||
# test_data_path=Path(config_dir),
|
||||
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""),
|
||||
).create()
|
||||
|
||||
# Save the new refresh token
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token}
|
||||
)
|
||||
hass.data[DOMAIN]["coordinators"] = {}
|
||||
|
||||
coordinator: DataUpdateCoordinator[dict[str, Any]] = DataUpdateCoordinator(
|
||||
hass, _LOGGER, name=DOMAIN
|
||||
)
|
||||
hon.subscribe_updates(coordinator.async_set_updated_data)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.unique_id] = {"hon": hon, "coordinator": coordinator}
|
||||
|
||||
for platform in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
|
@ -55,7 +61,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
|||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
||||
refresh_token = hass.data[DOMAIN][entry.unique_id].api.auth.refresh_token
|
||||
refresh_token = hass.data[DOMAIN][entry.unique_id]["hon"].api.auth.refresh_token
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue