Do not break Alexa sync when encounter bad entity (#39380)

This commit is contained in:
Paulus Schoutsen 2020-08-30 14:36:00 +02:00 committed by GitHub
parent b4db9f615d
commit ba75856f2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 29 deletions

View file

@ -1,6 +1,6 @@
"""Alexa entity adapters."""
import logging
from typing import List
from typing import TYPE_CHECKING, List
from homeassistant.components import (
alarm_control_panel,
@ -34,7 +34,7 @@ from homeassistant.const import (
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, State, callback
from homeassistant.helpers import network
from homeassistant.util.decorator import Registry
@ -42,6 +42,7 @@ from .capabilities import (
Alexa,
AlexaBrightnessController,
AlexaCameraStreamController,
AlexaCapability,
AlexaChannelController,
AlexaColorController,
AlexaColorTemperatureController,
@ -72,6 +73,9 @@ from .capabilities import (
)
from .const import CONF_DESCRIPTION, CONF_DISPLAY_CATEGORIES
if TYPE_CHECKING:
from .config import AbstractConfig
_LOGGER = logging.getLogger(__name__)
ENTITY_ADAPTERS = Registry()
@ -203,7 +207,7 @@ class AlexaEntity:
The API handlers should manipulate entities only through this interface.
"""
def __init__(self, hass, config, entity):
def __init__(self, hass: HomeAssistant, config: "AbstractConfig", entity: State):
"""Initialize Alexa Entity."""
self.hass = hass
self.config = config
@ -246,13 +250,13 @@ class AlexaEntity:
"""
raise NotImplementedError
def get_interface(self, capability):
def get_interface(self, capability) -> AlexaCapability:
"""Return the given AlexaInterface.
Raises _UnsupportedInterface.
"""
def interfaces(self):
def interfaces(self) -> List[AlexaCapability]:
"""Return a list of supported interfaces.
Used for discovery. The list should contain AlexaInterface instances.
@ -280,11 +284,18 @@ class AlexaEntity:
}
locale = self.config.locale
capabilities = [
i.serialize_discovery()
for i in self.interfaces()
if locale in i.supported_locales
]
capabilities = []
for i in self.interfaces():
if locale not in i.supported_locales:
continue
try:
capabilities.append(i.serialize_discovery())
except Exception: # pylint: disable=broad-except
_LOGGER.exception(
"Error serializing %s discovery for %s", i.name(), self.entity
)
result["capabilities"] = capabilities