mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Google assistant storage of connected agents (#29158)
* Make async_report_state take agent_user_id * Attempt to store synced agents * Drop now not needed initialization * Make sure cloud uses the all sync on changed preferences * Some more places to use all version of sync * Get the agent_user_id from the request context if available * Minor cleanup * Remove the old fixed agent_user_id for cloud Instead pass along cloud_user where appropriate. * async_delay_save takes a function * Adjust test for delayed store * Remove unused save function * Add login check.
This commit is contained in:
committed by
Paulus Schoutsen
parent
434b783b4c
commit
2569c4ae37
@@ -12,7 +12,6 @@ from homeassistant.components.google_assistant.const import (
|
||||
REPORT_STATE_BASE_URL,
|
||||
HOMEGRAPH_TOKEN_URL,
|
||||
)
|
||||
from homeassistant.auth.models import User
|
||||
|
||||
DUMMY_CONFIG = GOOGLE_ASSISTANT_SCHEMA(
|
||||
{
|
||||
@@ -67,6 +66,7 @@ async def test_update_access_token(hass):
|
||||
jwt = "dummyjwt"
|
||||
|
||||
config = GoogleConfig(hass, DUMMY_CONFIG)
|
||||
await config.async_initialize()
|
||||
|
||||
base_time = datetime(2019, 10, 14, tzinfo=timezone.utc)
|
||||
with patch(
|
||||
@@ -99,6 +99,8 @@ async def test_update_access_token(hass):
|
||||
async def test_call_homegraph_api(hass, aioclient_mock, hass_storage):
|
||||
"""Test the function to call the homegraph api."""
|
||||
config = GoogleConfig(hass, DUMMY_CONFIG)
|
||||
await config.async_initialize()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.google_assistant.http._get_homegraph_token"
|
||||
) as mock_get_token:
|
||||
@@ -120,6 +122,8 @@ async def test_call_homegraph_api(hass, aioclient_mock, hass_storage):
|
||||
async def test_call_homegraph_api_retry(hass, aioclient_mock, hass_storage):
|
||||
"""Test the that the calls get retried with new token on 401."""
|
||||
config = GoogleConfig(hass, DUMMY_CONFIG)
|
||||
await config.async_initialize()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.google_assistant.http._get_homegraph_token"
|
||||
) as mock_get_token:
|
||||
@@ -143,8 +147,10 @@ async def test_call_homegraph_api_retry(hass, aioclient_mock, hass_storage):
|
||||
async def test_call_homegraph_api_key(hass, aioclient_mock, hass_storage):
|
||||
"""Test the function to call the homegraph api."""
|
||||
config = GoogleConfig(
|
||||
hass, GOOGLE_ASSISTANT_SCHEMA({"project_id": "1234", "api_key": "dummy_key"})
|
||||
hass, GOOGLE_ASSISTANT_SCHEMA({"project_id": "1234", "api_key": "dummy_key"}),
|
||||
)
|
||||
await config.async_initialize()
|
||||
|
||||
aioclient_mock.post(MOCK_URL, status=200, json={})
|
||||
|
||||
res = await config.async_call_homegraph_api_key(MOCK_URL, MOCK_JSON)
|
||||
@@ -159,8 +165,10 @@ async def test_call_homegraph_api_key(hass, aioclient_mock, hass_storage):
|
||||
async def test_call_homegraph_api_key_fail(hass, aioclient_mock, hass_storage):
|
||||
"""Test the function to call the homegraph api."""
|
||||
config = GoogleConfig(
|
||||
hass, GOOGLE_ASSISTANT_SCHEMA({"project_id": "1234", "api_key": "dummy_key"})
|
||||
hass, GOOGLE_ASSISTANT_SCHEMA({"project_id": "1234", "api_key": "dummy_key"}),
|
||||
)
|
||||
await config.async_initialize()
|
||||
|
||||
aioclient_mock.post(MOCK_URL, status=666, json={})
|
||||
|
||||
res = await config.async_call_homegraph_api_key(MOCK_URL, MOCK_JSON)
|
||||
@@ -170,17 +178,16 @@ async def test_call_homegraph_api_key_fail(hass, aioclient_mock, hass_storage):
|
||||
|
||||
async def test_report_state(hass, aioclient_mock, hass_storage):
|
||||
"""Test the report state function."""
|
||||
agent_user_id = "user"
|
||||
config = GoogleConfig(hass, DUMMY_CONFIG)
|
||||
await config.async_initialize()
|
||||
|
||||
await config.async_connect_agent_user(agent_user_id)
|
||||
message = {"devices": {}}
|
||||
owner = User(name="Test User", perm_lookup=None, groups=[], is_owner=True)
|
||||
|
||||
with patch.object(config, "async_call_homegraph_api") as mock_call, patch.object(
|
||||
hass.auth, "async_get_owner"
|
||||
) as mock_get_owner:
|
||||
mock_get_owner.return_value = owner
|
||||
|
||||
await config.async_report_state(message)
|
||||
with patch.object(config, "async_call_homegraph_api") as mock_call:
|
||||
await config.async_report_state(message, agent_user_id)
|
||||
mock_call.assert_called_once_with(
|
||||
REPORT_STATE_BASE_URL,
|
||||
{"requestId": ANY, "agentUserId": owner.id, "payload": message},
|
||||
{"requestId": ANY, "agentUserId": agent_user_id, "payload": message},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user