I’m using the following code for my homemade letterbox sensor. It works fine although i’m aware it isn’t very efficient or elegant.
I’ve now started moving away from using the api password. I can’t work out where to put the long living token and if there are any other changes required. Has anyone done anything similar and could offer suggestions. It’s pickling my brain and i don’t want to lose the use of the letterbox sensor when the api password is finally discontinued.
import requests
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(16)
if input_state == False:
headers = {
'x-ha-access': 'password',
'Content-Type': 'application/json',
}
data = '{ "entity_id": "script.letterbox_shut" }'
response = requests.post('http://192.168.200.35:8123/api/services/script/turn_on', headers=headers, data=data)
time.sleep(2.0)
else:
headers = {
'x-ha-access': 'super1',
'Content-Type': 'application/json',
}
data = '{ "entity_id": "script.letterbox_open" }'
response = requests.post('http://192.168.200.35:8123/api/services/script/turn_on', headers=headers, data=data)
time.sleep(2.0)