mirror of
https://gitlab.com/fabinfra/fabaccess/initiators/shelly-timeout.git
synced 2025-03-12 14:51:51 +01:00
Changed MQTT Lib
This commit is contained in:
parent
a2a2c1f16d
commit
5a176fbd91
29
main.py
29
main.py
@ -1,9 +1,7 @@
|
|||||||
import asyncio
|
|
||||||
import signal
|
|
||||||
import datetime
|
import datetime
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from gmqtt import Client as MQTTClient
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
SHELLY_ID = ""
|
SHELLY_ID = ""
|
||||||
POWER_THRESHOLD = 0
|
POWER_THRESHOLD = 0
|
||||||
@ -11,11 +9,12 @@ TIME_THRESHOLD = 15 * 60
|
|||||||
|
|
||||||
LAST_TIME = None
|
LAST_TIME = None
|
||||||
|
|
||||||
STOP = asyncio.Event()
|
def on_message(client, userdata, msg):
|
||||||
|
|
||||||
def on_message(client, topic, payload, qos, properties):
|
|
||||||
global LAST_TIME
|
global LAST_TIME
|
||||||
|
|
||||||
|
topic = msg.topic
|
||||||
|
payload = msg.payload
|
||||||
|
|
||||||
print(topic)
|
print(topic)
|
||||||
print(payload)
|
print(payload)
|
||||||
|
|
||||||
@ -42,26 +41,20 @@ def on_message(client, topic, payload, qos, properties):
|
|||||||
def ask_exit(*args):
|
def ask_exit(*args):
|
||||||
STOP.set()
|
STOP.set()
|
||||||
|
|
||||||
async def main(host):
|
def main(host):
|
||||||
client = MQTTClient("shelly_client")
|
client = client = mqtt.Client("shelly_client")
|
||||||
|
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
|
|
||||||
await 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/energy")
|
client.subscribe(f"shellies/{SHELLY_ID}/relay/0/power")
|
||||||
|
|
||||||
await STOP.wait()
|
client.loop_forever()
|
||||||
await client.disconnect()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
host = sys.argv[1]
|
host = sys.argv[1]
|
||||||
SHELLY_ID = sys.argv[2]
|
SHELLY_ID = sys.argv[2]
|
||||||
POWER_THRESHOLD = int(sys.argv[3])
|
POWER_THRESHOLD = int(sys.argv[3])
|
||||||
|
|
||||||
loop.add_signal_handler(signal.SIGINT, ask_exit)
|
main(host)
|
||||||
loop.add_signal_handler(signal.SIGTERM, ask_exit)
|
|
||||||
|
|
||||||
loop.run_until_complete(main(host))
|
|
||||||
|
@ -1 +1 @@
|
|||||||
gmqtt
|
paho-mqtt
|
Loading…
x
Reference in New Issue
Block a user