-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from shaonianzhentan/dev
Dev
- Loading branch information
Showing
10 changed files
with
226 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` }); | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` }); | ||
} | ||
}) | ||
} |