Initial commit
This commit is contained in:
commit
27f8dbadea
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
__pycache__
|
||||
.venv
|
80
main.py
Executable file
80
main.py
Executable file
@ -0,0 +1,80 @@
|
||||
#!/bin/env python
|
||||
import zigate
|
||||
from zigate import dispatcher
|
||||
import time
|
||||
import os
|
||||
from datetime import datetime
|
||||
from settings import MAGNET_ATTRIBUTE, MAGNET_ADDR, MAGNET_ATTRIBUTE_OPENED_VALUE, OPENING_TRIGGER_DELAY, LOL_OPENED_CMD, LOL_CLOSED_CMD, CLOSING_TRIGGER_DELAY
|
||||
|
||||
z = zigate.connect(port=None)
|
||||
z.get_devices_list()
|
||||
|
||||
is_closing = False
|
||||
is_opening = False
|
||||
|
||||
def trigger_opening():
|
||||
global is_opening
|
||||
global is_closing
|
||||
|
||||
is_closing = False
|
||||
|
||||
# An opening is already in process
|
||||
if is_opening or is_closing:
|
||||
return
|
||||
|
||||
is_opening = True
|
||||
for i in range(0, OPENING_TRIGGER_DELAY):
|
||||
z.set_led(on=False)
|
||||
time.sleep(0.25)
|
||||
z.set_led(on=True)
|
||||
time.sleep(0.75)
|
||||
|
||||
# Check if we still opening room
|
||||
if not is_opening:
|
||||
return
|
||||
|
||||
is_opening = False
|
||||
|
||||
os.system(LOL_OPENED_CMD)
|
||||
print(datetime.now().isoformat()+" LOL opened")
|
||||
|
||||
def trigger_closing():
|
||||
global is_opening
|
||||
global is_closing
|
||||
|
||||
if is_opening:
|
||||
return
|
||||
|
||||
is_closing = True
|
||||
|
||||
for i in range(0, CLOSING_TRIGGER_DELAY):
|
||||
time.sleep(1)
|
||||
|
||||
# Check if we still opening room
|
||||
if not is_closing:
|
||||
return
|
||||
|
||||
is_closing = False
|
||||
is_opening = False
|
||||
z.set_led(on=True)
|
||||
|
||||
os.system(LOL_CLOSED_CMD)
|
||||
print(datetime.now().isoformat()+" LOL closed")
|
||||
|
||||
def on_zigbee_attribute_updated(sender, signal, attribute, **kwargs):
|
||||
if attribute["addr"] == MAGNET_ADDR and attribute["name"] == MAGNET_ATTRIBUTE:
|
||||
if attribute["value"] == MAGNET_ATTRIBUTE_OPENED_VALUE:
|
||||
trigger_opening()
|
||||
else:
|
||||
trigger_closing()
|
||||
|
||||
dispatcher.connect(on_zigbee_attribute_updated, zigate.ZIGATE_ATTRIBUTE_UPDATED)
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
print('Interrupted by user')
|
||||
|
||||
z.save_state()
|
||||
z.close()
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
zigate
|
17
settings.py
Normal file
17
settings.py
Normal file
@ -0,0 +1,17 @@
|
||||
# ZigBee address of door magnet sensor
|
||||
MAGNET_ADDR = "d617"
|
||||
|
||||
# Attribute name defning if door is opened or not
|
||||
MAGNET_ATTRIBUTE = "onoff"
|
||||
|
||||
# Value of previous attribute that represents opened door
|
||||
MAGNET_ATTRIBUTE_OPENED_VALUE = True
|
||||
|
||||
# Delay to wait before marking room as opened
|
||||
OPENING_TRIGGER_DELAY = 30
|
||||
|
||||
# Delay to wait before marking room as closed
|
||||
CLOSING_TRIGGER_DELAY = 5
|
||||
|
||||
LOL_OPENED_CMD = "ln -f -s a.txt test/status.txt"
|
||||
LOL_CLOSED_CMD = "ln -f -s b.txt test/status.txt"
|
1
test/a.txt
Normal file
1
test/a.txt
Normal file
@ -0,0 +1 @@
|
||||
A : Opened
|
1
test/b.txt
Normal file
1
test/b.txt
Normal file
@ -0,0 +1 @@
|
||||
B : Closed
|
1
test/status.txt
Symbolic link
1
test/status.txt
Symbolic link
@ -0,0 +1 @@
|
||||
a.txt
|
Loading…
Reference in New Issue
Block a user