HTTP 500 on set capability via rest API

I’m writing python code to interact with Homey through the oauth web API.
All works up to the point where I want tot set a value for a device.

  def set_target_temp(self, ts, temp):
        if self.use_bearer():
            id = self.get_device_id(ts)
            url = 'http://192.168.1.93/api/manager/devices/device/'+id+'/capability/target_temperature/'  
            headr = {'Authorization': 'Bearer ' + self.bearer_token}
            put_data = { "value": 13.0 }
            response = requests.put(url=url, data=put_data, headers=headr)
            if (response.status_code != 200):
                return {"status": "failed"}
            else:
                return {"status": "ok"}

This return an HTTP 500 with:

{
	"code": 500,
	"error": "target_temperature InvalidTypeError: 13.0 is of type string but expected number (invalid_type)",
	"error_description": "target_temperature InvalidTypeError: 13.0 is of type string but expected number (invalid_type)"
}

I also tried { “value”: “13” } with the same result.
Anyone ran into this before?

Frank

Are you sure that requests is:

  • posting the data as JSON
  • setting the correct content-type (application/json)?

It looks to me like it might be sending the data as application/x-www-form-urlencoded, which is typeless and therefore will always be interpreted as string.

Spot on as usual, thanks Robert !
I’ve changed to json=put_data and this works:

    def set_target_temp(self, ts, temp):
        if self.use_bearer():
            id = self.get_device_id(ts)
            url = 'http://192.168.1.93/api/manager/devices/device/'+id+'/capability/target_temperature/'  # getFirstHomey
            headr = {'Authorization': 'Bearer ' + self.bearer_token}
            put_data = { "value": 13.0 }
            response = requests.put(url=url, json=put_data, headers=headr)
            if (response.status_code != 200): #do we need this ?
                return {"status": "failed"}
            else:
                return {"status": "ok"}```
1 Like