Skip to content

Commit

Permalink
Add support for retain flag on sensor values (#153)
Browse files Browse the repository at this point in the history
* Add support for retain flag on sensor values
Fixes #152

* Update src/mqttManager.ts

* Update changelog
  • Loading branch information
neilenns authored Feb 1, 2024
1 parent 024e069 commit b811ba1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Version

## 4.6.0

* Add retain flag for `lastRain` sensor and new environment variable for enabling retain of all sensor values. (Fixes [#152](https://github.com/neilenns/ambientweather2mqtt/issues/152))

## 4.5.1

* Remove bashio calls from HomeAssistant add-on. (Fixes [[#147](https://github.com/neilenns/ambientweather2mqtt/issues/147)])
* Remove bashio calls from HomeAssistant add-on. (Fixes [#147](https://github.com/neilenns/ambientweather2mqtt/issues/147))

## 4.5.0

Expand Down
2 changes: 2 additions & 0 deletions hassio_aw2m/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ options:
STATION_MAC_ADDRESS: "00:00:00:00:00:00"
TZ: "America/Los_Angeles"
LOG_LEVEL: "info"
RETAIN_SENSOR_VALUES: false
schema:
MQTT_SERVER: "url"
MQTT_USERNAME: "str"
Expand All @@ -23,6 +24,7 @@ schema:
STATION_MAC_ADDRESS: "str"
TZ: "str"
LOG_LEVEL: list(error|warn|info|http|debug)
RETAIN_SENSOR_VALUES: "bool"
services:
- "mqtt:need"
arch:
Expand Down
7 changes: 6 additions & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DeviceClass from "./deviceClass.js";
import EntityCategory from "./entityCategory.js";
import EntityDataPayload from "./entityDataPayload.js";
import EntityDiscoveryPayload from "./entityDiscoveryPayload.js";
import env from "./env.js";
import mainLogger from "./log.js";
import * as mqttManager from "./mqttManager.js";
import SensorUnit from "./sensorUnit.js";
Expand All @@ -25,6 +26,7 @@ export default class Entity {
public availabilityTopic: string;
public value: EntityDataPayload;
public discoveryPayload: EntityDiscoveryPayload;
public retain: boolean;

private deviceId: string;

Expand All @@ -36,6 +38,7 @@ export default class Entity {
* @param deviceClass The device class for the sensor. Optional.
* @param icon The mdi icon for the sensor. Optional.
* @param entityCategory The Home Assistant entity category for the sensor. Optional.
* @param retain True if the MQTT data topic should be sent with the retain flag set to true. Optional, default false.
*/
constructor(
name: string,
Expand All @@ -44,8 +47,10 @@ export default class Entity {
deviceClass?: DeviceClass,
icon?: string,
entityCategory?: EntityCategory,
retain = false,
) {
this.deviceId = deviceId;
this.retain = retain;

this.discoveryPayload = {
device: {
Expand Down Expand Up @@ -103,7 +108,7 @@ export default class Entity {
return mqttManager.publish(this.stateTopic, this.value.toUTCString());
}

return mqttManager.publish(this.stateTopic, this.value.toString());
return mqttManager.publish(this.stateTopic, this.value.toString(), this.retain || env().RETAIN_SENSOR_VALUES);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/entityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ export function initialize(): void {
);
entities.set(
EntityNames.LASTRAIN,
new Sensor(EntityNames.LASTRAIN, deviceId, undefined, DeviceClass.TIMESTAMP, "clock-outline"),
new Sensor(EntityNames.LASTRAIN, deviceId, undefined, DeviceClass.TIMESTAMP, "clock-outline", undefined, true),
);
entities.set(
EntityNames.DEWPOINT1,
Expand Down
1 change: 1 addition & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const envSchema = z.object({
TZ: z.string().default("America/Los_Angeles"),
TOPIC_ROOT: z.string().optional(),
NODE_ENV: z.string().default("production"),
RETAIN_SENSOR_VALUES: z.string().transform<boolean>(booleanTransformer).default("false"),
});

let parsedEnv: z.infer<typeof envSchema>;
Expand Down
2 changes: 1 addition & 1 deletion src/mqttManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function publish(topic: string, data: string, retain?: boolean): Pr
return;
}

logger.debug(`Publishing ${data} to ${topic}`, { data, topic });
logger.debug(`Publishing ${data} to ${topic} with retain: ${retain}`, { data, topic, retain });
return client.publish(topic, data, { retain });
}

Expand Down
3 changes: 2 additions & 1 deletion src/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export default class Sensor extends Entity {
deviceClass?: DeviceClass,
icon?: string,
entityCategory?: EntityCategory,
retain = false,
) {
super(name, deviceId, unit, deviceClass, icon, entityCategory);
super(name, deviceId, unit, deviceClass, icon, entityCategory, retain);

this.discoveryTopic = `${TopicRoot}/sensor/${deviceId}/${this.discoveryPayload.name}/config`;
this.stateTopic = `${TopicRoot}/sensor/${deviceId}/${this.discoveryPayload.name}/state`;
Expand Down

0 comments on commit b811ba1

Please sign in to comment.