light/hyperion: close sockets, report setup success

This commit is contained in:
MakeMeASandwich 2015-10-25 11:08:59 +01:00
parent 2e9ee28637
commit 1be48f54c0

View File

@ -33,6 +33,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
device = Hyperion(host, port) device = Hyperion(host, port)
if device.setup(): if device.setup():
add_devices_callback([device]) add_devices_callback([device])
return True
else: else:
return False return False
@ -97,10 +98,12 @@ class Hyperion(Light):
try: try:
sock.connect((self._host, self._port)) sock.connect((self._host, self._port))
except OSError: except OSError:
sock.close()
return False return False
if not request: if not request:
# no communication needed, simple presence detection returns True # no communication needed, simple presence detection returns True
sock.close()
return True return True
sock.send(bytearray(json.dumps(request) + "\n", "utf-8")) sock.send(bytearray(json.dumps(request) + "\n", "utf-8"))
@ -108,6 +111,7 @@ class Hyperion(Light):
buf = sock.recv(4096) buf = sock.recv(4096)
except socket.timeout: except socket.timeout:
# something is wrong, assume it's offline # something is wrong, assume it's offline
sock.close()
return False return False
# read until a newline or timeout # read until a newline or timeout