Skip to content

Commit

Permalink
Add new retain configuration option.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmccans committed Apr 14, 2022
1 parent 1513d92 commit 2008c1d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.0.2] - 2022-02-13
### New
* New configuration option to retain your sensor values in the MQTT broker.

## [1.0.1] - 2022-04-08
### Fixes
* Updated documentation and added check for Mosquitto MQTT broker addon requirement.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ The `log_level` option controls the level of log output by the add-on and can be
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`.


### Option: `retain`

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.


## Current Limitations

* This add-on has only been tested with a single Airthings Wave Plus device, but should work with multiple devices
Expand Down
4 changes: 3 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Airthings
version: 1.0.1
version: 1.0.2
slug: airthings
description: Read sensor values from Airthings Wave environmental monitoring devices
url: https://github.com/mjmccans/hassio-addon-airthings
Expand All @@ -25,6 +25,7 @@ options:
retry_wait: 3
log_level: INFO
mqtt_discovery: "true"
retain: "false"
schema:
devices:
- mac: str?
Expand All @@ -34,4 +35,5 @@ schema:
retry_wait: int
log_level: str
mqtt_discovery: bool
retain: bool?
image: mjmccans/hassio-addon-airthings-{arch}
14 changes: 10 additions & 4 deletions src/airthings-mqtt.ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
CONFIG = {} # Variable to store configuration
DEVICES = {} # Variable to store devices

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

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

# Collect all of the sensor data
_LOGGER.info("Collecting sensor value messages...")
Expand All @@ -298,10 +297,17 @@ def mqtt_publish(msgs):
else:
val = round(val)
_LOGGER.info("{} = {}".format("airthings/"+mac+"/"+name, val))
msgs.append({'topic': "airthings/"+mac+"/"+name, 'payload': val})

# If this is a first run, clear any retained messages if "retained" is not set in config.
if first and not CONFIG["retain"]:
_LOGGER.debug("Appending message to delete any existing retained message...")
msgs.append({'topic': "airthings/"+mac+"/"+name, 'payload': '', 'retain': True})

msgs.append({'topic': "airthings/"+mac+"/"+name, 'payload': val, 'retain': CONFIG["retain"]})

# Publish the sensor data to mqtt broker
mqtt_publish(msgs)
first = False
else:
_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")
sys.exit(1)
Expand Down

0 comments on commit 2008c1d

Please sign in to comment.