mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Clean up Alexa when logging out from cloud (#109738)
* Clean up Alexa when logging out from cloud * Add test
This commit is contained in:
parent
6fce8a5403
commit
b7284b92ac
@ -29,12 +29,20 @@ class AbstractConfig(ABC):
|
|||||||
"""Initialize abstract config."""
|
"""Initialize abstract config."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._enable_proactive_mode_lock = asyncio.Lock()
|
self._enable_proactive_mode_lock = asyncio.Lock()
|
||||||
|
self._on_deinitialize: list[CALLBACK_TYPE] = []
|
||||||
|
|
||||||
async def async_initialize(self) -> None:
|
async def async_initialize(self) -> None:
|
||||||
"""Perform async initialization of config."""
|
"""Perform async initialization of config."""
|
||||||
self._store = AlexaConfigStore(self.hass)
|
self._store = AlexaConfigStore(self.hass)
|
||||||
await self._store.async_load()
|
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
|
@property
|
||||||
def supports_auth(self) -> bool:
|
def supports_auth(self) -> bool:
|
||||||
"""Return if config supports auth."""
|
"""Return if config supports auth."""
|
||||||
|
@ -246,21 +246,27 @@ class CloudAlexaConfig(alexa_config.AbstractConfig):
|
|||||||
await self._prefs.async_update(
|
await self._prefs.async_update(
|
||||||
alexa_settings_version=ALEXA_SETTINGS_VERSION
|
alexa_settings_version=ALEXA_SETTINGS_VERSION
|
||||||
)
|
)
|
||||||
async_listen_entity_updates(
|
self._on_deinitialize.append(
|
||||||
self.hass, CLOUD_ALEXA, self._async_exposed_entities_updated
|
async_listen_entity_updates(
|
||||||
|
self.hass, CLOUD_ALEXA, self._async_exposed_entities_updated
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
async def on_hass_start(hass: HomeAssistant) -> None:
|
async def on_hass_start(hass: HomeAssistant) -> None:
|
||||||
if self.enabled and ALEXA_DOMAIN not in self.hass.config.components:
|
if self.enabled and ALEXA_DOMAIN not in self.hass.config.components:
|
||||||
await async_setup_component(self.hass, ALEXA_DOMAIN, {})
|
await async_setup_component(self.hass, ALEXA_DOMAIN, {})
|
||||||
|
|
||||||
start.async_at_start(self.hass, on_hass_start)
|
self._on_deinitialize.append(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_started(self.hass, on_hass_started))
|
||||||
|
|
||||||
self._prefs.async_listen_updates(self._async_prefs_updated)
|
self._on_deinitialize.append(
|
||||||
self.hass.bus.async_listen(
|
self._prefs.async_listen_updates(self._async_prefs_updated)
|
||||||
er.EVENT_ENTITY_REGISTRY_UPDATED,
|
)
|
||||||
self._handle_entity_registry_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:
|
def _should_expose_legacy(self, entity_id: str) -> bool:
|
||||||
|
@ -213,6 +213,10 @@ class CloudClient(Interface):
|
|||||||
"""Cleanup some stuff after logout."""
|
"""Cleanup some stuff after logout."""
|
||||||
await self.prefs.async_set_username(None)
|
await self.prefs.async_set_username(None)
|
||||||
|
|
||||||
|
if self._alexa_config:
|
||||||
|
self._alexa_config.async_deinitialize()
|
||||||
|
self._alexa_config = None
|
||||||
|
|
||||||
if self._google_config:
|
if self._google_config:
|
||||||
self._google_config.async_deinitialize()
|
self._google_config.async_deinitialize()
|
||||||
self._google_config = None
|
self._google_config = None
|
||||||
|
@ -526,6 +526,9 @@ async def test_alexa_handle_logout(
|
|||||||
return_value=Mock(),
|
return_value=Mock(),
|
||||||
) as mock_enable:
|
) as mock_enable:
|
||||||
await aconf.async_enable_proactive_mode()
|
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.
|
# This will trigger a prefs update when we logout.
|
||||||
await cloud_prefs.get_cloud_user()
|
await cloud_prefs.get_cloud_user()
|
||||||
@ -536,8 +539,13 @@ async def test_alexa_handle_logout(
|
|||||||
"async_check_token",
|
"async_check_token",
|
||||||
side_effect=AssertionError("Should not be called"),
|
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)
|
await cloud_prefs.async_set_username(None)
|
||||||
|
aconf.async_deinitialize()
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
# Check listeners are removed:
|
||||||
|
assert not aconf._on_deinitialize
|
||||||
|
|
||||||
assert len(mock_enable.return_value.mock_calls) == 1
|
assert len(mock_enable.return_value.mock_calls) == 1
|
||||||
|
|
||||||
|
@ -468,10 +468,11 @@ async def test_logged_out(
|
|||||||
await cloud.logout()
|
await cloud.logout()
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Alexa is not cleaned up, Google is
|
# Check we clean up Alexa and Google
|
||||||
assert cloud.client._alexa_config is alexa_config_mock
|
assert cloud.client._alexa_config is None
|
||||||
assert cloud.client._google_config is None
|
assert cloud.client._google_config is None
|
||||||
google_config_mock.async_deinitialize.assert_called_once_with()
|
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:
|
async def test_remote_enable(hass: HomeAssistant) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user