core/homeassistant/components/light/hyperion.py

127 lines
3.7 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-02-19 05:27:50 +00:00
from homeassistant.components.light import ATTR_RGB_COLOR, Light
2015-10-17 17:36:52 +00:00
from homeassistant.const import CONF_HOST
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = []
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-07 21:08:21 +00:00
"""Setup a Hyperion server remote."""
2015-10-17 17:36:52 +00:00
host = config.get(CONF_HOST, None)
port = config.get("port", 19444)
default_color = config.get("default_color", [255, 255, 255])
device = Hyperion(config.get('name', host), host, port, default_color)
2015-10-20 15:30:23 +00:00
if device.setup():
add_devices_callback([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, 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._default_color = default_color
self._rgb_color = [0, 0, 0]
2015-10-17 17:36:52 +00:00
@property
def name(self):
2016-03-07 21:08:21 +00:00
"""Return the hostname of the server."""
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
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": 128,
"color": self._rgb_color})
2015-10-17 17:36:52 +00:00
def turn_off(self, **kwargs):
"""Disconnect all remotes."""
2015-10-17 17:36:52 +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."""
response = self.json_request({"command": "serverinfo"})
if response:
if response["info"]["activeLedColor"] == []:
self._rgb_color = [0, 0, 0]
else:
self._rgb_color =\
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."""
2015-10-17 17:36:52 +00:00
response = self.json_request({"command": "serverinfo"})
if response:
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
2015-10-20 15:30:23 +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:
if "\n" in str(buf, "utf-8"):
response = str(buf, "utf-8").split("\n")[0]
buffering = False
else:
try:
more = sock.recv(4096)
except socket.timeout:
more = None
if not more:
buffering = False
response = str(buf, "utf-8")
else:
buf += more
sock.close()
2015-10-20 15:30:23 +00:00
return json.loads(response)