mirror of
https://gitlab.com/fabinfra/fabaccess/initiators/shelly-timeout.git
synced 2025-03-12 14:51:51 +01:00
61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
import datetime
|
|
import sys
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
SHELLY_ID = ""
|
|
POWER_THRESHOLD = 0
|
|
TIME_THRESHOLD = 15 * 60
|
|
|
|
LAST_TIME = None
|
|
|
|
def on_message(client, userdata, msg):
|
|
global LAST_TIME
|
|
|
|
topic = msg.topic
|
|
payload = msg.payload
|
|
|
|
print(topic)
|
|
print(payload)
|
|
|
|
if topic.endswith("/relay/0") and payload.decode("UTF-8") == "on":
|
|
if LAST_TIME == None:
|
|
LAST_TIME = datetime.datetime.now()
|
|
print("UPDATE TIME")
|
|
if topic.endswith("/relay/0") and payload.decode("UTF-8") == "off":
|
|
LAST_TIME = None
|
|
print("RESET TIME")
|
|
|
|
if topic.endswith("/relay/0/power") and LAST_TIME != None:
|
|
if float(payload.decode("UTF-8")) > POWER_THRESHOLD:
|
|
LAST_TIME = None
|
|
print("RESET POWER")
|
|
elif (datetime.datetime.now() - LAST_TIME).total_seconds() > TIME_THRESHOLD:
|
|
#client.publish(f"shellies/{SHELLY_ID}/relay/0", "off")
|
|
print('{ "state": { "1.3.6.1.4.1.48398.612.2.4": { "state": "Free" } } }')
|
|
LAST_TIME = None
|
|
print("RESET STATE")
|
|
else:
|
|
print("WAITING TIME")
|
|
|
|
def ask_exit(*args):
|
|
STOP.set()
|
|
|
|
def main(host):
|
|
client = client = mqtt.Client("shelly_client")
|
|
|
|
client.on_message = on_message
|
|
|
|
client.connect(host)
|
|
client.subscribe(f"shellies/{SHELLY_ID}/relay/0")
|
|
client.subscribe(f"shellies/{SHELLY_ID}/relay/0/power")
|
|
|
|
client.loop_forever()
|
|
|
|
if __name__ == '__main__':
|
|
host = sys.argv[1]
|
|
SHELLY_ID = sys.argv[2]
|
|
POWER_THRESHOLD = int(sys.argv[3])
|
|
|
|
main(host)
|