From 36454a082a2b5461d5c0a634fece17a7c70cbc11 Mon Sep 17 00:00:00 2001 From: Daniel Havlik Date: Thu, 26 May 2022 00:29:07 +0200 Subject: [PATCH] allow to set client id of mqtt connection in config, to support multiple instances of sungather --- SunGather/config-example.yaml | 1 + SunGather/exports/mqtt.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/SunGather/config-example.yaml b/SunGather/config-example.yaml index 19a2e27..014fe66 100644 --- a/SunGather/config-example.yaml +++ b/SunGather/config-example.yaml @@ -64,6 +64,7 @@ exports: # topic: "inverter/{model}/registers" # [Optional] Default: "inverter/{model}/registers", Variable {model} will be replaced with model number of inverter # username: # [Optional] Username is MQTT server requires it # password: # [Optional] Password is MQTT server requires it + # client_id: # [Optional] Client id for mqtt connection. Defaults to "SunGather", need to be set to different values if you want to run multiple instances of sungather. homeassistant: True # [Optional] Default False, Publish discovery and sensors for HomeAssistant ha_sensors: # [Optional] / [Required] ha_discovery: True, Topics to enable discovery for HA - name: "Daily Generation" diff --git a/SunGather/exports/mqtt.py b/SunGather/exports/mqtt.py index 6896d1c..83a8442 100644 --- a/SunGather/exports/mqtt.py +++ b/SunGather/exports/mqtt.py @@ -14,6 +14,7 @@ class export_mqtt(object): self.mqtt_config = { 'host': config.get('host', None), 'port': config.get('port', 1883), + 'client_id': config.get('client_id', 'SunGather'), 'topic': config.get('topic', "inverter/{model}/registers").replace('{model}', inverter.getInverterModel(True)), 'username': config.get('username', None), 'password': config.get('password',None), @@ -26,8 +27,8 @@ class export_mqtt(object): if not self.mqtt_config['host']: logging.info(f"MQTT: Host config is required") return False - - self.mqtt_client = mqtt.Client(client_id="SunGather") + client_id = self.mqtt_config['client_id'] + self.mqtt_client = mqtt.Client(client_id) self.mqtt_client.on_connect = self.on_connect self.mqtt_client.on_disconnect = self.on_disconnect self.mqtt_client.on_publish = self.on_publish @@ -108,4 +109,4 @@ class export_mqtt(object): self.ha_discovery_published = True logging.info("MQTT: Published Home Assistant Discovery messages") - return True \ No newline at end of file + return True