Skip to content

Commit

Permalink
add Lawn Mower
Browse files Browse the repository at this point in the history
  • Loading branch information
shaonianzhentan committed Sep 11, 2023
1 parent 610ba04 commit d91fb12
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 2 deletions.
3 changes: 3 additions & 0 deletions HomeAssistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = class HomeAssistant {
set_position_topic: `${topic}position/set`,
availability_topic: `${topic}availability/state`,
power_command_topic: `${topic}power/set`,
pause_command_topic: `${topic}pause/set`,
dock_command_topic: `${topic}dock/set`,
start_mowing_command_topic: `${topic}start_mowing/set`,
effect_state_topic: `${topic}effect/state`,
effect_command_topic: `${topic}effect/set`,
brightness_state_topic: `${topic}brightness/state`,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Example:https://github.com/shaonianzhentan/node-red-contrib-ha-mqtt/wiki
- ✔️ [fan](https://www.home-assistant.io/integrations/fan.mqtt/)
- ✔️ [humidifier](https://www.home-assistant.io/integrations/humidifier.mqtt/)
- ✔️ [image](https://www.home-assistant.io/integrations/image.mqtt/)
- [lawn_mower](https://www.home-assistant.io/integrations/lawn_mower.mqtt/)
- ✔️ [lawn_mower](https://www.home-assistant.io/integrations/lawn_mower.mqtt/)
- ✔️ [light](https://www.home-assistant.io/integrations/light.mqtt/)
- ✔️ [lock](https://www.home-assistant.io/integrations/lock.mqtt/)
- ✔️ [number](https://www.home-assistant.io/integrations/number.mqtt/)
Expand Down
2 changes: 1 addition & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
- ✔️ [fan - 风扇](https://www.home-assistant.io/integrations/fan.mqtt/)
- ✔️ [humidifier - 加湿器](https://www.home-assistant.io/integrations/humidifier.mqtt/)
- ✔️ [image - 图像](https://www.home-assistant.io/integrations/image.mqtt/)
- [lawn_mower - 割草机](https://www.home-assistant.io/integrations/lawn_mower.mqtt/)
- ✔️ [lawn_mower - 割草机](https://www.home-assistant.io/integrations/lawn_mower.mqtt/)
- ✔️ [light - 灯](https://www.home-assistant.io/integrations/light.mqtt/)
- ✔️ [lock - 锁](https://www.home-assistant.io/integrations/lock.mqtt/)
- ✔️ [number - 数字](https://www.home-assistant.io/integrations/number.mqtt/)
Expand Down
48 changes: 48 additions & 0 deletions lawn_mower/lawn_mower.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script type="text/html" data-template-name="ha-mqtt-lawn_mower">
<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/lawn_mower.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-lawn_mower', {
category: RED._("node-red-contrib-ha-mqtt/common:homeAssistantCategory"),
color: '#C0DEED',
icon: "font-awesome/fa-scissors",
paletteLabel: 'Lawn Mower',
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.start"),
RED._("node-red-contrib-ha-mqtt/common:ioLabels.pause"),
RED._("node-red-contrib-ha-mqtt/common:ioLabels.dock")
],
label: function () {
return this.name || "lawn_mower";
},
oneditprepare: function () {
$("#node-input-config").typedInput({ type: "json", types: ["json"] })
}
});
</script>
63 changes: 63 additions & 0 deletions lawn_mower/lawn_mower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const HomeAssistant = require('../HomeAssistant')

module.exports = function (RED) {
RED.nodes.registerType('ha-mqtt-lawn_mower', 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 { state_topic, pause_command_topic, dock_command_topic, start_mowing_command_topic } = ha.config

const node = this
node.on('input', function (msg) {
const { payload, attributes } = msg
try {
if (payload) {
ha.publish(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: ex });
}
})

ha.subscribe(start_mowing_command_topic, (payload) => {
ha.send_payload(payload, 1, 3)

ha.publish(state_topic, 'mowing', RED._(`node-red-contrib-ha-mqtt/common:publish.state`))
})
ha.subscribe(pause_command_topic, (payload) => {
ha.send_payload(payload, 2, 3)

ha.publish(state_topic, 'paused', RED._(`node-red-contrib-ha-mqtt/common:publish.state`))
})
ha.subscribe(dock_command_topic, (payload) => {
ha.send_payload(payload, 3, 3)

ha.publish(state_topic, 'docked', RED._(`node-red-contrib-ha-mqtt/common:publish.state`))
})

try {
const discoveryConfig = {
state_topic: null,
activity_state_topic: state_topic,
pause_command_topic,
dock_command_topic,
start_mowing_command_topic
}
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` });
}
})
}
10 changes: 10 additions & 0 deletions lawn_mower/locales/en-US/lawn_mower.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script type="text/html" data-help-name="ha-mqtt-lawn_mower">
<p>Lawn Mower</p>
<h3>Input</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string</span></dt>
<dt>attributes <span class="property-type">object</span></dt>
</dl>
<h3>Details</h3>
<p>Generate a Lawn Mower entity in Home Assistant</p>
</script>
10 changes: 10 additions & 0 deletions lawn_mower/locales/zh/lawn_mower.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script type="text/html" data-help-name="ha-mqtt-lawn_mower">
<p>割草机</p>
<h3>输入</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string</span></dt>
<dt>attributes <span class="property-type">object</span></dt>
</dl>
<h3>详情</h3>
<p>在HomeAssistant中生成Lawn Mower实体</p>
</script>
3 changes: 3 additions & 0 deletions locales/en-US/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
},
"ioLabels": {
"state": "State",
"start": "Start",
"pause": "Pause",
"dock": "Dock",
"temperature": "Temperature",
"mode": "Mode",
"tilt": "Tilt",
Expand Down
3 changes: 3 additions & 0 deletions locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
},
"ioLabels": {
"state": "状态",
"start": "开始",
"pause": "暂停",
"dock": "停靠",
"temperature": "温度",
"mode": "模式",
"tilt": "倾斜",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"fan": "fan/fan.js",
"humidifier": "humidifier/humidifier.js",
"image": "image/image.js",
"lawn_mower": "lawn_mower/lawn_mower.js",
"light": "light/light.js",
"lock": "lock/lock.js",
"number": "number/number.js",
Expand Down

0 comments on commit d91fb12

Please sign in to comment.