2019-02-13 20:21:14 +00:00
|
|
|
"""Support for LIFX Cloud scenes."""
|
2017-04-16 23:40:12 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import aiohttp
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import AUTHORIZATION
|
2017-04-16 23:40:12 +00:00
|
|
|
import async_timeout
|
2017-11-04 19:04:05 +00:00
|
|
|
import voluptuous as vol
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
from homeassistant.components.scene import Scene
|
2017-11-04 19:04:05 +00:00
|
|
|
from homeassistant.const import CONF_TOKEN, CONF_TIMEOUT, CONF_PLATFORM
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2017-04-16 23:40:12 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
LIFX_API_URL = "https://api.lifx.com/v1/{0}"
|
2017-04-16 23:40:12 +00:00
|
|
|
DEFAULT_TIMEOUT = 10
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PLATFORM): "lifx_cloud",
|
|
|
|
vol.Required(CONF_TOKEN): cv.string,
|
|
|
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the scenes stored in the LIFX Cloud."""
|
2017-04-16 23:40:12 +00:00
|
|
|
token = config.get(CONF_TOKEN)
|
|
|
|
timeout = config.get(CONF_TIMEOUT)
|
|
|
|
|
2019-09-03 19:14:00 +00:00
|
|
|
headers = {AUTHORIZATION: f"Bearer {token}"}
|
2017-04-16 23:40:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = LIFX_API_URL.format("scenes")
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
httpsession = async_get_clientsession(hass)
|
2019-05-23 04:09:59 +00:00
|
|
|
with async_timeout.timeout(timeout):
|
2018-10-01 06:56:50 +00:00
|
|
|
scenes_resp = await httpsession.get(url, headers=headers)
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
|
|
|
_LOGGER.exception("Error on %s", url)
|
|
|
|
return False
|
|
|
|
|
|
|
|
status = scenes_resp.status
|
|
|
|
if status == 200:
|
2018-10-01 06:56:50 +00:00
|
|
|
data = await scenes_resp.json()
|
2017-04-16 23:40:12 +00:00
|
|
|
devices = []
|
|
|
|
for scene in data:
|
|
|
|
devices.append(LifxCloudScene(hass, headers, timeout, scene))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(devices)
|
2017-04-16 23:40:12 +00:00
|
|
|
return True
|
2018-07-23 08:16:05 +00:00
|
|
|
if status == 401:
|
2017-04-16 23:40:12 +00:00
|
|
|
_LOGGER.error("Unauthorized (bad token?) on %s", url)
|
|
|
|
return False
|
2017-07-06 06:30:01 +00:00
|
|
|
|
|
|
|
_LOGGER.error("HTTP error %d on %s", scenes_resp.status, url)
|
|
|
|
return False
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LifxCloudScene(Scene):
|
|
|
|
"""Representation of a LIFX Cloud scene."""
|
|
|
|
|
|
|
|
def __init__(self, hass, headers, timeout, scene_data):
|
|
|
|
"""Initialize the scene."""
|
|
|
|
self.hass = hass
|
|
|
|
self._headers = headers
|
|
|
|
self._timeout = timeout
|
|
|
|
self._name = scene_data["name"]
|
|
|
|
self._uuid = scene_data["uuid"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the scene."""
|
|
|
|
return self._name
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_activate(self):
|
2017-04-16 23:40:12 +00:00
|
|
|
"""Activate the scene."""
|
2019-07-31 19:25:30 +00:00
|
|
|
url = LIFX_API_URL.format("scenes/scene_id:%s/activate" % self._uuid)
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
httpsession = async_get_clientsession(self.hass)
|
2019-05-23 04:09:59 +00:00
|
|
|
with async_timeout.timeout(self._timeout):
|
2018-10-01 06:56:50 +00:00
|
|
|
await httpsession.put(url, headers=self._headers)
|
2017-04-16 23:40:12 +00:00
|
|
|
|
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
|
|
|
_LOGGER.exception("Error on %s", url)
|