Safe refresh token

This commit is contained in:
Andre Basche 2024-02-11 05:06:53 +01:00
parent 8694882c45
commit d175e19c3b

View file

@ -29,16 +29,22 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
session = aiohttp_client.async_get_clientsession(hass) session = aiohttp_client.async_get_clientsession(hass)
if (config_dir := hass.config.config_dir) is None: if (config_dir := hass.config.config_dir) is None:
raise ValueError("Missing Config Dir") raise ValueError("Missing Config Dir")
hon = await Hon( kwargs = {
entry.data[CONF_EMAIL], "email": entry.data[CONF_EMAIL],
entry.data[CONF_PASSWORD], "password": entry.data[CONF_PASSWORD],
mobile_id=MOBILE_ID, "mobile_id": MOBILE_ID,
session=session, "session": session,
refresh_token=entry.data[CONF_REFRESH_TOKEN], "test_data_path": Path(config_dir),
test_data_path=Path(config_dir), }
).create() 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.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = hon 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"] = {} hass.data[DOMAIN]["coordinators"] = {}
for platform in PLATFORMS: 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: 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) unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload: if unload:
if not hass.data[DOMAIN]: if not hass.data[DOMAIN]: