Skip to content

Commit

Permalink
Merge pull request #33 from shaonianzhentan/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
shaonianzhentan authored Sep 12, 2023
2 parents b28bafb + 5eedf36 commit c29b70f
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 38 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Example:https://github.com/shaonianzhentan/node-red-contrib-ha-mqtt/wiki
- ✔️ [tag](https://www.home-assistant.io/integrations/tag.mqtt/)
- ✔️ [text](https://www.home-assistant.io/integrations/text.mqtt/)
- ✔️ [vacuum](https://www.home-assistant.io/integrations/vacuum.mqtt/)
- [water_heater](https://www.home-assistant.io/integrations/water_heater.mqtt/)
- ✔️ [water_heater](https://www.home-assistant.io/integrations/water_heater.mqtt/)

Auto-discovery
```yaml
Expand All @@ -55,4 +55,9 @@ payload: online
## If this project is helpful to you, please donate a cup of <del style="font-size: 14px;">coffee</del> milk tea 😘
<a href="https://paypal.me/shaonianzhentan"><img src="https://raw.githubusercontent.com/shaonianzhentan/image/main/picture/paypal.me.png" height="300" alt="https://paypal.me/shaonianzhentan" title="https://paypal.me/shaonianzhentan"></a>
<a href="https://paypal.me/shaonianzhentan"><img src="https://raw.githubusercontent.com/shaonianzhentan/image/main/picture/paypal.me.png" height="300" alt="https://paypal.me/shaonianzhentan" title="https://paypal.me/shaonianzhentan"></a>
## Reference Projects
- https://github.com/toobug/pinyin
- https://github.com/hujiulong/gcoord
7 changes: 6 additions & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- ✔️ [tag - 标签](https://www.home-assistant.io/integrations/tag.mqtt/)
- ✔️ [text - 文本](https://www.home-assistant.io/integrations/text.mqtt/)
- ✔️ [vacuum - 扫地机器人](https://www.home-assistant.io/integrations/vacuum.mqtt/)
- [water_heater - 热水器](https://www.home-assistant.io/integrations/water_heater.mqtt/)
- ✔️ [water_heater - 热水器](https://www.home-assistant.io/integrations/water_heater.mqtt/)


自动发现
Expand All @@ -59,3 +59,8 @@ payload: online
## 关注我的微信订阅号,了解更多HomeAssistant相关知识
<img src="https://cdn.jsdelivr.net/gh/shaonianzhentan/ha-docs@master/docs/img/wechat-channel.png" height="160" alt="HomeAssistant家庭助理" title="HomeAssistant家庭助理">
## 引用项目
- https://github.com/toobug/pinyin
- https://github.com/hujiulong/gcoord
84 changes: 52 additions & 32 deletions device_tracker/device_tracker.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
const HomeAssistant = require('../HomeAssistant')
const gcoord = require('gcoord')

module.exports = function (RED) {
RED.nodes.registerType('ha-mqtt-device_tracker', function (cfg) {
RED.nodes.createNode(this, cfg);
this.server = RED.nodes.getNode(cfg.server);
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
try {
if (payload) {
ha.publish(ha.config.state_topic, payload, RED._(`node-red-contrib-ha-mqtt/common:publish.state`))
}
if (attributes) {
ha.publish(ha.config.json_attr_t, attributes, RED._(`node-red-contrib-ha-mqtt/common:publish.attributes`))
}
} catch (ex) {
node.status({ fill: "red", shape: "ring", text: JSON.stringify(ex) });
}
})

try {
const discoveryConfig = {}
ha.discovery(discoveryConfig, () => {
this.status({ fill: "green", shape: "ring", text: `node-red-contrib-ha-mqtt/common:publish.config` });
})
} catch (ex) {
this.status({ fill: "red", shape: "ring", text: `${ex}` });
RED.nodes.registerType('ha-mqtt-device_tracker', function (cfg) {
RED.nodes.createNode(this, cfg);
this.server = RED.nodes.getNode(cfg.server);
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
try {
if (payload) {
ha.publish(ha.config.state_topic, payload, RED._(`node-red-contrib-ha-mqtt/common:publish.state`))
}
if (attributes) {
// Converting China Map Coordinate System to GPS
if (attributes.gcoord) {
const { longitude, latitude } = attributes
let result = null
switch (attributes.gcoord) {
case 'GCJ02':
result = gcoord.transform([longitude, latitude], gcoord.GCJ02, gcoord.WGS84);
break;
case 'BD09':
result = gcoord.transform([longitude, latitude], gcoord.BD09, gcoord.WGS84);
break;
}
if (result) {
attributes.gcoord_lng = longitude
attributes.gcoord_lat = latitude
attributes.longitude = result[0]
attributes.latitude = result[1]
}
}
} else {
this.status({ fill: "red", shape: "ring", text: `node-red-contrib-ha-mqtt/common:errors.mqttNotConfigured` });
ha.publish(ha.config.json_attr_t, attributes, RED._(`node-red-contrib-ha-mqtt/common:publish.attributes`))
}
} catch (ex) {
node.status({ fill: "red", shape: "ring", text: JSON.stringify(ex) });
}
})
})

try {
const discoveryConfig = {}
ha.discovery(discoveryConfig, () => {
this.status({ fill: "green", shape: "ring", text: `node-red-contrib-ha-mqtt/common:publish.config` });
})
} catch (ex) {
this.status({ fill: "red", shape: "ring", text: `${ex}` });
}
} else {
this.status({ fill: "red", shape: "ring", text: `node-red-contrib-ha-mqtt/common:errors.mqttNotConfigured` });
}
})
}
1 change: 1 addition & 0 deletions locales/en-US/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start": "Start",
"pause": "Pause",
"dock": "Dock",
"power": "Power",
"temperature": "Temperature",
"mode": "Mode",
"tilt": "Tilt",
Expand Down
1 change: 1 addition & 0 deletions locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start": "开始",
"pause": "暂停",
"dock": "停靠",
"power": "电源",
"temperature": "温度",
"mode": "模式",
"tilt": "倾斜",
Expand Down
8 changes: 5 additions & 3 deletions 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.11",
"version": "1.2.12",
"description": "Generate MQTT entities in HomeAssistant",
"keywords": [
"node-red",
Expand Down Expand Up @@ -38,7 +38,8 @@
"tag": "tag/tag.js",
"text": "text/text.js",
"update": "update/update.js",
"vacuum": "vacuum/vacuum.js"
"vacuum": "vacuum/vacuum.js",
"water_heater": "water_heater/water_heater.js"
}
},
"engines": {
Expand All @@ -58,6 +59,7 @@
},
"homepage": "https://github.com/shaonianzhentan/node-red-contrib-ha-mqtt#readme",
"dependencies": {
"gcoord": "^1.0.5",
"node-pinyin": "^0.2.3"
}
}
}
18 changes: 18 additions & 0 deletions water_heater/locales/en-US/water_heater.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script type="text/html" data-help-name="ha-mqtt-water_heater">
<p>Water Heater</p>
<h3>Input</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">number</span></dt>
<dt>attributes <span class="property-type">object</span></dt>
<dt>mode <span class="property-type">string</span></dt>
<dt>temperature <span class="property-type">number</span></dt>
</dl>
<h3>Details</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">Current Temperature</span></dt>
<dt>attributes <span class="property-type">Attributes</span></dt>
<dt>mode <span class="property-type">Mode</span></dt>
<dt>temperature <span class="property-type">Temperature</span></dt>
</dl>
<p>Generate a water heater entity in Home Assistant</p>
</script>
18 changes: 18 additions & 0 deletions water_heater/locales/zh/water_heater.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script type="text/html" data-help-name="ha-mqtt-water_heater">
<p>热水器</p>
<h3>输入</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">number</span></dt>
<dt>attributes <span class="property-type">object</span></dt>
<dt>mode <span class="property-type">string</span></dt>
<dt>temperature <span class="property-type">number</span></dt>
</dl>
<h3>详情</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">当前温度</span></dt>
<dt>attributes <span class="property-type">属性</span></dt>
<dt>mode <span class="property-type">运行模式</span></dt>
<dt>temperature <span class="property-type">热水器温度</span></dt>
</dl>
<p>在HomeAssistant中生成water_heater实体</p>
</script>
48 changes: 48 additions & 0 deletions water_heater/water_heater.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script type="text/html" data-template-name="ha-mqtt-water_heater">
<div class="form-row">
<label for="node-input-server" data-i18n="node-red-contrib-ha-mqtt/common:label.mqttServer"></label>
<select id="node-input-server"></select>
</div>
<div class="form-row">
<label for="node-input-device" data-i18n="node-red-contrib-ha-mqtt/common:label.deviceName"></label>
<select type="text" id="node-input-device" data-i18n="[placeholder]node-red-contrib-ha-mqtt/common:label.deviceName"></select>
</div>
<div class="form-row">
<label for="node-input-name" data-i18n="node-red-contrib-ha-mqtt/common:label.entityName"></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red-contrib-ha-mqtt/common:label.entityName">
</div>
<div class="form-row">
<a href="https://www.home-assistant.io/integrations/water_heater.mqtt/" target="_blank" style="color:blue;">
<label for="node-input-config" data-i18n="node-red-contrib-ha-mqtt/common:label.config"></label>
</a>
<input type="text" id="node-input-config" data-i18n="[placeholder]node-red-contrib-ha-mqtt/common:label.config">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('ha-mqtt-water_heater', {
category: RED._("node-red-contrib-ha-mqtt/common:homeAssistantCategory"),
color: '#C0DEED',
icon: "font-awesome/fa-s15",
paletteLabel: 'Water Heater',
defaults: {
server: { value: "", type: "mqtt-broker", required: true },
device: { value: "", type: "ha-mqtt-device" , required: true},
name: { value: "", required: true },
config: { value: "" }
},
inputs: 1,
outputs: 3,
inputLabels: RED._("node-red-contrib-ha-mqtt/common:ioLabels.state"),
outputLabels: [
RED._("node-red-contrib-ha-mqtt/common:ioLabels.temperature"),
RED._("node-red-contrib-ha-mqtt/common:ioLabels.mode"),
RED._("node-red-contrib-ha-mqtt/common:ioLabels.power")
],
label: function () {
return this.name || "water_heater";
},
oneditprepare: function () {
$("#node-input-config").typedInput({ type: "json", types: ["json"] })
}
});
</script>
70 changes: 70 additions & 0 deletions water_heater/water_heater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const HomeAssistant = require('../HomeAssistant')

module.exports = function (RED) {
RED.nodes.registerType('ha-mqtt-water_heater', function (cfg) {
RED.nodes.createNode(this, cfg);
this.server = RED.nodes.getNode(cfg.server);
if (this.server) {
this.server.register(this)
const deviceNode = RED.nodes.getNode(cfg.device);
const ha = new HomeAssistant(this, cfg, deviceNode.device_info)
const node = this

const {
json_attr_t, mode_state_topic, mode_command_topic, power_command_topic,
temperature_command_topic, temperature_state_topic,
current_temperature_topic
} = ha.config

node.on('input', function (msg) {
const { payload, attributes, mode, temperature } = msg
try {
if (payload) {
ha.publish(current_temperature_topic, payload, RED._(`node-red-contrib-ha-mqtt/common:publish.current_temperature`))
}
if (attributes) {
ha.publish(json_attr_t, attributes, RED._(`node-red-contrib-ha-mqtt/common:publish.attributes`))
}
if (mode) {
ha.publish(mode_state_topic, mode, RED._(`node-red-contrib-ha-mqtt/common:publish.mode`))
}
if (temperature) {
ha.publish(temperature_state_topic, temperature, RED._(`node-red-contrib-ha-mqtt/common:publish.temperature`))
}
} catch (ex) {
node.status({ fill: "red", shape: "ring", text: ex });
}
})
ha.subscribe(temperature_command_topic, (payload) => {
ha.send_payload(payload, 1, 3)
ha.publish(temperature_state_topic, payload, RED._(`node-red-contrib-ha-mqtt/common:publish.temperature`))
})
ha.subscribe(mode_command_topic, (payload) => {
ha.send_payload(payload, 2, 3)
ha.publish(mode_state_topic, payload, RED._(`node-red-contrib-ha-mqtt/common:publish.mode`))
})
ha.subscribe(power_command_topic, (payload) => {
ha.send_payload(payload, 3, 3)
})
try {
ha.discovery({
state_topic: null,
power_command_topic,

mode_state_topic,
mode_command_topic,
temperature_state_topic,
temperature_command_topic,

current_temperature_topic
}, () => {
this.status({ fill: "green", shape: "ring", text: `node-red-contrib-ha-mqtt/common:publish.config` });
})
} catch (ex) {
this.status({ fill: "red", shape: "ring", text: `${ex}` });
}
} else {
this.status({ fill: "red", shape: "ring", text: `node-red-contrib-ha-mqtt/common:errors.mqttNotConfigured` });
}
})
}

0 comments on commit c29b70f

Please sign in to comment.