Skip to content

Commit 2008c1d

Browse files
committed
Add new retain configuration option.
1 parent 1513d92 commit 2008c1d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.0.2] - 2022-02-13
2+
### New
3+
* New configuration option to retain your sensor values in the MQTT broker.
4+
15
## [1.0.1] - 2022-04-08
26
### Fixes
37
* Updated documentation and added check for Mosquitto MQTT broker addon requirement.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ The `log_level` option controls the level of log output by the add-on and can be
105105
This option controls whether the Home Assistant's MQTT Discovery feature is enabled or disabled. If disabled, you can configure the sensors individually and they will be located at mqtt topic `/airthings/<mac>/<sensor name>` where <mac> is the `mac` address you set for your device and `sensor name` is the name of the sensor from the device. For example, the sensor names for the Airthings Wave Plus are: `humidity`, `radon_1day_avg`, `radon_longterm_avg`, `temperature`, `rel_atm_pressure`, `co2` and `voc`.
106106

107107

108+
### Option: `retain`
109+
110+
This option sets the "retain" flag for the sensor values sent to the MQTT broker. This means that the last sensor value will be retained by the MQTT broker, meaning that if you restart Home Assistant the last sensor values sent to the MQTT broker will show up immediately once Home Assistant restarts. The downside is that the sensor values may be out of date, particularly if the add-on has stopped. If you change this value to "false" the add-on will clear any existing retained values.
111+
112+
108113
## Current Limitations
109114

110115
* This add-on has only been tested with a single Airthings Wave Plus device, but should work with multiple devices

config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Airthings
2-
version: 1.0.1
2+
version: 1.0.2
33
slug: airthings
44
description: Read sensor values from Airthings Wave environmental monitoring devices
55
url: https://github.com/mjmccans/hassio-addon-airthings
@@ -25,6 +25,7 @@ options:
2525
retry_wait: 3
2626
log_level: INFO
2727
mqtt_discovery: "true"
28+
retain: "false"
2829
schema:
2930
devices:
3031
- mac: str?
@@ -34,4 +35,5 @@ schema:
3435
retry_wait: int
3536
log_level: str
3637
mqtt_discovery: bool
38+
retain: bool?
3739
image: mjmccans/hassio-addon-airthings-{arch}

src/airthings-mqtt.ha.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
CONFIG = {} # Variable to store configuration
3131
DEVICES = {} # Variable to store devices
3232

33-
# Sensor detail deafults (for MQTT discovery)
33+
# Sensor detail defaults (for MQTT discovery)
3434
SENSORS = {
3535
"radon_1day_avg": {"name": "Radon (1 day avg.)", "device_class": None, "unit_of_measurement": "Bq/m3", "icon": "mdi:radioactive", "state_class": "measurement"},
3636
"radon_longterm_avg": {"name": "Radon (longterm avg.)", "device_class": None, "unit_of_measurement": "Bq/m3", "icon": "mdi:radioactive", "state_class": "measurement"},
@@ -211,7 +211,7 @@ def mqtt_publish(msgs):
211211
CONFIG["mqtt"]["host"] = vars(args)['host']
212212
CONFIG["mqtt"]["port"] = vars(args)['port']
213213
CONFIG["mqtt"]["username"] = vars(args)['username']
214-
CONFIG["mqtt"]["password"] = vars(args)['password']
214+
CONFIG["mqtt"]["password"] = vars(args)['password']
215215

216216
# Set logging level (defaults to INFO)
217217
if CONFIG["log_level"] in ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]:
@@ -280,7 +280,6 @@ def mqtt_publish(msgs):
280280
_LOGGER.info("Done sending HA mqtt discovery configuration messages.")
281281
msgs = []
282282
time.sleep(5)
283-
first = False
284283

285284
# Collect all of the sensor data
286285
_LOGGER.info("Collecting sensor value messages...")
@@ -298,10 +297,17 @@ def mqtt_publish(msgs):
298297
else:
299298
val = round(val)
300299
_LOGGER.info("{} = {}".format("airthings/"+mac+"/"+name, val))
301-
msgs.append({'topic': "airthings/"+mac+"/"+name, 'payload': val})
300+
301+
# If this is a first run, clear any retained messages if "retained" is not set in config.
302+
if first and not CONFIG["retain"]:
303+
_LOGGER.debug("Appending message to delete any existing retained message...")
304+
msgs.append({'topic': "airthings/"+mac+"/"+name, 'payload': '', 'retain': True})
305+
306+
msgs.append({'topic': "airthings/"+mac+"/"+name, 'payload': val, 'retain': CONFIG["retain"]})
302307

303308
# Publish the sensor data to mqtt broker
304309
mqtt_publish(msgs)
310+
first = False
305311
else:
306312
_LOGGER.error("\033[31mNo sensor values collected. Please check your configuration and make sure your bluetooth adapter is available. If the watchdog option is enabled, this addon will restart and try again.\033[0m")
307313
sys.exit(1)

0 commit comments

Comments
 (0)