Files
homeassistantPi/lightSwitch.py
2022-10-22 21:05:18 -04:00

36 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import subprocess
import time
import RPi.GPIO as GPIO
#Toggle instead of turn on
#Don't have to check if on or off
#curl -k -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwYWRkMGMxZmFlNWQ0YWNmYThjNTdmMDRhNWIwODBjNiIsImlhdCI6MTY2NjQ2NzMzMCwiZXhwIjoxOTgxODI3MzMwfQ.Egk1vDF0LC_WRlorhCHLPihkPICIEwz6nbfJR7Y1Prs" -H "Content-Type: application/json" -d '{"entity_id": "switch.lamp_plug"}' http://192.168.0.196:8123/api/services/switch/turn_on
#Find an accurate broadcom pinout here
#https://www.etechnophiles.com/raspberry-pi-3-b-pinout-with-gpio-functions-schematic-and-specs-in-detail/
touchPin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(touchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def touchDet(pin):
touch = GPIO.input(pin)
return touch
try:
while True:
if touchDet(touchPin):
print('['+time.ctime()+'] - '+'Touch Detected, Light Switch Toggled')
subprocess.run('./home/pi/homeassistantPi/lightSwitch.sh')
time.sleep(0.2)
except KeyboardInterrupt:
print('interrupted!')
GPIO.cleanup()
#subprocess.run('./lightSwitch.sh')