I am fairly new with HA. in my researches, I keep finding info about hass but I dont think it’s related.
I am trying to send a command to HA using python script from a raspberry pi on the same network. I can get status using get like this:
url = “http://homeassistant.local:8123/api/states”
headers = {
“Authorization”: “Bearer ABCDEFG”,
“content-type”: “application/json”,
}
response = get(url, headers=headers)
print(response.text)
But I can’t figure out how to post a command. I tried:
url = “http://homeassistant.local:8123/api/services/switch/toggle”
headers = {
“Authorization”: “Bearer ABCDEFG”,
“content-type”: “application/json”,
“entity_id”: “switch.sonoff_s31_lite_zb_commutateur”,
}
response = post(url, json=headers)
print(response.text)
but that is giving me:
401: Unauthorized
Using the curl command in a terminal window works like this:
curl -X POST -H “Authorization: Bearer ABCDEFG” -H “Content-Type: application/json” -d ‘{“entity_id”: “switch.sonoff_s31_lite_zb_commutateur”}’ http://homeassistant.local:8123/api/services/switch/turn_on
but I really want to use a command that I can use within python. What is the proper syntax?