add response for GetBinaryState

This commit is contained in:
Emil Lerch 2019-07-22 17:15:57 -07:00
parent 39e5f5fb89
commit d3b8e23628
Signed by: lobo
GPG Key ID: CEC5F37C1BE5A481

View File

@ -201,6 +201,7 @@ class fauxmo(upnp_device):
def __init__(self, name, listener, poller, ip_address, port, action_handler=None): def __init__(self, name, listener, poller, ip_address, port, action_handler=None):
self.serial = self.make_uuid(name) self.serial = self.make_uuid(name)
self.name = name self.name = name
self.state = 0
self.ip_address = ip_address self.ip_address = ip_address
persistent_uuid = "Socket-1_0-" + self.serial persistent_uuid = "Socket-1_0-" + self.serial
other_headers = ['X-User-Agent: redsonic'] other_headers = ['X-User-Agent: redsonic']
@ -214,6 +215,12 @@ class fauxmo(upnp_device):
def get_name(self): def get_name(self):
return self.name return self.name
def get_state(self):
return self.state
def set_state(self, newstate):
self.state = newstate
def handle_request(self, data, sender, socket): def handle_request(self, data, sender, socket):
if data.find('GET /setup.xml HTTP/1.1') == 0: if data.find('GET /setup.xml HTTP/1.1') == 0:
dbg("Responding to setup.xml for %s" % self.name) dbg("Responding to setup.xml for %s" % self.name)
@ -237,6 +244,7 @@ class fauxmo(upnp_device):
dbg("Responding to ON for %s" % self.name) dbg("Responding to ON for %s" % self.name)
try: try:
success = self.action_handler.on() success = self.action_handler.on()
self.set_state(1)
except Exception as e: except Exception as e:
dbg("WARNING: Failed to turn ON: %s" % str(e)) dbg("WARNING: Failed to turn ON: %s" % str(e))
elif data.find('<BinaryState>0</BinaryState>') != -1: elif data.find('<BinaryState>0</BinaryState>') != -1:
@ -244,6 +252,7 @@ class fauxmo(upnp_device):
dbg("Responding to OFF for %s" % self.name) dbg("Responding to OFF for %s" % self.name)
try: try:
success = self.action_handler.off() success = self.action_handler.off()
self.set_state(0)
except Exception as e: except Exception as e:
dbg("WARNING: Failed to turn OFF: %s" % str(e)) dbg("WARNING: Failed to turn OFF: %s" % str(e))
else: else:
@ -265,6 +274,30 @@ class fauxmo(upnp_device):
"\r\n" "\r\n"
"%s" % (len(soap), date_str, soap)) "%s" % (len(soap), date_str, soap))
socket.send(message) socket.send(message)
elif data.find('SOAPACTION: "urn:Belkin:service:basicevent:1#GetBinaryState"') != -1:
# The Alexa app will want a full soap message for this
dbg("Responding to state request for %s. State is %s" % (self.name, str(self.get_state())))
soap = ('<s:Envelope "xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" ' +
's:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>' +
'<u:GetBinaryStateResponse xmlns:u="urn:Belkin:service:basicevent:1">' +
'<BinaryState>' + str(self.get_state()) + '</BinaryState>' +
'<brightness>100</brightness>' +
'<fader>300:-1:1:0:100</fader>' +
'</u:GetBinaryStateResponse>' +
'</s:Body> </s:Envelope>')
date_str = email.utils.formatdate(timeval=None, localtime=False, usegmt=True)
message = ("HTTP/1.1 200 OK\r\n"
"CONTENT-LENGTH: %d\r\n"
"CONTENT-TYPE: text/xml charset=\"utf-8\"\r\n"
"DATE: %s\r\n"
"EXT:\r\n"
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
"X-User-Agent: redsonic\r\n"
"CONNECTION: close\r\n"
"\r\n"
"%s" % (len(soap), date_str, soap))
socket.send(message)
else: else:
dbg(data) dbg(data)