Skip to content

Commit

Permalink
Replace OptionFlow listener with explicit refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed May 22, 2022
1 parent 3334d58 commit 9e258db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 0 additions & 8 deletions custom_components/bmw_connected_drive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
)

# Add event listener for option flow changes
entry.async_on_unload(entry.add_update_listener(async_update_options))

return True


Expand All @@ -116,11 +113,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok


async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(config_entry.entry_id)


class BMWBaseEntity(CoordinatorEntity[BMWDataUpdateCoordinator]):
"""Common base for BMW entities."""

Expand Down
10 changes: 10 additions & 0 deletions custom_components/bmw_connected_drive/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ async def async_step_account_options(
) -> FlowResult:
"""Handle the initial step."""
if user_input is not None:
# Manually update & reload the config entry after options change.
# Required as each successful login will store the latest refresh_token
# using async_update_entry, which would otherwise trigger a full reload
# if the options would be refreshed using a listener.
changed = self.hass.config_entries.async_update_entry(
self.config_entry,
options=user_input,
)
if changed:
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(
step_id="account_options",
Expand Down
9 changes: 6 additions & 3 deletions custom_components/bmw_connected_drive/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from datetime import timedelta
import logging
from types import MappingProxyType

import async_timeout
from bimmer_connected.account import MyBMWAccount
Expand Down Expand Up @@ -61,6 +60,11 @@ async def _async_update_data(self) -> None:

if self.account.refresh_token != old_refresh_token:
self._update_config_entry_refresh_token(self.account.refresh_token)
_LOGGER.debug(
"bimmer_connected: refresh token %s > %s",
old_refresh_token,
self.account.refresh_token,
)

def _update_config_entry_refresh_token(self, refresh_token: str | None) -> None:
"""Update or delete the refresh_token in the Config Entry."""
Expand All @@ -70,8 +74,7 @@ def _update_config_entry_refresh_token(self, refresh_token: str | None) -> None:
}
if not refresh_token:
data.pop(CONF_REFRESH_TOKEN)
self._entry.data = MappingProxyType(data)
self.hass.config_entries._async_schedule_save() # pylint: disable=protected-access
self.hass.config_entries.async_update_entry(self._entry, data=data)

def notify_listeners(self) -> None:
"""Notify all listeners to refresh HA state machine."""
Expand Down

0 comments on commit 9e258db

Please sign in to comment.