diff --git a/HomeAssistant.js b/HomeAssistant.js index 27e2e8d..96c52cb 100644 --- a/HomeAssistant.js +++ b/HomeAssistant.js @@ -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`, diff --git a/README.md b/README.md index ce8c5da..e4156a7 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/README.zh.md b/README.zh.md index 21fd4a2..8f316e5 100644 --- a/README.zh.md +++ b/README.zh.md @@ -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/) diff --git a/lawn_mower/lawn_mower.html b/lawn_mower/lawn_mower.html new file mode 100644 index 0000000..281e19b --- /dev/null +++ b/lawn_mower/lawn_mower.html @@ -0,0 +1,48 @@ + + \ No newline at end of file diff --git a/lawn_mower/lawn_mower.js b/lawn_mower/lawn_mower.js new file mode 100644 index 0000000..346e278 --- /dev/null +++ b/lawn_mower/lawn_mower.js @@ -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` }); + } + }) +} \ No newline at end of file diff --git a/lawn_mower/locales/en-US/lawn_mower.html b/lawn_mower/locales/en-US/lawn_mower.html new file mode 100644 index 0000000..950242a --- /dev/null +++ b/lawn_mower/locales/en-US/lawn_mower.html @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/lawn_mower/locales/zh/lawn_mower.html b/lawn_mower/locales/zh/lawn_mower.html new file mode 100644 index 0000000..ff6e611 --- /dev/null +++ b/lawn_mower/locales/zh/lawn_mower.html @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/locales/en-US/common.json b/locales/en-US/common.json index 2846d36..2c001d0 100644 --- a/locales/en-US/common.json +++ b/locales/en-US/common.json @@ -8,6 +8,9 @@ }, "ioLabels": { "state": "State", + "start": "Start", + "pause": "Pause", + "dock": "Dock", "temperature": "Temperature", "mode": "Mode", "tilt": "Tilt", diff --git a/locales/zh/common.json b/locales/zh/common.json index 3e6c585..346c1f0 100644 --- a/locales/zh/common.json +++ b/locales/zh/common.json @@ -8,6 +8,9 @@ }, "ioLabels": { "state": "状态", + "start": "开始", + "pause": "暂停", + "dock": "停靠", "temperature": "温度", "mode": "模式", "tilt": "倾斜", diff --git a/package.json b/package.json index 6f14910..7938b56 100644 --- a/package.json +++ b/package.json @@ -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",