Make workable

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

29
main.py
View File

@ -15,40 +15,41 @@ def on_message(client, userdata, msg):
topic = msg.topic topic = msg.topic
payload = msg.payload payload = msg.payload
print(topic) print(topic, file=sys.stderr)
print(payload) print(payload, file=sys.stderr)
if topic.endswith("/relay/0") and payload.decode("UTF-8") == "on": if topic.endswith("/relay/0/command") and payload.decode("UTF-8") == "on":
if LAST_TIME == None: if LAST_TIME == None:
LAST_TIME = datetime.datetime.now() LAST_TIME = datetime.datetime.now()
print("UPDATE TIME") print("UPDATE TIME", file=sys.stderr)
if topic.endswith("/relay/0") and payload.decode("UTF-8") == "off": if topic.endswith("/relay/0/command") and payload.decode("UTF-8") == "off":
LAST_TIME = None LAST_TIME = None
print("RESET TIME") print("RESET TIME", file=sys.stderr)
if topic.endswith("/relay/0/power") and LAST_TIME != None: if topic.endswith("/relay/0/power") and LAST_TIME != None:
if float(payload.decode("UTF-8")) > POWER_THRESHOLD: if float(payload.decode("UTF-8")) > POWER_THRESHOLD:
LAST_TIME = None LAST_TIME = datetime.datetime.now()
print("RESET POWER") print("RESET POWER", file=sys.stderr)
elif (datetime.datetime.now() - LAST_TIME).total_seconds() > TIME_THRESHOLD: elif (datetime.datetime.now() - LAST_TIME).total_seconds() > TIME_THRESHOLD:
#client.publish(f"shellies/{SHELLY_ID}/relay/0", "off") #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 LAST_TIME = None
print("RESET STATE") print("RESET STATE", file=sys.stderr)
else: else:
print("WAITING TIME") print("WAITING TIME", file=sys.stderr)
sys.stderr.flush()
def ask_exit(*args): def ask_exit(*args):
STOP.set() STOP.set()
def main(host): def main(host):
client = client = mqtt.Client("shelly_client") client = client = mqtt.Client(f"shelly_client_{SHELLY_ID}")
client.on_message = on_message client.on_message = on_message
client.connect(host) client.connect(host)
client.subscribe(f"shellies/{SHELLY_ID}/relay/0") client.subscribe(f"shellies/{SHELLY_ID}/relay/0/#")
client.subscribe(f"shellies/{SHELLY_ID}/relay/0/power")
client.loop_forever() client.loop_forever()