Python device integration through API

Good morning everyone,

I’m trying to realize a single device command on off on my envoirement, through the API and python code. I’m able to read the devices which I created on the envoirement but I found some issue if I try to write some command. I wrote this code

            request = input('Select command (on/off): ')
            if request == "on":
                r = api.device_on(deviceID)
            if request == "off":
                r = api.device_off(deviceID)
            if request != "on" and request != "off":
                print('not valid choose')

the class which realize the connection

def device_on(self,deviceID):
    url = "/v1/devices/{}/commands".format(deviceID) 
    method = 'POST'
    payload = {"commands":[{"component": "main","capability": "switch","command": "on","arguments": []}]}
    return self.make_request(url,method,payload)

def device_off(self,deviceID):
    url = "/v1/devices/{}/commands".format(deviceID) 
    method = 'POST'
    payload = {"commands":[{"component": "main","capability": "switch","command": "off","arguments": []}]}
    return self.make_request(url,method,payload)

end definetly the request POST

def make_request(self, url, method , params=None):

    url = self.url_root + url
    headers = {'Accept': 'application/json', 'Authorization' : self.token}

    if method == 'GET':
        r = requests.get(url, params=params, headers=headers)
    elif method == 'POST':
        r =, json=params, headers=headers)  
        response = r.json()
        response = r.text
    return response

When I run my application I obtained this information

Select command (on/off): off
{‘results’: [{‘id’: ‘XXXXXX-XXXXX-XXXXX-XXXXX-XXXXXXXXX’, ‘status’: ‘ACCEPTED’}]}

so, it seems everitings working but if i’m going to check on develop envoirement’s log, I found this information

10:24:30: error java.lang.NullPointerException: Cannot invoke method off() on null object @line 108 (off)

So, the bulb is not yet physically connected and I can’t check if really works (on monday I will test with real device) but I would like to understand re log error what does it means. Is it the correct reply if the device missing or there is something wrong on my payload parameters?

@erickv please, do you also have any idea regarding that?

Thanks for your support


1 Like

Hi, @c.tondi

Thank you for sharing your approach with us.

From the error message that you’ve triggered, I can see that the issue is at the DTH of the virtual device that you’ve created. To have a better experience interacting with virtual devices through the API, I recommend you to use the Virtual Switch or Simulated Switch DTHs available, which have been prepared to work without a physical device.

Please, let me know if this information has been useful to you by marking this post as a Solution, otherwise share with me your doubts.

Hi @erickv,

thank you for your reply. Whit a virtual switch, the code works correctly like expected.

Thank you so much