Alexa to not use customize for entity config (#11461)
* Alexa to not use customize for entity config * Test Alexa entity config * Improve tests * Fix test
This commit is contained in:
1 parent
71fb7a6ef6
commit
8b57777ce9
5 files changed
+140
-57
No files matched your search
@@ -12,6 +12,7 @@ import voluptuous as vol
|
||||
from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_START, CONF_REGION, CONF_MODE)
|
||||
from homeassistant.helpers import entityfilter
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.components.alexa import smart_home as alexa_sh
|
||||
@@ -25,7 +26,7 @@ REQUIREMENTS = ['warrant==0.6.1']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_ALEXA = 'alexa'
|
||||
CONF_GOOGLE_ASSISTANT = 'google_assistant'
|
||||
CONF_GOOGLE_ACTIONS = 'google_actions'
|
||||
CONF_FILTER = 'filter'
|
||||
CONF_COGNITO_CLIENT_ID = 'cognito_client_id'
|
||||
CONF_RELAYER = 'relayer'
|
||||
@@ -35,6 +36,14 @@ MODE_DEV = 'development'
|
||||
DEFAULT_MODE = 'production'
|
||||
DEPENDENCIES = ['http']
|
||||
|
||||
CONF_ENTITY_CONFIG = 'entity_config'
|
||||
|
||||
ALEXA_ENTITY_SCHEMA = vol.Schema({
|
||||
vol.Optional(alexa_sh.CONF_DESCRIPTION): cv.string,
|
||||
vol.Optional(alexa_sh.CONF_DISPLAY_CATEGORIES): cv.string,
|
||||
vol.Optional(alexa_sh.CONF_NAME): cv.string,
|
||||
})
|
||||
|
||||
ASSISTANT_SCHEMA = vol.Schema({
|
||||
vol.Optional(
|
||||
CONF_FILTER,
|
||||
@@ -42,6 +51,10 @@ ASSISTANT_SCHEMA = vol.Schema({
|
||||
): entityfilter.FILTER_SCHEMA,
|
||||
})
|
||||
|
||||
ALEXA_SCHEMA = ASSISTANT_SCHEMA.extend({
|
||||
vol.Optional(CONF_ENTITY_CONFIG): {cv.entity_id: ALEXA_ENTITY_SCHEMA}
|
||||
})
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_MODE, default=DEFAULT_MODE):
|
||||
@@ -51,8 +64,8 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_USER_POOL_ID): str,
|
||||
vol.Optional(CONF_REGION): str,
|
||||
vol.Optional(CONF_RELAYER): str,
|
||||
vol.Optional(CONF_ALEXA): ASSISTANT_SCHEMA,
|
||||
vol.Optional(CONF_GOOGLE_ASSISTANT): ASSISTANT_SCHEMA,
|
||||
vol.Optional(CONF_ALEXA): ALEXA_SCHEMA,
|
||||
vol.Optional(CONF_GOOGLE_ACTIONS): ASSISTANT_SCHEMA,
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
@@ -61,18 +74,19 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
def async_setup(hass, config):
|
||||
"""Initialize the Home Assistant cloud."""
|
||||
if DOMAIN in config:
|
||||
kwargs = config[DOMAIN]
|
||||
kwargs = dict(config[DOMAIN])
|
||||
else:
|
||||
kwargs = {CONF_MODE: DEFAULT_MODE}
|
||||
|
||||
if CONF_ALEXA not in kwargs:
|
||||
kwargs[CONF_ALEXA] = ASSISTANT_SCHEMA({})
|
||||
alexa_conf = kwargs.pop(CONF_ALEXA, None) or ALEXA_SCHEMA({})
|
||||
gactions_conf = (kwargs.pop(CONF_GOOGLE_ACTIONS, None) or
|
||||
ASSISTANT_SCHEMA({}))
|
||||
|
||||
if CONF_GOOGLE_ASSISTANT not in kwargs:
|
||||
kwargs[CONF_GOOGLE_ASSISTANT] = ASSISTANT_SCHEMA({})
|
||||
|
||||
kwargs[CONF_ALEXA] = alexa_sh.Config(**kwargs[CONF_ALEXA])
|
||||
kwargs['gass_should_expose'] = kwargs.pop(CONF_GOOGLE_ASSISTANT)['filter']
|
||||
kwargs[CONF_ALEXA] = alexa_sh.Config(
|
||||
should_expose=alexa_conf[CONF_FILTER],
|
||||
entity_config=alexa_conf.get(CONF_ENTITY_CONFIG),
|
||||
)
|
||||
kwargs['gactions_should_expose'] = gactions_conf[CONF_FILTER]
|
||||
cloud = hass.data[DOMAIN] = Cloud(hass, **kwargs)
|
||||
|
||||
success = yield from cloud.initialize()
|
||||
@@ -87,15 +101,15 @@ def async_setup(hass, config):
|
||||
class Cloud:
|
||||
"""Store the configuration of the cloud connection."""
|
||||
|
||||
def __init__(self, hass, mode, alexa, gass_should_expose,
|
||||
def __init__(self, hass, mode, alexa, gactions_should_expose,
|
||||
cognito_client_id=None, user_pool_id=None, region=None,
|
||||
relayer=None):
|
||||
"""Create an instance of Cloud."""
|
||||
self.hass = hass
|
||||
self.mode = mode
|
||||
self.alexa_config = alexa
|
||||
self._gass_should_expose = gass_should_expose
|
||||
self._gass_config = None
|
||||
self._gactions_should_expose = gactions_should_expose
|
||||
self._gactions_config = None
|
||||
self.jwt_keyset = None
|
||||
self.id_token = None
|
||||
self.access_token = None
|
||||
@@ -144,15 +158,19 @@ class Cloud:
|
||||
return self.path('{}_auth.json'.format(self.mode))
|
||||
|
||||
@property
|
||||
def gass_config(self):
|
||||
def gactions_config(self):
|
||||
"""Return the Google Assistant config."""
|
||||
if self._gass_config is None:
|
||||
self._gass_config = ga_sh.Config(
|
||||
should_expose=self._gass_should_expose,
|
||||
if self._gactions_config is None:
|
||||
def should_expose(entity):
|
||||
"""If an entity should be exposed."""
|
||||
return self._gactions_should_expose(entity.entity_id)
|
||||
|
||||
self._gactions_config = ga_sh.Config(
|
||||
should_expose=should_expose,
|
||||
agent_user_id=self.claims['cognito:username']
|
||||
)
|
||||
|
||||
return self._gass_config
|
||||
return self._gactions_config
|
||||
|
||||
@asyncio.coroutine
|
||||
def initialize(self):
|
||||
@@ -182,7 +200,7 @@ class Cloud:
|
||||
self.id_token = None
|
||||
self.access_token = None
|
||||
self.refresh_token = None
|
||||
self._gass_config = None
|
||||
self._gactions_config = None
|
||||
|
||||
yield from self.hass.async_add_job(
|
||||
lambda: os.remove(self.user_info_path))
|
||||
|
||||
@@ -214,7 +214,7 @@ def async_handle_alexa(hass, cloud, payload):
|
||||
@asyncio.coroutine
|
||||
def async_handle_google_actions(hass, cloud, payload):
|
||||
"""Handle an incoming IoT message for Google Actions."""
|
||||
result = yield from ga.async_handle_message(hass, cloud.gass_config,
|
||||
result = yield from ga.async_handle_message(hass, cloud.gactions_config,
|
||||
payload)
|
||||
return result
|
||||
|
||||
|
||||
Reference in new issue
Block a user