Safe refresh token

This commit is contained in:
Andre Basche 2024-02-11 05:06:53 +01:00
parent 8694882c45
commit d175e19c3b
1 changed files with 19 additions and 9 deletions

View File

@ -29,16 +29,22 @@ 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")
hon = await Hon(
entry.data[CONF_EMAIL],
entry.data[CONF_PASSWORD],
mobile_id=MOBILE_ID,
session=session,
refresh_token=entry.data[CONF_REFRESH_TOKEN],
test_data_path=Path(config_dir),
).create()
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
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token}
)
hass.data[DOMAIN]["coordinators"] = {}
for platform in PLATFORMS:
@ -49,7 +55,11 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
entry.data[CONF_REFRESH_TOKEN] = hass.data[DOMAIN][entry.unique_id].api.auth.refresh_token
refresh_token = hass.data[DOMAIN][entry.unique_id].api.auth.refresh_token
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
)
unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload:
if not hass.data[DOMAIN]: