From 505fc6e07f734315941d84d1384e0594c943e247 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Mon, 22 Aug 2022 19:08:59 +0200 Subject: [PATCH] Make workable --- main.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 4c6b321..cb928b3 100644 --- a/main.py +++ b/main.py @@ -14,42 +14,43 @@ def on_message(client, userdata, msg): topic = msg.topic payload = msg.payload - - print(topic) - print(payload) - - if topic.endswith("/relay/0") and payload.decode("UTF-8") == "on": + + print(topic, file=sys.stderr) + print(payload, file=sys.stderr) + + if topic.endswith("/relay/0/command") 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": + print("UPDATE TIME", file=sys.stderr) + if topic.endswith("/relay/0/command") and payload.decode("UTF-8") == "off": LAST_TIME = None - print("RESET TIME") + print("RESET TIME", file=sys.stderr) if topic.endswith("/relay/0/power") and LAST_TIME != None: if float(payload.decode("UTF-8")) > POWER_THRESHOLD: - LAST_TIME = None - print("RESET POWER") + LAST_TIME = datetime.datetime.now() + print("RESET POWER", file=sys.stderr) 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" } } }') + print('{ "state": "Free" }') + sys.stdout.flush() LAST_TIME = None - print("RESET STATE") + print("RESET STATE", file=sys.stderr) else: - print("WAITING TIME") - + print("WAITING TIME", file=sys.stderr) + sys.stderr.flush() + def ask_exit(*args): STOP.set() def main(host): - client = client = mqtt.Client("shelly_client") + client = client = mqtt.Client(f"shelly_client_{SHELLY_ID}") 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.subscribe(f"shellies/{SHELLY_ID}/relay/0/#") + client.loop_forever() if __name__ == '__main__':