Cloud: Add Alexa report state (#24536)
* Cloud: Add Alexa report state * Lint * Lint * Only track state changes when we are logged in
This commit is contained in:
1 parent
5ab1996d3f
commit
a02b69db38
17 files changed
+207
-33
No files matched your search
@@ -1,14 +1,26 @@
|
||||
"""Config helpers for Alexa."""
|
||||
from .state_report import async_enable_proactive_mode
|
||||
|
||||
|
||||
class AbstractConfig:
|
||||
"""Hold the configuration for Alexa."""
|
||||
|
||||
_unsub_proactive_report = None
|
||||
|
||||
def __init__(self, hass):
|
||||
"""Initialize abstract config."""
|
||||
self.hass = hass
|
||||
|
||||
@property
|
||||
def supports_auth(self):
|
||||
"""Return if config supports auth."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def should_report_state(self):
|
||||
"""Return if states should be proactively reported."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def endpoint(self):
|
||||
"""Endpoint for report state."""
|
||||
@@ -19,6 +31,30 @@ class AbstractConfig:
|
||||
"""Return entity config."""
|
||||
return {}
|
||||
|
||||
@property
|
||||
def is_reporting_states(self):
|
||||
"""Return if proactive mode is enabled."""
|
||||
return self._unsub_proactive_report is not None
|
||||
|
||||
async def async_enable_proactive_mode(self):
|
||||
"""Enable proactive mode."""
|
||||
if self._unsub_proactive_report is None:
|
||||
self._unsub_proactive_report = self.hass.async_create_task(
|
||||
async_enable_proactive_mode(self.hass, self)
|
||||
)
|
||||
resp = await self._unsub_proactive_report
|
||||
|
||||
# Failed to start reporting.
|
||||
if resp is None:
|
||||
self._unsub_proactive_report = None
|
||||
|
||||
async def async_disable_proactive_mode(self):
|
||||
"""Disable proactive mode."""
|
||||
unsub_func = await self._unsub_proactive_report
|
||||
if unsub_func:
|
||||
unsub_func()
|
||||
self._unsub_proactive_report = None
|
||||
|
||||
def should_expose(self, entity_id):
|
||||
"""If an entity should be exposed."""
|
||||
# pylint: disable=no-self-use
|
||||
|
||||
@@ -87,7 +87,9 @@ async def async_api_accept_grant(hass, config, directive, context):
|
||||
|
||||
if config.supports_auth:
|
||||
await config.async_accept_grant(auth_code)
|
||||
await async_enable_proactive_mode(hass, config)
|
||||
|
||||
if config.should_report_state:
|
||||
await async_enable_proactive_mode(hass, config)
|
||||
|
||||
return directive.response(
|
||||
name='AcceptGrant.Response',
|
||||
|
||||
@@ -25,6 +25,7 @@ class AlexaConfig(AbstractConfig):
|
||||
|
||||
def __init__(self, hass, config):
|
||||
"""Initialize Alexa config."""
|
||||
super().__init__(hass)
|
||||
self._config = config
|
||||
|
||||
if config.get(CONF_CLIENT_ID) and config.get(CONF_CLIENT_SECRET):
|
||||
@@ -38,6 +39,11 @@ class AlexaConfig(AbstractConfig):
|
||||
"""Return if config supports auth."""
|
||||
return self._auth is not None
|
||||
|
||||
@property
|
||||
def should_report_state(self):
|
||||
"""Return if we should proactively report states."""
|
||||
return self._auth is not None
|
||||
|
||||
@property
|
||||
def endpoint(self):
|
||||
"""Endpoint for report state."""
|
||||
@@ -73,7 +79,7 @@ async def async_setup(hass, config):
|
||||
smart_home_config = AlexaConfig(hass, config)
|
||||
hass.http.register_view(SmartHomeView(smart_home_config))
|
||||
|
||||
if smart_home_config.supports_auth:
|
||||
if smart_home_config.should_report_state:
|
||||
await async_enable_proactive_mode(hass, smart_home_config)
|
||||
|
||||
|
||||
|
||||
@@ -21,24 +21,23 @@ async def async_enable_proactive_mode(hass, smart_home_config):
|
||||
|
||||
Proactive mode makes this component report state changes to Alexa.
|
||||
"""
|
||||
if smart_home_config.async_get_access_token is None:
|
||||
# no function to call to get token
|
||||
return
|
||||
|
||||
if await smart_home_config.async_get_access_token() is None:
|
||||
# not ready yet
|
||||
return
|
||||
|
||||
async def async_entity_state_listener(changed_entity, old_state,
|
||||
new_state):
|
||||
if not smart_home_config.should_expose(changed_entity):
|
||||
_LOGGER.debug("Not exposing %s because filtered by config",
|
||||
changed_entity)
|
||||
if not new_state:
|
||||
return
|
||||
|
||||
if new_state.domain not in ENTITY_ADAPTERS:
|
||||
return
|
||||
|
||||
if not smart_home_config.should_expose(changed_entity):
|
||||
_LOGGER.debug("Not exposing %s because filtered by config",
|
||||
changed_entity)
|
||||
return
|
||||
|
||||
alexa_changed_entity = \
|
||||
ENTITY_ADAPTERS[new_state.domain](hass, smart_home_config,
|
||||
new_state)
|
||||
@@ -49,7 +48,7 @@ async def async_enable_proactive_mode(hass, smart_home_config):
|
||||
alexa_changed_entity)
|
||||
return
|
||||
|
||||
hass.helpers.event.async_track_state_change(
|
||||
return hass.helpers.event.async_track_state_change(
|
||||
MATCH_ALL, async_entity_state_listener
|
||||
)
|
||||
|
||||
@@ -94,7 +93,7 @@ async def async_send_changereport_message(hass, config, alexa_entity):
|
||||
allow_redirects=True)
|
||||
|
||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||
_LOGGER.error("Timeout calling LWA to get auth token.")
|
||||
_LOGGER.error("Timeout sending report to Alexa.")
|
||||
return None
|
||||
|
||||
response_text = await response.text()
|
||||
|
||||
Reference in new issue
Block a user