diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-01-01 02:33:33 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-01-01 02:33:33 +0300 |
commit | d2a5d8c6c2a2ae27e650ec26183e7b9881b939c7 (patch) | |
tree | c8d643d2a7fcd16c40c660bc5ea12fb355af43fb /src/relay_mqtt_util.py | |
parent | ebdedacf2f8daa75e4473f1f87129cc2a3a56893 (diff) |
relay_mqtt_*: add monitoring util, improve overall support
Diffstat (limited to 'src/relay_mqtt_util.py')
-rwxr-xr-x | src/relay_mqtt_util.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/relay_mqtt_util.py b/src/relay_mqtt_util.py new file mode 100755 index 0000000..430af2c --- /dev/null +++ b/src/relay_mqtt_util.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +from typing import Optional +from argparse import ArgumentParser + +from home.config import config +from home.mqtt import MQTTRelay, MQTTRelayDevice +from home.mqtt.payload import MQTTPayload +from home.mqtt.payload.relay import InitialStatPayload, StatPayload + +mqtt_relay: Optional[MQTTRelay] = None + + +def on_mqtt_message(device_id, message: MQTTPayload): + if isinstance(message, InitialStatPayload) or isinstance(message, StatPayload): + message = f'[{device_id}] state={message.flags.state} rssi={message.rssi}' + if isinstance(message, InitialStatPayload): + message += f' fw={message.fw_version}' + print(message) + + +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument('--device-id', type=str, required=True) + + config.load('relay_mqtt_util', parser=parser) + arg = parser.parse_args() + + mqtt_relay = MQTTRelay(devices=MQTTRelayDevice(id=arg.device_id)) + mqtt_relay.set_message_callback(on_mqtt_message) + mqtt_relay.configure_tls() + mqtt_relay.connect_and_loop() |