Streamline Notion config entry updates (refresh token and user ID) (#112832)

This commit is contained in:
Aaron Bach 2024-03-10 11:04:17 -06:00 committed by GitHub
parent b59bba8951
commit 113df1ab62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -166,9 +166,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except NotionError as err: except NotionError as err:
raise ConfigEntryNotReady("Config entry failed to load") from err raise ConfigEntryNotReady("Config entry failed to load") from err
# Always update the config entry with the latest refresh token and user UUID: # Update the Notion user UUID and refresh token if they've changed:
entry_updates["data"][CONF_REFRESH_TOKEN] = client.refresh_token for key, value in (
entry_updates["data"][CONF_USER_UUID] = client.user_uuid (CONF_REFRESH_TOKEN, client.refresh_token),
(CONF_USER_UUID, client.user_uuid),
):
if entry.data[key] == value:
continue
entry_updates["data"][key] = value
hass.config_entries.async_update_entry(entry, **entry_updates)
@callback @callback
def async_save_refresh_token(refresh_token: str) -> None: def async_save_refresh_token(refresh_token: str) -> None:
@ -181,12 +188,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Create a callback to save the refresh token when it changes: # Create a callback to save the refresh token when it changes:
entry.async_on_unload(client.add_refresh_token_callback(async_save_refresh_token)) entry.async_on_unload(client.add_refresh_token_callback(async_save_refresh_token))
# Save the client's refresh token if it's different than what we already have:
if (token := client.refresh_token) and token != entry.data[CONF_REFRESH_TOKEN]:
async_save_refresh_token(token)
hass.config_entries.async_update_entry(entry, **entry_updates)
async def async_update() -> NotionData: async def async_update() -> NotionData:
"""Get the latest data from the Notion API.""" """Get the latest data from the Notion API."""
data = NotionData(hass=hass, entry=entry) data = NotionData(hass=hass, entry=entry)