From 27f8dbadeac5da8644eb1c692a08bfb1be27cd98 Mon Sep 17 00:00:00 2001 From: EpicKiwi Date: Wed, 19 Jul 2023 10:20:07 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ main.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + settings.py | 17 ++++++++++ test/a.txt | 1 + test/b.txt | 1 + test/status.txt | 1 + 7 files changed, 103 insertions(+) create mode 100644 .gitignore create mode 100755 main.py create mode 100644 requirements.txt create mode 100644 settings.py create mode 100644 test/a.txt create mode 100644 test/b.txt create mode 120000 test/status.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f7550b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +.venv diff --git a/main.py b/main.py new file mode 100755 index 0000000..aefb04c --- /dev/null +++ b/main.py @@ -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() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5c2d945 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +zigate \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..3633746 --- /dev/null +++ b/settings.py @@ -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" \ No newline at end of file diff --git a/test/a.txt b/test/a.txt new file mode 100644 index 0000000..5321c6e --- /dev/null +++ b/test/a.txt @@ -0,0 +1 @@ +A : Opened \ No newline at end of file diff --git a/test/b.txt b/test/b.txt new file mode 100644 index 0000000..ae86b10 --- /dev/null +++ b/test/b.txt @@ -0,0 +1 @@ +B : Closed \ No newline at end of file diff --git a/test/status.txt b/test/status.txt new file mode 120000 index 0000000..8d14cbf --- /dev/null +++ b/test/status.txt @@ -0,0 +1 @@ +a.txt \ No newline at end of file