Skip to content

Commit

Permalink
Support MQTT device retention data flag settings
Browse files Browse the repository at this point in the history
  • Loading branch information
shaonianzhentan committed Oct 9, 2023
1 parent 5eedf36 commit f94f914
Show file tree
Hide file tree
Showing 31 changed files with 79 additions and 95 deletions.
6 changes: 4 additions & 2 deletions HomeAssistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function object_id(name) {

const DiscoveryDevice = {}
module.exports = class HomeAssistant {
constructor(node, cfg, device_info) {
constructor(node, cfg, { device_info, retain }) {
this.retain = retain === true
this.device_info = device_info
node.config = cfg.config
this.node = node
Expand Down Expand Up @@ -144,7 +145,8 @@ module.exports = class HomeAssistant {
payload = String(payload)
break;
}
this.node.server.client.publish(topic, payload)

this.node.server.client.publish(topic, payload, { retain: this.retain })
if (msg) {
this.node.status({ fill: "green", shape: "ring", text: `${msg}${payload}` });
}
Expand Down
2 changes: 1 addition & 1 deletion binary_sensor/binary_sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function (RED) {
this.server.register(this)

const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const { json_attr_t, command_topic } = ha.config
const node = this
node.on('input', function (msg) {
Expand Down
2 changes: 1 addition & 1 deletion camera/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion climate/climate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this

const {
Expand Down
2 changes: 1 addition & 1 deletion cover/cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const { command_topic, state_topic, set_position_topic, position_topic, tilt_command_topic, tilt_status_topic } = ha.config
const node = this
node.on('input', function (msg) {
Expand Down
38 changes: 24 additions & 14 deletions device/device.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@
<div class="form-row">
<a href="https://developers.home-assistant.io/docs/device_registry_index/" target="_blank" style="color:blue;">
<label for="node-input-config" data-i18n="node-red-contrib-ha-mqtt/common:label.config"></label>
</a>
</a>
<input type="text" id="node-config-input-config" data-i18n="[placeholder]node-red-contrib-ha-mqtt/common:label.config">
</div>
<div class="form-row">
<label for="node-config-input-retain">
retain
</label>
<label style="width: 70%;">
<input type="checkbox" id="node-config-input-retain" style="display:inline-block; width:22px; vertical-align:top;" autocomplete="off"/>
<span data-i18n="node-red-contrib-ha-mqtt/common:label.retainMessage" style="user-select: none;"></span>
</label>
</div>
</script>

<script type="text/javascript">
RED.nodes.registerType('ha-mqtt-device', {
category: 'config',
defaults: {
name: { value: "", required: true },
config: { value: "" }
},
label: function () {
return this.name.trim();
},
oneditprepare: function () {
$("#node-config-input-config").typedInput({ type: "json", types: ["json"] })
}
});
RED.nodes.registerType('ha-mqtt-device', {
category: 'config',
defaults: {
name: { value: "", required: true },
config: { value: "" },
retain: { value: false }
},
label: function () {
return this.name.trim();
},
oneditprepare: function () {
$("#node-config-input-config").typedInput({ type: "json", types: ["json"] })
}
});
</script>
37 changes: 19 additions & 18 deletions device/device.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
const HomeAssistant = require('../HomeAssistant')
module.exports = function (RED) {
RED.nodes.registerType('ha-mqtt-device', function (cfg) {
RED.nodes.createNode(this, cfg);
let { name, config } = cfg
name = name.trim()
if (!name) {
name = 'Home Assistant'
}
config = config ? JSON.parse(config) : {}
this.device_info = {
configuration_url: 'https://github.com/shaonianzhentan/node-red-contrib-ha-mqtt',
identifiers: `ha-mqtt-${name}`,
manufacturer: "shaonianzhentan",
model: 'HA-MQTT',
sw_version: HomeAssistant.version,
...config,
name
}
})
RED.nodes.registerType('ha-mqtt-device', function (cfg) {
RED.nodes.createNode(this, cfg);
let { name, config, retain } = cfg
name = name.trim()
if (!name) {
name = 'Home Assistant'
}
config = config ? JSON.parse(config) : {}
this.retain = retain
this.device_info = {
configuration_url: 'https://github.com/shaonianzhentan/node-red-contrib-ha-mqtt',
identifiers: `ha-mqtt-${name}`,
manufacturer: "shaonianzhentan",
model: 'HA-MQTT',
sw_version: HomeAssistant.version,
...config,
name
}
})
}
2 changes: 1 addition & 1 deletion device_automation/device_automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (RED) {
const subtype = cfg.name
cfg.name = `${subtype}${cfg.action}`
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
const { name, state_topic } = ha.config
node.on('input', function (msg) {
Expand Down
2 changes: 1 addition & 1 deletion device_tracker/device_tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function (RED) {
this.server.register(this)

const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
let { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion fan/fan.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes, preset_mode, percentage, oscillation } = msg
Expand Down
2 changes: 1 addition & 1 deletion humidifier/humidifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes, mode, target_humidity } = msg
Expand Down
2 changes: 1 addition & 1 deletion image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion lawn_mower/lawn_mower.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)

const { state_topic, pause_command_topic, dock_command_topic, start_mowing_command_topic } = ha.config

Expand Down
2 changes: 1 addition & 1 deletion light/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
const { command_topic, effect_state_topic, effect_command_topic, brightness_state_topic, brightness_command_topic } = ha.config
node.on('input', function (msg) {
Expand Down
3 changes: 2 additions & 1 deletion locales/en-US/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"mqttServer": "MQTT Server",
"entityName": "Entity Name",
"deviceName": "Device Name",
"config": "Configuration"
"config": "Configuration",
"retainMessage": "Retain data for immediate updates"
},
"ioLabels": {
"state": "State",
Expand Down
3 changes: 2 additions & 1 deletion locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"mqttServer": "MQTT",
"entityName": "实体名称",
"deviceName": "设备名称",
"config": "配置"
"config": "配置",
"retainMessage": "保留数据即时更新"
},
"ioLabels": {
"state": "状态",
Expand Down
2 changes: 1 addition & 1 deletion lock/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion number/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-ha-mqtt",
"version": "1.2.12",
"version": "1.2.13",
"description": "Generate MQTT entities in HomeAssistant",
"keywords": [
"node-red",
Expand Down
2 changes: 1 addition & 1 deletion scene/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
const { command_topic } = ha.config

Expand Down
2 changes: 1 addition & 1 deletion select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion sensor/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion siren/siren.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion switch/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion tag/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
try {
Expand Down
2 changes: 1 addition & 1 deletion text/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
2 changes: 1 addition & 1 deletion update/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const ha = new HomeAssistant(this, cfg, deviceNode)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
Expand Down
Loading

0 comments on commit f94f914

Please sign in to comment.