Enhance SmartThings component subscription (#21124)

* Move to config v2 to store SmartApp oauth keys

* Add migration functionality.

* Regenerate refresh token on periodic basis

* Fix regenerate and misc. optimizations

* Review feedback

* Subscription sync logic now performs a difference operation

* Removed config entry reloading.
This commit is contained in:
Andrew Sayre authored and Martin Hjelmare committed 2019-02-22 20:35:12 +01:00
1 parent d9712027e8
commit 8b38b82e73
14 files changed
+529 -275

No files matched your search

@@ -13,15 +13,16 @@ from uuid import uuid4
from aiohttp import web
from homeassistant.components import webhook
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_WEBHOOK_ID
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, async_dispatcher_send)
from homeassistant.helpers.typing import HomeAssistantType
from .const import (
APP_NAME_PREFIX, APP_OAUTH_SCOPES, CONF_APP_ID, CONF_INSTALLED_APP_ID,
CONF_INSTANCE_ID, CONF_LOCATION_ID, DATA_BROKERS, DATA_MANAGER, DOMAIN,
APP_NAME_PREFIX, APP_OAUTH_CLIENT_NAME, APP_OAUTH_SCOPES, CONF_APP_ID,
CONF_INSTALLED_APP_ID, CONF_INSTALLED_APPS, CONF_INSTANCE_ID,
CONF_LOCATION_ID, CONF_REFRESH_TOKEN, DATA_BROKERS, DATA_MANAGER, DOMAIN,
SETTINGS_INSTANCE_ID, SIGNAL_SMARTAPP_PREFIX, STORAGE_KEY, STORAGE_VERSION)
_LOGGER = logging.getLogger(__name__)
@@ -83,7 +84,7 @@ async def create_app(hass: HomeAssistantType, api):
app = App()
for key, value in template.items():
setattr(app, key, value)
app = (await api.create_app(app))[0]
app, client = await api.create_app(app)
_LOGGER.debug("Created SmartApp '%s' (%s)", app.app_name, app.app_id)
# Set unique hass id in settings
@@ -97,12 +98,12 @@ async def create_app(hass: HomeAssistantType, api):
# Set oauth scopes
oauth = AppOAuth(app.app_id)
oauth.client_name = 'Home Assistant'
oauth.client_name = APP_OAUTH_CLIENT_NAME
oauth.scope.extend(APP_OAUTH_SCOPES)
await api.update_app_oauth(oauth)
_LOGGER.debug("Updated App OAuth for SmartApp '%s' (%s)",
app.app_name, app.app_id)
return app
return app, client
async def update_app(hass: HomeAssistantType, app):
@@ -185,32 +186,24 @@ async def setup_smartapp_endpoint(hass: HomeAssistantType):
DATA_MANAGER: manager,
CONF_INSTANCE_ID: config[CONF_INSTANCE_ID],
DATA_BROKERS: {},
CONF_WEBHOOK_ID: config[CONF_WEBHOOK_ID]
CONF_WEBHOOK_ID: config[CONF_WEBHOOK_ID],
CONF_INSTALLED_APPS: []
}
async def smartapp_sync_subscriptions(
hass: HomeAssistantType, auth_token: str, location_id: str,
installed_app_id: str, *, skip_delete=False):
installed_app_id: str, devices):
"""Synchronize subscriptions of an installed up."""
from pysmartthings import (
CAPABILITIES, SmartThings, SourceType, Subscription)
CAPABILITIES, SmartThings, SourceType, Subscription,
SubscriptionEntity
)
api = SmartThings(async_get_clientsession(hass), auth_token)
devices = await api.devices(location_ids=[location_id])
tasks = []
# Build set of capabilities and prune unsupported ones
capabilities = set()
for device in devices:
capabilities.update(device.capabilities)
capabilities.intersection_update(CAPABILITIES)
# Remove all (except for installs)
if not skip_delete:
await api.delete_subscriptions(installed_app_id)
# Create for each capability
async def create_subscription(target):
async def create_subscription(target: str):
sub = Subscription()
sub.installed_app_id = installed_app_id
sub.location_id = location_id
@@ -224,52 +217,89 @@ async def smartapp_sync_subscriptions(
_LOGGER.exception("Failed to create subscription for '%s' under "
"app '%s'", target, installed_app_id)
tasks = [create_subscription(c) for c in capabilities]
await asyncio.gather(*tasks)
async def delete_subscription(sub: SubscriptionEntity):
try:
await api.delete_subscription(
installed_app_id, sub.subscription_id)
_LOGGER.debug("Removed subscription for '%s' under app '%s' "
"because it was no longer needed",
sub.capability, installed_app_id)
except Exception: # pylint:disable=broad-except
_LOGGER.exception("Failed to remove subscription for '%s' under "
"app '%s'", sub.capability, installed_app_id)
# Build set of capabilities and prune unsupported ones
capabilities = set()
for device in devices:
capabilities.update(device.capabilities)
capabilities.intersection_update(CAPABILITIES)
# Get current subscriptions and find differences
subscriptions = await api.subscriptions(installed_app_id)
for subscription in subscriptions:
if subscription.capability in capabilities:
capabilities.remove(subscription.capability)
else:
# Delete the subscription
tasks.append(delete_subscription(subscription))
# Remaining capabilities need subscriptions created
tasks.extend([create_subscription(c) for c in capabilities])
if tasks:
await asyncio.gather(*tasks)
else:
_LOGGER.debug("Subscriptions for app '%s' are up-to-date",
installed_app_id)
async def smartapp_install(hass: HomeAssistantType, req, resp, app):
"""
Handle when a SmartApp is installed by the user into a location.
Setup subscriptions using the access token SmartThings provided in the
event. An explicit subscription is required for each 'capability' in order
to receive the related attribute updates. Finally, create a config entry
representing the installation if this is not the first installation under
the account.
Create a config entry representing the installation if this is not
the first installation under the account, otherwise store the data
for the config flow.
"""
await smartapp_sync_subscriptions(
hass, req.auth_token, req.location_id, req.installed_app_id,
skip_delete=True)
# The permanent access token is copied from another config flow with the
# same parent app_id. If one is not found, that means the user is within
# the initial config flow and the entry at the conclusion.
access_token = next((
entry.data.get(CONF_ACCESS_TOKEN) for entry
install_data = {
CONF_INSTALLED_APP_ID: req.installed_app_id,
CONF_LOCATION_ID: req.location_id,
CONF_REFRESH_TOKEN: req.refresh_token
}
# App attributes (client id/secret, etc...) are copied from another entry
# with the same parent app_id. If one is not found, the install data is
# stored for the config flow to retrieve during the wait step.
entry = next((
entry for entry
in hass.config_entries.async_entries(DOMAIN)
if entry.data[CONF_APP_ID] == app.app_id), None)
if access_token:
if entry:
data = entry.data.copy()
data.update(install_data)
# Add as job not needed because the current coroutine was invoked
# from the dispatcher and is not being awaited.
await hass.config_entries.flow.async_init(
DOMAIN, context={'source': 'install'},
data={
CONF_APP_ID: app.app_id,
CONF_INSTALLED_APP_ID: req.installed_app_id,
CONF_LOCATION_ID: req.location_id,
CONF_ACCESS_TOKEN: access_token
})
data=data)
else:
# Store the data where the flow can find it
hass.data[DOMAIN][CONF_INSTALLED_APPS].append(install_data)
async def smartapp_update(hass: HomeAssistantType, req, resp, app):
"""
Handle when a SmartApp is updated (reconfigured) by the user.
Synchronize subscriptions to ensure we're up-to-date.
Store the refresh token in the config entry.
"""
await smartapp_sync_subscriptions(
hass, req.auth_token, req.location_id, req.installed_app_id)
# Update refresh token in config entry
entry = next((entry for entry in hass.config_entries.async_entries(DOMAIN)
if entry.data.get(CONF_INSTALLED_APP_ID) ==
req.installed_app_id),
None)
if entry:
entry.data[CONF_REFRESH_TOKEN] = req.refresh_token
hass.config_entries.async_update_entry(entry)
_LOGGER.debug("SmartApp '%s' under parent app '%s' was updated",
req.installed_app_id, app.app_id)