nougad / velux-cli

Go Client for Velux Active KIX 300
MIT License
31 stars 3 forks source link

setstate: syntax seems to have changed #4

Closed thica closed 1 year ago

thica commented 1 year ago

Hi,

the API works fine, but the syntax for setstate might have changed. The return for setstate is {"error":{"code":10,"message":"Missing home_id"}}. Even if you change the syntax to pass home_id instead of id, other errors come up, Any updates on the syntax available?

nougad commented 1 year ago

Hey, sorry for the late reply. I can't confirm the problem. Would be great to see the full request so we can compare the payload.

Example request to open a roller shutter:

$ curl -v -s -H 'Content-Type: application/json; charset=utf-8' -H 'Authorization: Bearer XXXX|YYYY' -X POST -d '{ "home": { "id": "HOMEID", "modules": [{ "bridge": "de:ad:be:af:42:21", "id": "SHUTTER_ID", "target_position": 100 }] } }' https://app.velux-active.com/syncapi/v1/setstate
{
  "status": "ok",
  "time_server": 1672156930
}

I only own roller shutters to maybe the parameters are different with other devices?

thica commented 1 year ago

Hi, thanks for your response: On the positive side, your "curl code" works. On the negative, if I code it in python, it doesn't work. Take it as a feedback, I will try myself to figure it out. Looks to me, that it is something with the header data.

`Headers:{'Authorization': 'Bearer 63764484b6c8c344480fXXXX|57a93d019b82775816aeab9a74aXXXX', 'Content-Type': 'application/json; charset=utf-8'} Data:{'home': {'id': '63764eb722225a0e2808XXXX', 'modules': [{'bridge': '70:ee:50:7e:XX:XX', 'id': '53141e321408XXXX', 'target_position': 20}]}} URL:'https://app.velux-active.com/syncapi/v1/setstate'

oResponse = requests.post(uUrl, data=dData, headers=self.dHeaders)

Error: {"error":{"message":"Unexpected token h in JSON at position 0","code":400}} `

nougad commented 1 year ago

A full example would be useful but from locking at the snippet I'm wondering if you forgot the JSON serialization for the body: requests.post(uUrl, data=json.dumps(dData), ...

thica commented 1 year ago

Hi, thanks again. First: it works with json.dumps(dData). However, as dData was already a dict, this shouldn't be necessary. I will not investigate further on it, it works!. Thx

mborsetti commented 1 year ago

Hi, thanks again. First: it works with json.dumps(dData). However, as dData was already a dict, this shouldn't be necessary. I will not investigate further on it, it works!. Thx

You need to use json= instead of data= to serialize the data as JSON per the requests documentation:

oResponse = requests.post(uUrl, json=dData, headers=self.dHeaders)