Skip to content

Commit d175e19

Browse files
committed
Safe refresh token
1 parent 8694882 commit d175e19

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

custom_components/hon/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
2929
session = aiohttp_client.async_get_clientsession(hass)
3030
if (config_dir := hass.config.config_dir) is None:
3131
raise ValueError("Missing Config Dir")
32-
hon = await Hon(
33-
entry.data[CONF_EMAIL],
34-
entry.data[CONF_PASSWORD],
35-
mobile_id=MOBILE_ID,
36-
session=session,
37-
refresh_token=entry.data[CONF_REFRESH_TOKEN],
38-
test_data_path=Path(config_dir),
39-
).create()
32+
kwargs = {
33+
"email": entry.data[CONF_EMAIL],
34+
"password": entry.data[CONF_PASSWORD],
35+
"mobile_id": MOBILE_ID,
36+
"session": session,
37+
"test_data_path": Path(config_dir),
38+
}
39+
if refresh_token := entry.data.get(CONF_REFRESH_TOKEN):
40+
kwargs["refresh_token"] = refresh_token
41+
hon = await Hon(**kwargs).create()
4042
hass.data.setdefault(DOMAIN, {})
4143
hass.data[DOMAIN][entry.unique_id] = hon
44+
45+
hass.config_entries.async_update_entry(
46+
entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token}
47+
)
4248
hass.data[DOMAIN]["coordinators"] = {}
4349

4450
for platform in PLATFORMS:
@@ -49,7 +55,11 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
4955

5056

5157
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
52-
entry.data[CONF_REFRESH_TOKEN] = hass.data[DOMAIN][entry.unique_id].api.auth.refresh_token
58+
refresh_token = hass.data[DOMAIN][entry.unique_id].api.auth.refresh_token
59+
60+
hass.config_entries.async_update_entry(
61+
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
62+
)
5363
unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
5464
if unload:
5565
if not hass.data[DOMAIN]:

0 commit comments

Comments
 (0)