Make workable

This commit is contained in:
Nadja Reitzenstein 2022-08-22 19:08:59 +02:00
parent 5a176fbd91
commit 505fc6e07f

39
main.py
View File

@ -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__':