From b437ffc1d9c57e7c8e0bb8cb7bcffe44f77e10ba Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sat, 9 Mar 2024 09:04:24 -0700 Subject: [PATCH 1/2] Remove redundancy in Notion refresh token save on startup --- homeassistant/components/notion/__init__.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/notion/__init__.py b/homeassistant/components/notion/__init__.py index 9587f22f6cb5e9..882265debcac50 100644 --- a/homeassistant/components/notion/__init__.py +++ b/homeassistant/components/notion/__init__.py @@ -170,6 +170,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry_updates["data"][CONF_REFRESH_TOKEN] = client.refresh_token entry_updates["data"][CONF_USER_UUID] = client.user_uuid + hass.config_entries.async_update_entry(entry, **entry_updates) + @callback def async_save_refresh_token(refresh_token: str) -> None: """Save a refresh token to the config entry data.""" @@ -181,12 +183,6 @@ def async_save_refresh_token(refresh_token: str) -> None: # Create a callback to save the refresh token when it changes: 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: """Get the latest data from the Notion API.""" data = NotionData(hass=hass, entry=entry) From d2625be1919e5158c0dbfa0f49a8db7d80a50b89 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sat, 9 Mar 2024 11:11:37 -0700 Subject: [PATCH 2/2] Only change if necessary --- homeassistant/components/notion/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notion/__init__.py b/homeassistant/components/notion/__init__.py index 882265debcac50..9b62eb840c0e6d 100644 --- a/homeassistant/components/notion/__init__.py +++ b/homeassistant/components/notion/__init__.py @@ -166,9 +166,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: except NotionError as err: raise ConfigEntryNotReady("Config entry failed to load") from err - # Always update the config entry with the latest refresh token and user UUID: - entry_updates["data"][CONF_REFRESH_TOKEN] = client.refresh_token - entry_updates["data"][CONF_USER_UUID] = client.user_uuid + # Update the Notion user UUID and refresh token if they've changed: + for key, value in ( + (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)