From e3304caf0695a95601bcd04809f4f26bb89bbf6d Mon Sep 17 00:00:00 2001 From: MakeMeASandwich Date: Sat, 17 Oct 2015 19:36:52 +0200 Subject: [PATCH] add hyperion light support --- .coveragerc | 1 + homeassistant/components/light/hyperion.py | 135 +++++++++++++++++++++ homeassistant/util/color.py | 40 ++++++ 3 files changed, 176 insertions(+) create mode 100644 homeassistant/components/light/hyperion.py diff --git a/.coveragerc b/.coveragerc index 7e474d287c2..d472423c950 100644 --- a/.coveragerc +++ b/.coveragerc @@ -49,6 +49,7 @@ omit = homeassistant/components/light/hue.py homeassistant/components/light/limitlessled.py homeassistant/components/light/blinksticklight.py + homeassistant/components/light/hyperion.py homeassistant/components/media_player/cast.py homeassistant/components/media_player/denon.py homeassistant/components/media_player/firetv.py diff --git a/homeassistant/components/light/hyperion.py b/homeassistant/components/light/hyperion.py new file mode 100644 index 00000000000..b11485ff8d5 --- /dev/null +++ b/homeassistant/components/light/hyperion.py @@ -0,0 +1,135 @@ +""" +homeassistant.components.light.hyperion +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Support for Hyperion remotes. + +Configuration: + +To connect to [a Hyperion server](https://github.com/tvdzwan/hyperion) you +will need to add something like the following to your configuration.yaml file: + +light: + platform: hyperion + host: 192.168.1.98 + port: 19444 + +The JSON server port is 19444 by default. +""" +import logging +import socket +import json + +from homeassistant.const import CONF_HOST +from homeassistant.components.light import (Light, ATTR_XY_COLOR, + ATTR_BRIGHTNESS) +from homeassistant.util.color import color_RGB_to_xy, \ + color_xy_brightness_to_RGB + +_LOGGER = logging.getLogger(__name__) +REQUIREMENTS = [] + + +def setup_platform(hass, config, add_devices_callback, discovery_info=None): + """ Sets up a Hyperion server remote """ + host = config.get(CONF_HOST, None) + port = config.get("port", 19444) + add_devices_callback([Hyperion(host, port)]) + + +class Hyperion(Light): + """ Represents a Hyperion remote """ + + def __init__(self, host, port): + self._host = host + self._port = port + self._name = "unknown" + self._is_available = False + self._xy_color = color_RGB_to_xy(0, 0, 0) + self._brightness = 255 + + @property + def name(self): + """ Get the hostname of the server. """ + return self._name + + @property + def color_xy(self): + """ XY color value. """ + return self._xy_color + + @property + def brightness(self): + """ Brightness. """ + return self._brightness + + @property + def is_on(self): + """ True if device is on. """ + self.check_remote() + return self._is_available + + def turn_on(self, **kwargs): + """ Turn the lights on. """ + if self._is_available: + if ATTR_XY_COLOR in kwargs: + self._xy_color = kwargs[ATTR_XY_COLOR] + if ATTR_BRIGHTNESS in kwargs: + self._brightness = kwargs[ATTR_BRIGHTNESS] + self.update_remote() + + def turn_off(self, **kwargs): + """ Disconnect the remote. """ + self.json_request({"command": "clearall"}) + + def check_remote(self): + """ Ping the remote and gets the hostname. """ + response = self.json_request({"command": "serverinfo"}) + if response: + self._name = response["info"]["hostname"] + + def update_remote(self): + """ Set the remote's lights. """ + rgb = color_xy_brightness_to_RGB(self._xy_color[0], self._xy_color[1], + self._brightness) + rgb = [int(c) for c in rgb] + self.json_request( + {"command": "color", "priority": 128, "color": rgb}) + + def json_request(self, request, wait_for_response=False): + """ Communicate with the json server. """ + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(5) + sock.connect((self._host, self._port)) + except OSError: + self._is_available = False + return None + + sock.send(bytearray(json.dumps(request) + "\n", "utf-8")) + + try: + buf = sock.recv(4096) + except socket.timeout: + return None + + 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() + + j = json.loads(response) + self._is_available = True + return j diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 5f967fa87b9..26dca7ab0c6 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -39,3 +39,43 @@ def color_RGB_to_xy(R, G, B): # Convert XYZ to xy, see CIE 1931 color space on wikipedia return X / (X + Y + Z), Y / (X + Y + Z) + + +# taken from +# https://github.com/benknight/hue-python-rgb-converter/blob/master/rgb_cie.py +# pylint: disable=bad-builtin +def color_xy_brightness_to_RGB(vX, vY, brightness): + ''' + Convert from XYZ to RGB. + ''' + brightness /= 255. + if brightness == 0: + return (0, 0, 0) + + Y = brightness + X = (Y / vY) * vX + Z = (Y / vY) * (1 - vX - vY) + + # Convert to RGB using Wide RGB D65 conversion. + r = X * 1.612 - Y * 0.203 - Z * 0.302 + g = -X * 0.509 + Y * 1.412 + Z * 0.066 + b = X * 0.026 - Y * 0.072 + Z * 0.962 + + # Apply reverse gamma correction. + r, g, b = map( + lambda x: (12.92 * x) if (x <= 0.0031308) else + ((1.0 + 0.055) * pow(x, (1.0 / 2.4)) - 0.055), + [r, g, b] + ) + + # Bring all negative components to zero. + r, g, b = map(lambda x: max(0, x), [r, g, b]) + + # If one component is greater than 1, weight components by that value. + max_component = max(r, g, b) + if max_component > 1: + r, g, b = map(lambda x: x / max_component, [r, g, b]) + + r, g, b = map(lambda x: int(x * 255), [r, g, b]) + + return (r, g, b)