core/homeassistant/components/light/hyperion.py

169 lines
4.9 KiB
Python
Raw Normal View History

2015-10-17 17:36:52 +00:00
"""
Support for Hyperion remotes.
2015-11-09 12:12:18 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.hyperion/
2015-10-17 17:36:52 +00:00
"""
2016-02-19 05:27:50 +00:00
import json
2015-10-17 17:36:52 +00:00
import logging
import socket
2016-09-11 07:24:25 +00:00
import voluptuous as vol
from homeassistant.components.light import (
ATTR_RGB_COLOR, SUPPORT_RGB_COLOR, Light, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_HOST, CONF_PORT, CONF_NAME)
import homeassistant.helpers.config_validation as cv
2015-10-17 17:36:52 +00:00
_LOGGER = logging.getLogger(__name__)
2016-09-11 07:24:25 +00:00
CONF_DEFAULT_COLOR = 'default_color'
CONF_PRIORITY = 'priority'
2016-09-11 07:24:25 +00:00
DEFAULT_COLOR = [255, 255, 255]
DEFAULT_NAME = 'Hyperion'
DEFAULT_PORT = 19444
DEFAULT_PRIORITY = 128
2015-10-17 17:36:52 +00:00
SUPPORT_HYPERION = SUPPORT_RGB_COLOR
2016-09-11 07:24:25 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_DEFAULT_COLOR, default=DEFAULT_COLOR):
vol.All(list, vol.Length(min=3, max=3),
[vol.All(vol.Coerce(int), vol.Range(min=0, max=255))]),
2016-09-11 07:24:25 +00:00
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PRIORITY, default=DEFAULT_PRIORITY): cv.positive_int,
2016-09-11 07:24:25 +00:00
})
2015-10-17 17:36:52 +00:00
2016-09-11 07:24:25 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up a Hyperion server remote."""
2016-09-11 07:24:25 +00:00
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
priority = config.get(CONF_PRIORITY)
2016-09-11 07:24:25 +00:00
default_color = config.get(CONF_DEFAULT_COLOR)
device = Hyperion(config.get(CONF_NAME), host, port, priority,
default_color)
2016-09-11 07:24:25 +00:00
2015-10-20 15:30:23 +00:00
if device.setup():
2016-09-11 07:24:25 +00:00
add_devices([device])
return True
return False
2015-10-17 17:36:52 +00:00
class Hyperion(Light):
2016-03-07 21:08:21 +00:00
"""Representation of a Hyperion remote."""
2015-10-17 17:36:52 +00:00
def __init__(self, name, host, port, priority, default_color):
2016-03-07 21:08:21 +00:00
"""Initialize the light."""
2015-10-17 17:36:52 +00:00
self._host = host
self._port = port
self._name = name
self._priority = priority
self._default_color = default_color
self._rgb_color = [0, 0, 0]
2015-10-17 17:36:52 +00:00
@property
def name(self):
"""Return the name of the light."""
2015-10-17 17:36:52 +00:00
return self._name
@property
2015-10-20 15:30:23 +00:00
def rgb_color(self):
2016-03-07 21:08:21 +00:00
"""Return last RGB color value set."""
2015-10-20 15:30:23 +00:00
return self._rgb_color
2015-10-17 17:36:52 +00:00
@property
def is_on(self):
"""Return true if not black."""
return self._rgb_color != [0, 0, 0]
2015-10-17 17:36:52 +00:00
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_HYPERION
2015-10-17 17:36:52 +00:00
def turn_on(self, **kwargs):
2016-03-07 21:08:21 +00:00
"""Turn the lights on."""
if ATTR_RGB_COLOR in kwargs:
self._rgb_color = kwargs[ATTR_RGB_COLOR]
else:
self._rgb_color = self._default_color
2015-10-20 15:30:23 +00:00
self.json_request({
'command': 'color',
'priority': self._priority,
'color': self._rgb_color
})
2015-10-17 17:36:52 +00:00
def turn_off(self, **kwargs):
"""Disconnect all remotes."""
2016-09-11 07:24:25 +00:00
self.json_request({'command': 'clearall'})
self._rgb_color = [0, 0, 0]
2015-10-17 17:36:52 +00:00
2015-10-20 15:30:23 +00:00
def update(self):
"""Get the remote's active color."""
2016-09-11 07:24:25 +00:00
response = self.json_request({'command': 'serverinfo'})
if response:
# workaround for outdated Hyperion
2016-09-11 07:24:25 +00:00
if 'activeLedColor' not in response['info']:
self._rgb_color = self._default_color
return
2016-09-11 07:24:25 +00:00
if response['info']['activeLedColor'] == []:
self._rgb_color = [0, 0, 0]
else:
self._rgb_color =\
2016-09-11 07:24:25 +00:00
response['info']['activeLedColor'][0]['RGB Value']
2015-10-20 15:30:23 +00:00
def setup(self):
2016-03-07 21:08:21 +00:00
"""Get the hostname of the remote."""
2016-09-11 07:24:25 +00:00
response = self.json_request({'command': 'serverinfo'})
2015-10-17 17:36:52 +00:00
if response:
if self._name == self._host:
self._name = response['info']['hostname']
2015-10-20 15:30:23 +00:00
return True
return False
2015-10-17 17:36:52 +00:00
def json_request(self, request, wait_for_response=False):
2016-03-07 21:08:21 +00:00
"""Communicate with the JSON server."""
2015-10-20 15:30:23 +00:00
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
2015-10-17 17:36:52 +00:00
try:
sock.connect((self._host, self._port))
except OSError:
sock.close()
2015-10-20 15:30:23 +00:00
return False
2015-10-17 17:36:52 +00:00
2016-09-11 07:24:25 +00:00
sock.send(bytearray(json.dumps(request) + '\n', 'utf-8'))
2015-10-17 17:36:52 +00:00
try:
buf = sock.recv(4096)
except socket.timeout:
2016-03-07 21:08:21 +00:00
# Something is wrong, assume it's offline
sock.close()
2015-10-20 15:30:23 +00:00
return False
2015-10-17 17:36:52 +00:00
2016-03-07 21:08:21 +00:00
# Read until a newline or timeout
2015-10-17 17:36:52 +00:00
buffering = True
while buffering:
2016-09-11 07:24:25 +00:00
if '\n' in str(buf, 'utf-8'):
response = str(buf, 'utf-8').split('\n')[0]
2015-10-17 17:36:52 +00:00
buffering = False
else:
try:
more = sock.recv(4096)
except socket.timeout:
more = None
if not more:
buffering = False
2016-09-11 07:24:25 +00:00
response = str(buf, 'utf-8')
2015-10-17 17:36:52 +00:00
else:
buf += more
sock.close()
2015-10-20 15:30:23 +00:00
return json.loads(response)