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 time
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
from wiegand import decoder
|
from wiegand import decoder
|
||||||
|
|
||||||
class gate:
|
class door:
|
||||||
def __init__(self, pi, relay_pin):
|
def __init__(self, api_key):
|
||||||
self.pi = pi
|
self.api_key = api_key
|
||||||
self.relay_pin = relay_pin
|
|
||||||
|
|
||||||
# set the pin to high to keep the gate locked
|
def unlock_door(self):
|
||||||
pi.write(relay_pin, 1)
|
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 lock_door(self):
|
||||||
def unlock_gate(self, timeout):
|
body = {"entity_id": "lock.kwikset_smartcode_convert_gen1_bb8103fd_door_lock"}
|
||||||
self.pi.write(self.relay_pin, 0)
|
headers = {'authorization': f'Bearer {self.api_key}'}
|
||||||
time.sleep(timeout)
|
requests.post("https://ha.zoisite.xyz/api/services/lock/lock", headers=headers, data=json.dumps(body))
|
||||||
self.pi.write(self.relay_pin, 1)
|
print("locked")
|
||||||
|
|
||||||
def load_secrets():
|
def load_secrets():
|
||||||
with open("./secrets.json") as secrets:
|
with open("./secrets.json") as secrets:
|
||||||
@@ -26,25 +29,30 @@ def load_secrets():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
secrets = load_secrets()
|
secrets = load_secrets()
|
||||||
pi = pigpio.pi()
|
pi = pigpio.pi()
|
||||||
gate = gate(pi, 4)
|
door = door(secrets['api_key'])
|
||||||
|
|
||||||
current_code = ""
|
current_code = ""
|
||||||
def callback(bits, value):
|
def callback(bits, value):
|
||||||
global current_code
|
global current_code
|
||||||
# indicates keypad press
|
# indicates keypad press
|
||||||
if bits == 4:
|
if bits == 4:
|
||||||
|
print(value)
|
||||||
current_code = current_code[-3:] + str(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:
|
if len(current_code) == 4:
|
||||||
# check if we have a valid code
|
# check if we have a valid code
|
||||||
if current_code in secrets['codes']:
|
if current_code in secrets['codes']:
|
||||||
gate.unlock_gate(5)
|
door.unlock_door()
|
||||||
current_code = ""
|
current_code = ""
|
||||||
# indicates a card swipe
|
# indicates a card swipe
|
||||||
elif bits == 37:
|
elif bits == 37:
|
||||||
current_code = ""
|
current_code = ""
|
||||||
str_value = str(value)
|
str_value = str(value)
|
||||||
if str_value in secrets['cards']:
|
if str_value in secrets['cards']:
|
||||||
gate.unlock_gate(5)
|
door.unlock_door()
|
||||||
|
|
||||||
logging.info("bits={} value={}".format(bits, value))
|
logging.info("bits={} value={}".format(bits, value))
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"cards": ["0000000000"],
|
"cards": ["0000000000"],
|
||||||
"codes": ["0000"]
|
"codes": ["0000"],
|
||||||
|
"api_key": "xxxxxxxxxx"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user