Skip to content

Commit

Permalink
Clean up Alexa when logging out from cloud (home-assistant#109738)
Browse files Browse the repository at this point in the history
* Clean up Alexa when logging out from cloud

* Add test
  • Loading branch information
emontnemery authored Feb 5, 2024
1 parent 6fce8a5 commit b7284b9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
8 changes: 8 additions & 0 deletions homeassistant/components/alexa/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ def __init__(self, hass: HomeAssistant) -> None:
"""Initialize abstract config."""
self.hass = hass
self._enable_proactive_mode_lock = asyncio.Lock()
self._on_deinitialize: list[CALLBACK_TYPE] = []

async def async_initialize(self) -> None:
"""Perform async initialization of config."""
self._store = AlexaConfigStore(self.hass)
await self._store.async_load()

@callback
def async_deinitialize(self) -> None:
"""Remove listeners."""
_LOGGER.debug("async_deinitialize")
while self._on_deinitialize:
self._on_deinitialize.pop()()

@property
def supports_auth(self) -> bool:
"""Return if config supports auth."""
Expand Down
22 changes: 14 additions & 8 deletions homeassistant/components/cloud/alexa_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,27 @@ async def on_hass_started(hass: HomeAssistant) -> None:
await self._prefs.async_update(
alexa_settings_version=ALEXA_SETTINGS_VERSION
)
async_listen_entity_updates(
self.hass, CLOUD_ALEXA, self._async_exposed_entities_updated
self._on_deinitialize.append(
async_listen_entity_updates(
self.hass, CLOUD_ALEXA, self._async_exposed_entities_updated
)
)

async def on_hass_start(hass: HomeAssistant) -> None:
if self.enabled and ALEXA_DOMAIN not in self.hass.config.components:
await async_setup_component(self.hass, ALEXA_DOMAIN, {})

start.async_at_start(self.hass, on_hass_start)
start.async_at_started(self.hass, on_hass_started)
self._on_deinitialize.append(start.async_at_start(self.hass, on_hass_start))
self._on_deinitialize.append(start.async_at_started(self.hass, on_hass_started))

self._prefs.async_listen_updates(self._async_prefs_updated)
self.hass.bus.async_listen(
er.EVENT_ENTITY_REGISTRY_UPDATED,
self._handle_entity_registry_updated,
self._on_deinitialize.append(
self._prefs.async_listen_updates(self._async_prefs_updated)
)
self._on_deinitialize.append(
self.hass.bus.async_listen(
er.EVENT_ENTITY_REGISTRY_UPDATED,
self._handle_entity_registry_updated,
)
)

def _should_expose_legacy(self, entity_id: str) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ async def logout_cleanups(self) -> None:
"""Cleanup some stuff after logout."""
await self.prefs.async_set_username(None)

if self._alexa_config:
self._alexa_config.async_deinitialize()
self._alexa_config = None

if self._google_config:
self._google_config.async_deinitialize()
self._google_config = None
Expand Down
8 changes: 8 additions & 0 deletions tests/components/cloud/test_alexa_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ async def test_alexa_handle_logout(
return_value=Mock(),
) as mock_enable:
await aconf.async_enable_proactive_mode()
await hass.async_block_till_done()

assert len(aconf._on_deinitialize) == 5

# This will trigger a prefs update when we logout.
await cloud_prefs.get_cloud_user()
Expand All @@ -536,8 +539,13 @@ async def test_alexa_handle_logout(
"async_check_token",
side_effect=AssertionError("Should not be called"),
):
# Fake logging out; CloudClient.logout_cleanups sets username to None
# and deinitializes the Google config.
await cloud_prefs.async_set_username(None)
aconf.async_deinitialize()
await hass.async_block_till_done()
# Check listeners are removed:
assert not aconf._on_deinitialize

assert len(mock_enable.return_value.mock_calls) == 1

Expand Down
5 changes: 3 additions & 2 deletions tests/components/cloud/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,11 @@ async def test_logged_out(
await cloud.logout()
await hass.async_block_till_done()

# Alexa is not cleaned up, Google is
assert cloud.client._alexa_config is alexa_config_mock
# Check we clean up Alexa and Google
assert cloud.client._alexa_config is None
assert cloud.client._google_config is None
google_config_mock.async_deinitialize.assert_called_once_with()
alexa_config_mock.async_deinitialize.assert_called_once_with()


async def test_remote_enable(hass: HomeAssistant) -> None:
Expand Down

0 comments on commit b7284b9

Please sign in to comment.