This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (127 loc) · 4.18 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import "dotenv/config";
import mqtt from "mqtt";
import fetch from "node-fetch";
const CBYGE_URL = process.env.CBYGE_URL;
const mqtt_client = mqtt.connect(process.env.MQTT_URL, {
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
});
function publish(topic, message) {
mqtt_client.publish(topic, message, {
qos: 1,
retain: true,
});
}
async function set_on(id, payload) {
const path = `/api/device/set_on?id=${id}&on=${payload.state === "ON" ? "1" : "0"}`;
// Optimistically set the result through MQTT
publish(`homeassistant/gecync/state/${id}`, JSON.stringify(payload));
const result = await fetch(CBYGE_URL + path);
const status = await result.json();
console.log("status", status);
if (status.error) {
// TODO: Rollback the state?
// Sometimes this times out, however, it does
// seem to effect the state of the lights most
// of the time.
}
}
async function set_brightness(id, brightness) {
const path = `/api/device/set_brightness?id=${id}&brightness=${brightness}`;
// Optimistically set the result through MQTT
publish(`homeassistant/gecync/brightness/${id}`, JSON.stringify(brightness));
const result = await fetch(CBYGE_URL + path);
const status = await result.json();
console.log("status", status);
if (status.error) {
// TODO: Rollback the state?
// Sometimes this times out, however, it does
// seem to effect the state of the lights most
// of the time.
}
}
async function configure() {
// Get list of lights
console.log("fetching devices...");
const result = await fetch(CBYGE_URL + "/api/devices?update_status=1");
const devices = await result.json();
// console.log(devices);
// Subscribe to status changes from home assistant
mqtt_client.subscribe("homeassistant/gecync/#");
mqtt_client.subscribe("homeassistant/status");
// Send config to home assistant through mqtt
devices.forEach(function (light) {
const topic = "homeassistant/light/gecync/" + light.id + "/config";
const payload = {
name: light.name,
unique_id: light.id,
schema: "json",
state_topic: "homeassistant/gecync/state/" + light.id,
command_topic: "homeassistant/gecync/set-state/" + light.id,
brightness_state_topic: "homeassistant/gecync/brightness/" + light.id,
brightness_command_topic:
"homeassistant/gecync/set-brightness/" + light.id,
color_temp_state_topic: "homeassistant/gecync/color-temp/" + light.id,
color_temp_command_topic: "homeassistant/gecync/set-color-temp/" + light.id,
brightness: true,
brightness_scale: 100,
color_mode: true,
supported_color_modes: ["brightness"],
device: {
identifiers: [light.id, light.name],
name: light.name,
model: "GE Cync Direct Connect Light Bulb",
manufacturer: "GE",
sw_version: "4.XX",
},
};
publish(topic, JSON.stringify(payload));
// Set the status through MQTT
publish(
`homeassistant/gecync/state/${light.id}`,
JSON.stringify({ state: light.status.is_on ? "ON" : "OFF" })
);
});
}
/* subscribe to some topics */
mqtt_client.on("connect", function () {
console.log("connected");
configure();
});
mqtt_client.on("reconnect", function () {
console.log("reconnected");
configure();
});
function retryConnection() {
mqtt_client.reconnect();
}
mqtt_client.on("close", function () {
console.log("connection lost");
// wait a while then reconnect
setTimeout(retryConnection, 15000);
});
mqtt_client.on("disconnect", function () {
console.log("disconnected");
// wait a while then reconnect
setTimeout(retryConnection, 15000);
});
/* Forward MQTT packets to REST */
mqtt_client.on("message", function (topic, message) {
console.log("message:", topic.toString(), message.toString());
const [, scope, command, id] = topic.toString().split("/");
const payload = JSON.parse(message.toString());
if (scope === "gecync") {
switch (command) {
case "set-state":
set_on(id, payload);
break;
case "set-brightness":
set_brightness(id, payload);
break;
}
} else if (scope === "status") {
if (payload === "online") {
configure();
}
}
});