Add SmartThings Scene platform (#21405)

* Add SmartThings Scene platform

* Fixed failing tests after rebase

* Update cover tests.
This commit is contained in:
Andrew Sayre authored and Paulus Schoutsen committed 2019-02-26 13:12:24 -08:00
1 parent 344e839bec
commit 3b9db88065
14 files changed
+266 -72

No files matched your search

@@ -19,7 +19,7 @@ from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from .config_flow import SmartThingsFlowHandler # noqa
from .const import (
CONF_APP_ID, CONF_INSTALLED_APP_ID, CONF_OAUTH_CLIENT_ID,
CONF_APP_ID, CONF_INSTALLED_APP_ID, CONF_LOCATION_ID, CONF_OAUTH_CLIENT_ID,
CONF_OAUTH_CLIENT_SECRET, CONF_REFRESH_TOKEN, DATA_BROKERS, DATA_MANAGER,
DOMAIN, EVENT_BUTTON, SIGNAL_SMARTTHINGS_UPDATE, SUPPORTED_PLATFORMS,
TOKEN_REFRESH_INTERVAL)
@@ -93,6 +93,9 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
installed_app = await validate_installed_app(
api, entry.data[CONF_INSTALLED_APP_ID])
# Get scenes
scenes = await async_get_entry_scenes(entry, api)
# Get SmartApp token to sync subscriptions
token = await api.generate_tokens(
entry.data[CONF_OAUTH_CLIENT_ID],
@@ -123,7 +126,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
installed_app.installed_app_id, devices)
# Setup device broker
broker = DeviceBroker(hass, entry, token, smart_app, devices)
broker = DeviceBroker(hass, entry, token, smart_app, devices, scenes)
broker.connect()
hass.data[DOMAIN][DATA_BROKERS][entry.entry_id] = broker
@@ -156,6 +159,20 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
return True
async def async_get_entry_scenes(entry: ConfigEntry, api):
"""Get the scenes within an integration."""
try:
return await api.scenes(location_id=entry.data[CONF_LOCATION_ID])
except ClientResponseError as ex:
if ex.status == 403:
_LOGGER.exception("Unable to load scenes for config entry '%s' "
"because the access token does not have the "
"required access", entry.title)
else:
raise
return []
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Unload a config entry."""
broker = hass.data[DOMAIN][DATA_BROKERS].pop(entry.entry_id, None)
@@ -171,7 +188,7 @@ class DeviceBroker:
"""Manages an individual SmartThings config entry."""
def __init__(self, hass: HomeAssistantType, entry: ConfigEntry,
token, smart_app, devices: Iterable):
token, smart_app, devices: Iterable, scenes: Iterable):
"""Create a new instance of the DeviceBroker."""
self._hass = hass
self._entry = entry
@@ -182,6 +199,7 @@ class DeviceBroker:
self._regenerate_token_remove = None
self._assignments = self._assign_capabilities(devices)
self.devices = {device.device_id: device for device in devices}
self.scenes = {scene.scene_id: scene for scene in scenes}
def _assign_capabilities(self, devices: Iterable):
"""Assign platforms to capabilities."""
@@ -192,6 +210,8 @@ class DeviceBroker:
for platform_name in SUPPORTED_PLATFORMS:
platform = importlib.import_module(
'.' + platform_name, self.__module__)
if not hasattr(platform, 'get_capabilities'):
continue
assigned = platform.get_capabilities(capabilities)
if not assigned:
continue
@@ -34,7 +34,8 @@ SUPPORTED_PLATFORMS = [
'cover',
'switch',
'binary_sensor',
'sensor'
'sensor',
'scene'
]
TOKEN_REFRESH_INTERVAL = timedelta(days=14)
VAL_UID = "^(?:([0-9a-fA-F]{32})|([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]" \
@@ -0,0 +1,50 @@
"""Support for scenes through the SmartThings cloud API."""
from homeassistant.components.scene import Scene
from .const import DATA_BROKERS, DOMAIN
DEPENDENCIES = ['smartthings']
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Platform uses config entry setup."""
pass
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add switches for a config entry."""
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
async_add_entities(
[SmartThingsScene(scene) for scene in broker.scenes.values()])
class SmartThingsScene(Scene):
"""Define a SmartThings scene."""
def __init__(self, scene):
"""Init the scene class."""
self._scene = scene
async def async_activate(self):
"""Activate scene."""
await self._scene.execute()
@property
def device_state_attributes(self):
"""Get attributes about the state."""
return {
'icon': self._scene.icon,
'color': self._scene.color,
'location_id': self._scene.location_id
}
@property
def name(self) -> str:
"""Return the name of the device."""
return self._scene.name
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._scene.scene_id