diff options
Diffstat (limited to 'src/mqtt_node_util.py')
-rwxr-xr-x | src/mqtt_node_util.py | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/src/mqtt_node_util.py b/src/mqtt_node_util.py index 0352c8f..d7ee127 100755 --- a/src/mqtt_node_util.py +++ b/src/mqtt_node_util.py @@ -5,19 +5,10 @@ from typing import Optional from argparse import ArgumentParser, ArgumentError from home.config import config -from home.mqtt import MqttNode, get_mqtt_modules, import_mqtt_module, MqttModule +from home.mqtt import MqttNode, MqttModule, MqttWrapper, get_mqtt_modules -mqtt: Optional[MqttNode] = None - - -def add_module(module: str) -> MqttModule: - module = import_mqtt_module(module) - if not hasattr(module, 'MODULE_NAME'): - raise RuntimeError(f'MODULE_NAME not found in module {m}') - cl = getattr(module, getattr(module, 'MODULE_NAME')) - instance = cl() - mqtt.add_module(instance) - return instance +mqtt_node: Optional[MqttNode] = None +mqtt: Optional[MqttWrapper] = None if __name__ == '__main__': @@ -38,19 +29,22 @@ if __name__ == '__main__': if (arg.switch_relay is not None or arg.node_secret is not None) and 'relay' not in arg.modules: raise ArgumentError(None, '--relay is only allowed when \'relay\' module included in --modules') - mqtt = MqttNode(node_id=arg.node_id) + mqtt = MqttWrapper(randomize_client_id=True) + mqtt_node = MqttNode(node_id=arg.node_id) + + mqtt.add_node(mqtt_node) # must-have modules - ota_module = add_module('ota') - add_module('diagnostics') + ota_module = mqtt_node.load_module('ota') + mqtt_node.load_module('diagnostics') if arg.modules: for m in arg.modules: - module_instance = add_module(m) + module_instance = mqtt_node.load_module(m) if m == 'relay' and arg.switch_relay is not None: if not arg.node_secret: raise ArgumentError(None, '--switch-relay requires --node-secret') - module_instance.switchpower(mqtt, + module_instance.switchpower(mqtt_node, arg.switch_relay == 1, arg.node_secret) @@ -61,10 +55,10 @@ if __name__ == '__main__': if arg.push_ota: if not os.path.exists(arg.push_ota): raise OSError(f'--push-ota: file \"{arg.push_ota}\" does not exists') - if not arg.node_secret: - raise ArgumentError(None, 'pushing OTA requires --node-secret') + if not arg.node_secret: + raise ArgumentError(None, 'pushing OTA requires --node-secret') - ota_module.push_ota(arg.node_secret, arg.push_ota, 1) + ota_module.push_ota(arg.node_secret, arg.push_ota, 1) while True: sleep(0.1) |