Skip to content

API WeMo

ARIbemoss edited this page May 11, 2017 · 1 revision

[Back to BEMOSS Connectivity Layer]

API Interface: WeMo Smart Plug


API interface code


Attributes

This API interface supports the following attributes that can be found from the device:

#!python

Status     GET/POST   Current Device Status 1 or True for ON and  0 or False for OFF

Capabilities

This API interface supports three methods: getDeviceStatus, setDeviceStatus, and identifyDevice.

The getDeviceStatus method, if called, sends a HTTP GET request to the device IP address, and receives the data from the device as a XML format response:

#!python

    def getDeviceStatus(self):
        print(" {0}Agent is querying its current status please wait ...".format(self.variables.get('agent_id',None)))
        header = {
            'Content-Type': 'text/xml; charset="utf-8"',
            'SOAPACTION': '"urn:Belkin:service:basicevent:1#GetBinaryState"'
        }
        body="<?xml version='1.0' encoding='utf-8'?><s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><s:Body><u:GetBinaryState xmlns:u='urn:Belkin:service:basicevent:1'></u:GetBinaryState></s:Body></s:Envelope>"
        controlUrl=self.get_variable('address')+'/upnp/control/basicevent1'
        try:
            response = requests.post(controlUrl, body, headers=header)
            dom = minidom.parseString(response.content)
            if int(dom.getElementsByTagName('BinaryState')[0].firstChild.data) == 0|False:
                self.set_variable('status',"OFF")
            elif int(dom.getElementsByTagName('BinaryState')[0].firstChild.data) == 1|True:
                self.set_variable('status',"ON")
            self.printDeviceStatus()
        except:
            print("ERROR: classAPI_WeMo connection failure! @ getDeviceStatus")

The setDeviceStatus method receives attributes to change in device as arguments from the OS layer in JSON format. It checks for message format validity and then converts the attribute names to the names appropriate for device. It then sends the attributes with new values as XML format data in the POST request to device IP address. It returns back to OS 1 or 0 for 'success' or 'failure' respectively.

#!python

    def setDeviceStatus(self, newstatus):
        setDeviceStatusResult = True
        header = {
                'Content-Type': 'text/xml; charset="utf-8"',
                'SOAPACTION': '"urn:Belkin:service:basicevent:1#SetBinaryState"'
            }
        #Data conversion before passing to the device
        _data = json.dumps(newstatus)
        _data = json.loads(_data)

        # print _data
        # print type(_data)

        if _data['status'] == "OFF":
            newstatus = 0
        elif _data['status'] == "ON":
            newstatus = 1

        # print newstatus

        body="<?xml version='1.0' encoding='utf-8'?><s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' " \
             "s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><s:Body><u:SetBinaryState " \
             "xmlns:u='urn:Belkin:service:basicevent:1'><BinaryState>"+str(int(newstatus))\
             +"</BinaryState></u:SetBinaryState></s:Body></s:Envelope>"

        controlUrl=self.get_variable('address')+'/upnp/control/basicevent1'
        try:
            response = requests.post(controlUrl, body, headers=header)
            dom = minidom.parseString(response.content)
            responsestatus = dom.getElementsByTagName('BinaryState')[0].firstChild.data
            if responsestatus!='Error':
                self.set_variable('status',int(responsestatus))
        except:
            print("ERROR: classAPI_WeMo connection failure! @ setDeviceStatus")
            setDeviceStatusResult = False
        self.getDeviceStatus()
        self.set_variable('status', self.get_variable('status'))
        return setDeviceStatusResult

Finally, the identifyDevice method, if called, changes device status (ON to OFF or reverse). It then waits for 10 seconds and then reverts the status back to previous status. Like setDeviceStatus, it returns boolean 1/0 for success/failure.

#!python

    def identifyDevice(self):
        identifyDeviceResult = False
        try:
            self.getDeviceStatus()
            self.toggleDeviceStatus()
            print(self.get_variable("model")+" is being identified with starting status "+str(self.get_variable('status')))
            self.timeDelay(5)
            self.toggleDeviceStatus()
            print("Identification for "+self.get_variable("model")+" is done with status "+str(self.get_variable('status')))
            identifyDeviceResult = True
        except:
            print("ERROR: classAPI_WeMo connection failure! @ identifyDevice")
        return identifyDeviceResult

[Back to BEMOSS Connectivity Layer]

Clone this wiki locally