Hyperion active (#2634)

* Hyperion lets you turn it on and off

* Update hyperion to use recent API functions

The plugin now gets the active color from the server.
Add a configuration option "default_color" to customize the turn_on color.
This commit is contained in:
schneefux 2016-07-28 06:11:12 +02:00 committed by Paulus Schoutsen
parent a0f72e3569
commit 26983aa646

View File

@ -19,24 +19,24 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup a Hyperion server remote.""" """Setup a Hyperion server remote."""
host = config.get(CONF_HOST, None) host = config.get(CONF_HOST, None)
port = config.get("port", 19444) port = config.get("port", 19444)
device = Hyperion(config.get('name', host), host, port) default_color = config.get("default_color", [255, 255, 255])
device = Hyperion(config.get('name', host), host, port, default_color)
if device.setup(): if device.setup():
add_devices_callback([device]) add_devices_callback([device])
return True return True
else: return False
return False
class Hyperion(Light): class Hyperion(Light):
"""Representation of a Hyperion remote.""" """Representation of a Hyperion remote."""
def __init__(self, name, host, port): def __init__(self, name, host, port, default_color):
"""Initialize the light.""" """Initialize the light."""
self._host = host self._host = host
self._port = port self._port = port
self._name = name self._name = name
self._is_available = True self._default_color = default_color
self._rgb_color = [255, 255, 255] self._rgb_color = [0, 0, 0]
@property @property
def name(self): def name(self):
@ -50,38 +50,43 @@ class Hyperion(Light):
@property @property
def is_on(self): def is_on(self):
"""Return true if the device is online.""" """Return true if not black."""
return self._is_available return self._rgb_color != [0, 0, 0]
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the lights on.""" """Turn the lights on."""
if self._is_available: if ATTR_RGB_COLOR in kwargs:
if ATTR_RGB_COLOR in kwargs: self._rgb_color = kwargs[ATTR_RGB_COLOR]
self._rgb_color = kwargs[ATTR_RGB_COLOR] else:
self._rgb_color = self._default_color
self.json_request({"command": "color", "priority": 128, self.json_request({"command": "color", "priority": 128,
"color": self._rgb_color}) "color": self._rgb_color})
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Disconnect the remote.""" """Disconnect all remotes."""
self.json_request({"command": "clearall"}) self.json_request({"command": "clearall"})
self._rgb_color = [0, 0, 0]
def update(self): def update(self):
"""Ping the remote.""" """Get the remote's active color."""
# just see if the remote port is open response = self.json_request({"command": "serverinfo"})
self._is_available = self.json_request() if response:
if response["info"]["activeLedColor"] == []:
self._rgb_color = [0, 0, 0]
else:
self._rgb_color =\
response["info"]["activeLedColor"][0]["RGB Value"]
def setup(self): def setup(self):
"""Get the hostname of the remote.""" """Get the hostname of the remote."""
response = self.json_request({"command": "serverinfo"}) response = self.json_request({"command": "serverinfo"})
if response: if response:
if self._name == self._host: self._name = response["info"]["hostname"]
self._name = response["info"]["hostname"]
return True return True
return False return False
def json_request(self, request=None, wait_for_response=False): def json_request(self, request, wait_for_response=False):
"""Communicate with the JSON server.""" """Communicate with the JSON server."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5) sock.settimeout(5)
@ -92,11 +97,6 @@ class Hyperion(Light):
sock.close() sock.close()
return False return False
if not request:
# No communication needed, simple presence detection returns True
sock.close()
return True
sock.send(bytearray(json.dumps(request) + "\n", "utf-8")) sock.send(bytearray(json.dumps(request) + "\n", "utf-8"))
try: try:
buf = sock.recv(4096) buf = sock.recv(4096)