From 23e9be756d1091c301ed43f289e607bd2197ad37 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 27 Mar 2024 16:02:47 +0100 Subject: [PATCH] Fix exception when logging out from cloud (#114306) * Fix exception when logging out from cloud * Add test --- .../components/cloud/google_config.py | 6 ++- tests/components/cloud/test_google_config.py | 42 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/cloud/google_config.py b/homeassistant/components/cloud/google_config.py index 991ac88a44f..1ba2fab717f 100644 --- a/homeassistant/components/cloud/google_config.py +++ b/homeassistant/components/cloud/google_config.py @@ -400,7 +400,11 @@ class CloudGoogleConfig(AbstractConfig): @callback def async_get_agent_users(self) -> tuple: """Return known agent users.""" - if not self._prefs.google_connected or not self._cloud.username: + if ( + not self._cloud.is_logged_in # Can't call Cloud.username if not logged in + or not self._prefs.google_connected + or not self._cloud.username + ): return () return (self._cloud.username,) diff --git a/tests/components/cloud/test_google_config.py b/tests/components/cloud/test_google_config.py index 5ce390fd9a3..66530bfa3f8 100644 --- a/tests/components/cloud/test_google_config.py +++ b/tests/components/cloud/test_google_config.py @@ -1,7 +1,7 @@ """Test the Cloud Google Config.""" from http import HTTPStatus -from unittest.mock import Mock, patch +from unittest.mock import Mock, PropertyMock, patch from freezegun import freeze_time import pytest @@ -865,3 +865,43 @@ async def test_google_config_get_agent_user_id( == config.agent_user_id ) assert config.get_agent_user_id_from_webhook("other_id") != config.agent_user_id + + +async def test_google_config_get_agent_users( + hass: HomeAssistant, mock_cloud_login, cloud_prefs +) -> None: + """Test overridden async_get_agent_users method.""" + username_mock = PropertyMock(return_value="blah") + + # We should not call Cloud.username when not logged in + cloud_prefs._prefs["google_connected"] = True + assert cloud_prefs.google_connected + mock_cloud = Mock(is_logged_in=False) + type(mock_cloud).username = username_mock + config = CloudGoogleConfig( + hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, mock_cloud + ) + assert config.async_get_agent_users() == () + username_mock.assert_not_called() + + # We should not call Cloud.username when not connected + cloud_prefs._prefs["google_connected"] = False + assert not cloud_prefs.google_connected + mock_cloud = Mock(is_logged_in=True) + type(mock_cloud).username = username_mock + config = CloudGoogleConfig( + hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, mock_cloud + ) + assert config.async_get_agent_users() == () + username_mock.assert_not_called() + + # Logged in and connected + cloud_prefs._prefs["google_connected"] = True + assert cloud_prefs.google_connected + mock_cloud = Mock(is_logged_in=True) + type(mock_cloud).username = username_mock + config = CloudGoogleConfig( + hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, mock_cloud + ) + assert config.async_get_agent_users() == ("blah",) + username_mock.assert_called()