From 0cb913370f3c006fb93481b03a5a83ad1e2fa43e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 6 Feb 2024 15:40:12 +0100 Subject: [PATCH] Rename google_assistant.AbstractConfig.get_local_agent_user_id (#109798) * Rename google_assistant.AbstractConfig get_local_agent_user_id to get_local_user_id * Fix --- homeassistant/components/cloud/google_config.py | 8 ++++++-- .../components/google_assistant/helpers.py | 13 +++++++++---- tests/components/google_assistant/test_http.py | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/cloud/google_config.py b/homeassistant/components/cloud/google_config.py index 10601bf4784..98dee500421 100644 --- a/homeassistant/components/cloud/google_config.py +++ b/homeassistant/components/cloud/google_config.py @@ -173,8 +173,12 @@ class CloudGoogleConfig(AbstractConfig): """Return the webhook ID to be used for actions for a given agent user id via the local SDK.""" return self._prefs.google_local_webhook_id - def get_local_agent_user_id(self, webhook_id: Any) -> str: - """Return the user ID to be used for actions received via the local SDK.""" + def get_local_user_id(self, webhook_id: Any) -> str: + """Map webhook ID to a Home Assistant user ID. + + Any action inititated by Google Assistant via the local SDK will be attributed + to the returned user ID. + """ return self._user @property diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 84cbdb6211e..7d431f8c94c 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -167,11 +167,16 @@ class AbstractConfig(ABC): and self._local_last_active > utcnow() - timedelta(seconds=70) ) - def get_local_agent_user_id(self, webhook_id): - """Return the user ID to be used for actions received via the local SDK. + def get_local_user_id(self, webhook_id): + """Map webhook ID to a Home Assistant user ID. - Return None is no agent user id is found. + Any action inititated by Google Assistant via the local SDK will be attributed + to the returned user ID. + + Return None if no user id is found for the webhook_id. """ + # Note: The manually setup Google Assistant currently returns the Google agent + # user ID instead of a valid Home Assistant user ID found_agent_user_id = None for agent_user_id, agent_user_data in self._store.agent_user_ids.items(): if agent_user_data[STORE_GOOGLE_LOCAL_WEBHOOK_ID] == webhook_id: @@ -423,7 +428,7 @@ class AbstractConfig(ABC): pprint.pformat(async_redact_request_msg(payload)), ) - if (agent_user_id := self.get_local_agent_user_id(webhook_id)) is None: + if (agent_user_id := self.get_local_user_id(webhook_id)) is None: # No agent user linked to this webhook, means that the user has somehow unregistered # removing webhook and stopping processing of this request. _LOGGER.error( diff --git a/tests/components/google_assistant/test_http.py b/tests/components/google_assistant/test_http.py index c6589555c3e..b3ec486e818 100644 --- a/tests/components/google_assistant/test_http.py +++ b/tests/components/google_assistant/test_http.py @@ -258,8 +258,8 @@ async def test_google_config_local_fulfillment( await hass.async_block_till_done() assert config.get_local_webhook_id(agent_user_id) == local_webhook_id - assert config.get_local_agent_user_id(local_webhook_id) == agent_user_id - assert config.get_local_agent_user_id("INCORRECT") is None + assert config.get_local_user_id(local_webhook_id) == agent_user_id + assert config.get_local_user_id("INCORRECT") is None async def test_secure_device_pin_config(hass: HomeAssistant) -> None: