No removed relay code, now using home assistant to open lock
This commit is contained in:
36
gate.py
36
gate.py
@@ -2,22 +2,25 @@ import pigpio
|
||||
import time
|
||||
import logging
|
||||
import json
|
||||
import requests
|
||||
|
||||
from wiegand import decoder
|
||||
|
||||
class gate:
|
||||
def __init__(self, pi, relay_pin):
|
||||
self.pi = pi
|
||||
self.relay_pin = relay_pin
|
||||
class door:
|
||||
def __init__(self, api_key):
|
||||
self.api_key = api_key
|
||||
|
||||
# set the pin to high to keep the gate locked
|
||||
pi.write(relay_pin, 1)
|
||||
def unlock_door(self):
|
||||
body = {"entity_id": "lock.kwikset_smartcode_convert_gen1_bb8103fd_door_lock"}
|
||||
headers = {'authorization': f'Bearer {self.api_key}'}
|
||||
requests.post("https://ha.zoisite.xyz/api/services/lock/unlock", headers=headers, data=json.dumps(body))
|
||||
print("unlocked")
|
||||
|
||||
# unlocks the gate for a number of seconds defined by timeout
|
||||
def unlock_gate(self, timeout):
|
||||
self.pi.write(self.relay_pin, 0)
|
||||
time.sleep(timeout)
|
||||
self.pi.write(self.relay_pin, 1)
|
||||
def lock_door(self):
|
||||
body = {"entity_id": "lock.kwikset_smartcode_convert_gen1_bb8103fd_door_lock"}
|
||||
headers = {'authorization': f'Bearer {self.api_key}'}
|
||||
requests.post("https://ha.zoisite.xyz/api/services/lock/lock", headers=headers, data=json.dumps(body))
|
||||
print("locked")
|
||||
|
||||
def load_secrets():
|
||||
with open("./secrets.json") as secrets:
|
||||
@@ -26,25 +29,30 @@ def load_secrets():
|
||||
if __name__ == "__main__":
|
||||
secrets = load_secrets()
|
||||
pi = pigpio.pi()
|
||||
gate = gate(pi, 4)
|
||||
door = door(secrets['api_key'])
|
||||
|
||||
current_code = ""
|
||||
def callback(bits, value):
|
||||
global current_code
|
||||
# indicates keypad press
|
||||
if bits == 4:
|
||||
print(value)
|
||||
current_code = current_code[-3:] + str(value)
|
||||
if value == 11:
|
||||
# lock door on # character
|
||||
door.lock_door()
|
||||
current_code = ""
|
||||
if len(current_code) == 4:
|
||||
# check if we have a valid code
|
||||
if current_code in secrets['codes']:
|
||||
gate.unlock_gate(5)
|
||||
door.unlock_door()
|
||||
current_code = ""
|
||||
# indicates a card swipe
|
||||
elif bits == 37:
|
||||
current_code = ""
|
||||
str_value = str(value)
|
||||
if str_value in secrets['cards']:
|
||||
gate.unlock_gate(5)
|
||||
door.unlock_door()
|
||||
|
||||
logging.info("bits={} value={}".format(bits, value))
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"cards": ["0000000000"],
|
||||
"codes": ["0000"]
|
||||
"codes": ["0000"],
|
||||
"api_key": "xxxxxxxxxx"
|
||||
}
|
||||
Reference in New Issue
Block a user