diff --git a/fauxmo/fauxmo.py b/fauxmo/fauxmo.py index d775464..c458626 100755 --- a/fauxmo/fauxmo.py +++ b/fauxmo/fauxmo.py @@ -201,6 +201,7 @@ class fauxmo(upnp_device): def __init__(self, name, listener, poller, ip_address, port, action_handler=None): self.serial = self.make_uuid(name) self.name = name + self.state = 0 self.ip_address = ip_address persistent_uuid = "Socket-1_0-" + self.serial other_headers = ['X-User-Agent: redsonic'] @@ -214,6 +215,12 @@ class fauxmo(upnp_device): def get_name(self): 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): if data.find('GET /setup.xml HTTP/1.1') == 0: 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) try: success = self.action_handler.on() + self.set_state(1) except Exception as e: dbg("WARNING: Failed to turn ON: %s" % str(e)) elif data.find('0') != -1: @@ -244,6 +252,7 @@ class fauxmo(upnp_device): dbg("Responding to OFF for %s" % self.name) try: success = self.action_handler.off() + self.set_state(0) except Exception as e: dbg("WARNING: Failed to turn OFF: %s" % str(e)) else: @@ -265,6 +274,30 @@ class fauxmo(upnp_device): "\r\n" "%s" % (len(soap), date_str, soap)) 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 = ('' + + '' + + '' + str(self.get_state()) + '' + + '100' + + '300:-1:1:0:100' + + '' + + ' ') + 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: dbg(data)