Keep track of Alexa authorization status (#63979)

This commit is contained in:
Erik Montnemery
2022-01-13 18:47:31 +01:00
committed by GitHub
parent 49a32c398c
commit be628a7c4d
12 changed files with 204 additions and 23 deletions

View File

@@ -12,7 +12,7 @@ import pytest
from homeassistant.components.alexa import errors as alexa_errors
from homeassistant.components.alexa.entities import LightCapabilities
from homeassistant.components.cloud.const import DOMAIN, RequireRelink
from homeassistant.components.cloud.const import DOMAIN
from homeassistant.components.google_assistant.helpers import GoogleEntity
from homeassistant.core import State
from homeassistant.util.location import LocationInfo
@@ -414,6 +414,7 @@ async def test_websocket_status(
"exclude_entity_globs": [],
"exclude_entities": [],
},
"alexa_registered": False,
"google_entities": {
"include_domains": ["light"],
"include_entity_globs": [],
@@ -509,6 +510,28 @@ async def test_websocket_update_preferences(
assert setup_api.tts_default_voice == ("en-GB", "male")
async def test_websocket_update_preferences_alexa_report_state(
hass, hass_ws_client, aioclient_mock, setup_api, mock_cloud_login
):
"""Test updating alexa_report_state sets alexa authorized."""
client = await hass_ws_client(hass)
with patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig"
".async_get_access_token",
), patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig.set_authorized"
) as set_authorized_mock:
set_authorized_mock.assert_not_called()
await client.send_json(
{"id": 5, "type": "cloud/update_prefs", "alexa_report_state": True}
)
response = await client.receive_json()
set_authorized_mock.assert_called_once_with(True)
assert response["success"]
async def test_websocket_update_preferences_require_relink(
hass, hass_ws_client, aioclient_mock, setup_api, mock_cloud_login
):
@@ -518,12 +541,16 @@ async def test_websocket_update_preferences_require_relink(
with patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig"
".async_get_access_token",
side_effect=RequireRelink,
):
side_effect=alexa_errors.RequireRelink,
), patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig.set_authorized"
) as set_authorized_mock:
set_authorized_mock.assert_not_called()
await client.send_json(
{"id": 5, "type": "cloud/update_prefs", "alexa_report_state": True}
)
response = await client.receive_json()
set_authorized_mock.assert_called_once_with(False)
assert not response["success"]
assert response["error"]["code"] == "alexa_relink"
@@ -539,11 +566,15 @@ async def test_websocket_update_preferences_no_token(
"homeassistant.components.cloud.alexa_config.AlexaConfig"
".async_get_access_token",
side_effect=alexa_errors.NoTokenAvailable,
):
), patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig.set_authorized"
) as set_authorized_mock:
set_authorized_mock.assert_not_called()
await client.send_json(
{"id": 5, "type": "cloud/update_prefs", "alexa_report_state": True}
)
response = await client.receive_json()
set_authorized_mock.assert_called_once_with(False)
assert not response["success"]
assert response["error"]["code"] == "alexa_relink"