Skip to content

Commit

Permalink
Fix qos
Browse files Browse the repository at this point in the history
  • Loading branch information
junalmeida committed Jan 4, 2022
1 parent 5652613 commit ef912b0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ jobs:
with:
path: "./${{ matrix.addon }}"
- name: Get version
id: version
run: |
version="${{ steps.info.outputs.version }}"
version="${version%\"}"
Expand Down
1 change: 1 addition & 0 deletions meterparser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion meterparser/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 14 additions & 8 deletions meterparser/src/app/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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:
Expand Down Expand Up @@ -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))

0 comments on commit ef912b0

Please sign in to comment.