-
Notifications
You must be signed in to change notification settings - Fork 59
/
mqtt-announce-control.js
93 lines (80 loc) · 2.87 KB
/
mqtt-announce-control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2021 Allterco Robotics EOOD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Shelly is a Trademark of Allterco Robotics
// Shelly Script example: Use MQTT in scripting to provide backwards compatibility
// with Gen1 MQTT topics shellies/announce, shellies/command, <device-id>/command,
// /command/switch:0/output
// Publish device status, input and switch status
const deviceInfo = Shelly.getDeviceInfo();
const wifiStatus = Shelly.getComponentStatus("Wifi"); // Get wifi status
const mqttConfig = Shelly.getComponentConfig("MQTT"); // Get mqtt config
const CONFIG = {
device_id: deviceInfo.id,
device_mac: deviceInfo.mac,
device_model: deviceInfo.model,
fw_ver: deviceInfo.fw_id,
topic_prefix: mqttConfig.topic_prefix,
wifi_ip: wifiStatus.sta_ip,
};
function isConfigReady() {
for (let key in CONFIG) {
if (CONFIG[key] === "") return false;
}
return true;
}
//Monitor ip changes
Shelly.addStatusHandler(function (status) {
if (status.component === "wifi" && status.delta.status === "got ip") {
CONFIG.wifi_ip = status.delta.sta_ip;
}
});
//Subscribe and announce changes
function announce() {
MQTT.publish(CONFIG.topic_prefix + "/status", JSON.stringify(CONFIG));
const inputStatus = Shelly.getComponentStatus("Input:0");
const switchStatus = Shelly.getComponentStatus("Switch:0");
MQTT.publish(CONFIG.topic_prefix + "/input:0", JSON.stringify(inputStatus));
MQTT.publish(CONFIG.topic_prefix + "/switch:0", JSON.stringify(switchStatus));
}
function announceHandler(topic, message) {
if (message !== "announce") return;
announce();
}
function switchControlHandler(topic, message) {
if (message !== "on" && message !== "off") return;
Shelly.call("Switch.Set", { id: 0, on: message === "on" });
}
function subscribeToTopics() {
MQTT.subscribe("shellies/command", announceHandler);
MQTT.subscribe(CONFIG.topic_prefix + "/command", announceHandler);
MQTT.subscribe(
CONFIG.topic_prefix + "/command/switch:0/output",
switchControlHandler
);
}
//Start a timer that checks if all fields in CONFIG are populated
let configReadyTimer;
function connectToMQTT() {
configReadyTimer = Timer.set(1000, true, function () {
if (!isConfigReady()) return;
if (!MQTT.isConnected()) return;
subscribeToTopics();
Timer.clear(configReadyTimer);
});
}
connectToMQTT();
MQTT.setDisconnectHandler(function () {
connectToMQTT();
});