From ef912b04a21832f1ef1f5bda5ffe9f13ce3206e7 Mon Sep 17 00:00:00 2001 From: Marcos Junior Date: Tue, 4 Jan 2022 14:24:03 -0600 Subject: [PATCH] Fix qos --- .github/workflows/main.yaml | 1 + meterparser/CHANGELOG.md | 1 + meterparser/config.yaml | 2 +- meterparser/src/app/mqtt.py | 22 ++++++++++++++-------- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index c4f5cb2..b4476eb 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -126,6 +126,7 @@ jobs: with: path: "./${{ matrix.addon }}" - name: Get version + id: version run: | version="${{ steps.info.outputs.version }}" version="${version%\"}" diff --git a/meterparser/CHANGELOG.md b/meterparser/CHANGELOG.md index 7d94320..5a12f14 100644 --- a/meterparser/CHANGELOG.md +++ b/meterparser/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## [1.0.0.*] +- Set qos - Rewrite topic to allow concurrent instances. - Send current value to sensor on intervals - Fix service type on devices diff --git a/meterparser/config.yaml b/meterparser/config.yaml index 23d33e6..5cf2ffc 100644 --- a/meterparser/config.yaml +++ b/meterparser/config.yaml @@ -1,5 +1,5 @@ name: Meter Parser -version: 1.0.0.6 +version: 1.0.0.7 slug: meter-parser description: Read meter needles and numbers from a camera snapshot. url: https://github.com/junalmeida/homeassistant-addons/meterparser diff --git a/meterparser/src/app/mqtt.py b/meterparser/src/app/mqtt.py index 7a3d629..4ef44b7 100644 --- a/meterparser/src/app/mqtt.py +++ b/meterparser/src/app/mqtt.py @@ -23,6 +23,7 @@ def __init__(self): self._mqtt_client = mqtt.Client("meter-parser-addon") self._mqtt_client.on_connect = self.mqtt_connected self._mqtt_client.on_disconnect = self.mqtt_disconnected + self._mqtt_client.on_publish = self.mqtt_publish self.cameras: list = list() self.device_id = slugify((os.environ["HOSTNAME"] if "HOSTNAME" in os.environ else os.environ["COMPUTERNAME"]).lower()) @@ -42,15 +43,17 @@ def mqtt_connected(self, client, userdata, flags, rc): if camera is None: camera = Camera(cfg, entity_id, self, config["debug_path"] if "debug_path" in config else None) self.cameras.append(camera) - camera.start() - self.mqtt_sensor_discovery(camera.entity_id, camera.name, camera._device_class, camera._unit_of_measurement) + camera.start() + self.mqtt_sensor_discovery(camera.entity_id, camera.name, camera._device_class, camera._unit_of_measurement) def mqtt_disconnected(self, client, userdata, rc): - if not self._mqtt_client.is_connected: + if not self._mqtt_client.is_connected(): for camera in self.cameras: camera.stop() self.cameras.clear() _LOGGER.debug("%s camera(s) running" % len(self.cameras)) + def mqtt_publish(self, client, userdata, mid): + _LOGGER.debug("Message #%s delivered to %s" % (mid, client._host)) def mqtt_start(self): while True: @@ -101,17 +104,20 @@ def mqtt_sensor_discovery(self, entity_id: str, name: str, device_class: str, un "device": self.device } - self._mqtt_client.publish(topic_sensor, payload=json.dumps(payload_sensor).encode("utf-8")) - self._mqtt_client.publish(topic_camera, payload=json.dumps(payload_camera).encode("utf-8")) + self._mqtt_client.publish(topic_sensor, payload=json.dumps(payload_sensor), qos=2) + self._mqtt_client.publish(topic_camera, payload=json.dumps(payload_camera), qos=2) def mqtt_set_state(self, type:str, entity_id: str, state): topic = "%s/%s/%s/%s/state" % (discovery_prefix, type, self.device_id, entity_id) - self._mqtt_client.publish(topic, payload=state) + result = self._mqtt_client.publish(topic, payload=state, qos=2) + _LOGGER.debug("State #%s scheduled to %s=%s" % (result.mid, entity_id, state)) def mqtt_set_attributes(self, type:str, entity_id: str, attributes): topic = "%s/%s/%s/%s/attributes" % (discovery_prefix, type, self.device_id, entity_id) - self._mqtt_client.publish(topic, payload=json.dumps(attributes)) + result = self._mqtt_client.publish(topic, payload=json.dumps(attributes), qos=2) + _LOGGER.debug("Attributes #%s scheduled to %s" % (result.mid, entity_id)) def mqtt_set_availability(self, type:str, entity_id: str, available: bool): topic = "%s/%s/%s/%s/availability" % (discovery_prefix, type, self.device_id, entity_id) - self._mqtt_client.publish(topic, payload=("online" if available else "offline")) + result = self._mqtt_client.publish(topic, payload=("online" if available else "offline"), qos=2) + _LOGGER.debug("Availability #%s scheduled to %s=%s" % (result.mid, entity_id, available))