core/homeassistant/components/light/hyperion.py

127 lines
3.5 KiB
Python
Raw Normal View History

2015-10-17 17:36:52 +00:00
"""
homeassistant.components.light.hyperion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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):
""" Sets up a Hyperion server remote """
host = config.get(CONF_HOST, None)
port = config.get("port", 19444)
2015-10-20 15:30:23 +00:00
device = Hyperion(host, port)
if device.setup():
add_devices_callback([device])
return True
2015-10-20 15:30:23 +00:00
else:
return False
2015-10-17 17:36:52 +00:00
class Hyperion(Light):
""" Represents a Hyperion remote """
def __init__(self, host, port):
self._host = host
self._port = port
2015-10-20 15:30:23 +00:00
self._name = host
self._is_available = True
self._rgb_color = [255, 255, 255]
2015-10-17 17:36:52 +00:00
@property
def name(self):
2015-10-20 15:30:23 +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):
""" Last RGB color value set. """
return self._rgb_color
2015-10-17 17:36:52 +00:00
@property
def is_on(self):
2015-10-20 15:30:23 +00:00
""" True if the device is online. """
2015-10-17 17:36:52 +00:00
return self._is_available
def turn_on(self, **kwargs):
""" Turn the lights on. """
if self._is_available:
2015-10-20 15:30:23 +00:00
if ATTR_RGB_COLOR in kwargs:
self._rgb_color = kwargs[ATTR_RGB_COLOR]
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 the remote. """
self.json_request({"command": "clearall"})
2015-10-20 15:30:23 +00:00
def update(self):
""" Ping the remote. """
# just see if the remote port is open
self._is_available = self.json_request()
def setup(self):
""" 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
2015-10-17 17:36:52 +00:00
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
def json_request(self, request=None, wait_for_response=False):
2015-10-17 17:36:52 +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
if not request:
# no communication needed, simple presence detection returns True
sock.close()
2015-10-20 15:30:23 +00:00
return True
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:
2015-10-20 15:30:23 +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
2015-10-20 15:30:23 +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)