Improve Alexa error handling (#24745)
This commit is contained in:
1 parent
d4fc22add4
commit
f5f86993f1
8 files changed
+121
-9
No files matched your search
@@ -103,6 +103,15 @@ class AlexaConfig(alexa_config.AbstractConfig):
|
||||
|
||||
if resp.status == 400:
|
||||
if body['reason'] in ('RefreshTokenNotFound', 'UnknownRegion'):
|
||||
if self.should_report_state:
|
||||
await self._prefs.async_update(alexa_report_state=False)
|
||||
self.hass.components.persistent_notification.async_create(
|
||||
"There was an error reporting state to Alexa ({}). "
|
||||
"Please re-link your Alexa skill via the Alexa app to "
|
||||
"continue using it.".format(body['reason']),
|
||||
"Alexa state reporting disabled",
|
||||
"cloud_alexa_report",
|
||||
)
|
||||
raise RequireRelink
|
||||
|
||||
raise alexa_errors.NoTokenAvailable
|
||||
@@ -200,6 +209,9 @@ class AlexaConfig(alexa_config.AbstractConfig):
|
||||
if not to_update and not to_remove:
|
||||
return True
|
||||
|
||||
# Make sure it's valid.
|
||||
await self.async_get_access_token()
|
||||
|
||||
tasks = []
|
||||
|
||||
if to_update:
|
||||
@@ -241,4 +253,7 @@ class AlexaConfig(alexa_config.AbstractConfig):
|
||||
elif action == 'remove' and self.should_expose(entity_id):
|
||||
to_remove.append(entity_id)
|
||||
|
||||
await self._sync_helper(to_update, to_remove)
|
||||
try:
|
||||
await self._sync_helper(to_update, to_remove)
|
||||
except alexa_errors.NoTokenAvailable:
|
||||
pass
|
||||
@@ -12,7 +12,10 @@ from homeassistant.components.google_assistant import smart_home as ga
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.util.aiohttp import MockRequest
|
||||
from homeassistant.components.alexa import smart_home as alexa_sh
|
||||
from homeassistant.components.alexa import (
|
||||
smart_home as alexa_sh,
|
||||
errors as alexa_errors,
|
||||
)
|
||||
|
||||
from . import utils, alexa_config, google_config
|
||||
from .const import DISPATCHER_REMOTE_UPDATE
|
||||
@@ -98,8 +101,14 @@ class CloudClient(Interface):
|
||||
"""Initialize the client."""
|
||||
self.cloud = cloud
|
||||
|
||||
if self.alexa_config.should_report_state and self.cloud.is_logged_in:
|
||||
if (not self.alexa_config.should_report_state or
|
||||
not self.cloud.is_logged_in):
|
||||
return
|
||||
|
||||
try:
|
||||
await self.alexa_config.async_enable_proactive_mode()
|
||||
except alexa_errors.NoTokenAvailable:
|
||||
pass
|
||||
|
||||
async def cleanups(self) -> None:
|
||||
"""Cleanup some stuff after logout."""
|
||||
|
||||
@@ -14,7 +14,10 @@ from homeassistant.components.http.data_validator import (
|
||||
RequestDataValidator)
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.components.websocket_api import const as ws_const
|
||||
from homeassistant.components.alexa import entities as alexa_entities
|
||||
from homeassistant.components.alexa import (
|
||||
entities as alexa_entities,
|
||||
errors as alexa_errors,
|
||||
)
|
||||
from homeassistant.components.google_assistant import helpers as google_helpers
|
||||
|
||||
from .const import (
|
||||
@@ -375,6 +378,24 @@ async def websocket_update_prefs(hass, connection, msg):
|
||||
changes = dict(msg)
|
||||
changes.pop('id')
|
||||
changes.pop('type')
|
||||
|
||||
# If we turn alexa linking on, validate that we can fetch access token
|
||||
if changes.get(PREF_ALEXA_REPORT_STATE):
|
||||
try:
|
||||
with async_timeout.timeout(10):
|
||||
await cloud.client.alexa_config.async_get_access_token()
|
||||
except asyncio.TimeoutError:
|
||||
connection.send_error(msg['id'], 'alexa_timeout',
|
||||
'Timeout validating Alexa access token.')
|
||||
return
|
||||
except alexa_errors.NoTokenAvailable:
|
||||
connection.send_error(
|
||||
msg['id'], 'alexa_relink',
|
||||
'Please go to the Alexa app and re-link the Home Assistant '
|
||||
'skill and then try to enable state reporting.'
|
||||
)
|
||||
return
|
||||
|
||||
await cloud.client.prefs.async_update(**changes)
|
||||
|
||||
connection.send_message(websocket_api.result_message(msg['id']))
|
||||
@@ -575,7 +596,15 @@ async def alexa_sync(hass, connection, msg):
|
||||
cloud = hass.data[DOMAIN]
|
||||
|
||||
with async_timeout.timeout(10):
|
||||
success = await cloud.client.alexa_config.async_sync_entities()
|
||||
try:
|
||||
success = await cloud.client.alexa_config.async_sync_entities()
|
||||
except alexa_errors.NoTokenAvailable:
|
||||
connection.send_error(
|
||||
msg['id'], 'alexa_relink',
|
||||
'Please go to the Alexa app and re-link the Home Assistant '
|
||||
'skill.'
|
||||
)
|
||||
return
|
||||
|
||||
if success:
|
||||
connection.send_result(msg['id'])
|
||||
|
||||
Reference in new issue
Block a user