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)
print(r)
time.sleep(3)
os.system('clear')
if request == "off":
r = api.device_off(deviceID)
print(r)
time.sleep(3)
os.system('clear')
if request != "on" and request != "off":
print('not valid choose')
time.sleep(3)
os.system('clear')
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
self.log.debug(url)
headers = {'Accept': 'application/json', 'Authorization' : self.token}
if method == 'GET':
r = requests.get(url, params=params, headers=headers)
elif method == 'POST':
r = requests.post(url, json=params, headers=headers)
try:
response = r.json()
except:
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
Claudio