forked from TheAgentK/tuya-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuya-mqtt.js
221 lines (195 loc) · 7.38 KB
/
tuya-mqtt.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env node
const fs = require('fs')
const mqtt = require('mqtt')
const json5 = require('json5')
const debug = require('debug')('tuya-mqtt:info')
const debugCommand = require('debug')('tuya-mqtt:command')
const debugError = require('debug')('tuya-mqtt:error')
const SimpleSwitch = require('./devices/simple-switch')
const SimpleDimmer = require('./devices/simple-dimmer')
const RGBTWLight = require('./devices/rgbtw-light')
const GenericDevice = require('./devices/generic-device')
const utils = require('./lib/utils')
var CONFIG = undefined
var tuyaDevices = new Array()
var mqttClient = undefined
var will_topic = undefined
// Setup Exit Handlers
process.on('exit', processExit.bind(null, {}))
process.on('SIGINT', processExit.bind(null, {exitCode: 0}))
process.on('SIGTERM', processExit.bind(null, {exitCode: 0}))
process.on('uncaughtException', processExit.bind(null, {exitCode: 1}))
// Disconnect from and publish offline status for all devices on exit
async function processExit(options, exitReason) {
for (let tuyaDevice of tuyaDevices) {
tuyaDevice.device.disconnect()
}
const exitCode = (typeof options?.exitCode === 'number') ? options.exitCode : (typeof exitReason === 'number'? exitReason : 2)
function printError(...args) {
if (debugError.enabled) { debugError.apply(this, args); }
else { console.error.apply(this, args) }
}
const printer = (exitCode === 0)? debug : printError;
await utils.sleep(1)
printer('Exiting due to: ', exitReason)
printer('Exit code: ', exitCode)
process.removeAllListeners('exit') //Deregister self to avoid loop
process.exit(exitCode)
}
// Get new deivce based on configured type
function getDevice(configDevice, mqttClient) {
const deviceInfo = {
configDevice: configDevice,
mqttClient: mqttClient,
topic: CONFIG.topic,
qos: CONFIG.qos,
retain_status_topic: CONFIG.retain_status_topic,
publish_homeassistant_discovery: CONFIG.publish_homeassistant_discovery
}
switch (configDevice.type) {
case 'SimpleSwitch':
return new SimpleSwitch(deviceInfo)
case 'SimpleDimmer':
return new SimpleDimmer(deviceInfo)
case 'RGBTWLight':
return new RGBTWLight(deviceInfo)
}
return new GenericDevice(deviceInfo)
}
function initDevices(configDevices, mqttClient) {
for (let configDevice of configDevices) {
const newDevice = getDevice(configDevice, mqttClient)
tuyaDevices.push(newDevice)
}
}
// Republish devices 2x with 30 seconds sleep if restart of HA is detected
async function republishDevices() {
for (let i = 0; i < 2; i++) {
debug('Resending device config/state in 30 seconds')
await utils.sleep(30)
for (let device of tuyaDevices) {
device.republish()
}
await utils.sleep(2)
}
}
// Main code function
const main = async() => {
let configDevices
try {
CONFIG = json5.parse(fs.readFileSync('./config.json', 'utf-8'))
} catch (e) {
console.error('Configuration file not found!')
debugError(e)
process.exit(1)
}
if (typeof CONFIG.qos === 'undefined') {
CONFIG.qos = 1
}
if (typeof CONFIG.retain_status_topic === 'undefined') {
CONFIG.retain_status_topic = false
}
if (typeof CONFIG.monitored_birth_topic === 'undefined') {
CONFIG.monitored_birth_topic = "homeassistant/status"
}
if (typeof CONFIG.publish_homeassistant_discovery === 'undefined') {
CONFIG.publish_homeassistant_discovery = true
}
try {
configDevices = fs.readFileSync('./devices.conf', 'utf8')
configDevices = json5.parse(configDevices)
} catch (e) {
console.error('Devices file not found!')
debugError(e)
process.exit(1)
}
if (!configDevices.length) {
console.error('No devices found in devices file!')
process.exit(1)
}
will_topic = CONFIG.topic + 'script_status'
let initialization_complete = false
mqttClient = mqtt.connect({
host: CONFIG.host,
port: CONFIG.port,
protocol: CONFIG.protocol,
rejectUnauthorized: CONFIG.rejectUnauthorized,
username: CONFIG.mqtt_user,
password: CONFIG.mqtt_pass,
will: {
topic: will_topic,
payload: 'offline', //TODO: add publishing this on clean disconnect as well
qos: CONFIG.qos,
retain: CONFIG.retain_status_topic
}
})
mqttClient.on('connect', function (/*connack*/) {
debug('Connection established to MQTT server')
initialization_complete = true
let topic = CONFIG.topic + '#'
mqttClient.subscribe(topic)
if( CONFIG.monitored_birth_topic ) {
mqttClient.subscribe(CONFIG.monitored_birth_topic)
}
mqttClient.publish(will_topic, 'online', { qos: CONFIG.qos, retain: CONFIG.retain_status_topic });
initDevices(configDevices, mqttClient)
})
mqttClient.on('reconnect', function () {
if (mqttClient.connected) {
debug('Connection to MQTT server lost. Attempting to reconnect...')
} else {
debug('Unable to connect to MQTT server')
}
})
mqttClient.on('error', function (error) {
debugError('Unable to connect to MQTT server', error)
if (!initialization_complete) {
processExit({exitCode:3}, 'Unable to connect to MQTT server:' + error.message)
}
})
mqttClient.on('message', function (topic, _message) {
try {
const message = _message.toString()
const splitTopic = topic.split('/')
const topicLength = splitTopic.length
const commandTopic = splitTopic[topicLength - 1]
const deviceTopicLevel = splitTopic[1]
if(CONFIG.monitored_birth_topic && topic === CONFIG.monitored_birth_topic) {
debug('Monitored birth state topic [' + topic + '] received message: ' + message)
if (message === 'online') {
republishDevices()
}
} else if (commandTopic.includes('command')) {
// If it looks like a valid command topic try to process it
debugCommand('Received MQTT message -> ', JSON.stringify({
topic: topic,
message: message
}))
// Use device topic level to find matching device
const device = tuyaDevices.find(d => d.options.name === deviceTopicLevel || d.options.id === deviceTopicLevel)
if (device === undefined) {
debugError("Received command for unrecognized device topic: " + deviceTopicLevel)
} else {
switch (topicLength) {
case 3:
device.processCommand(message, commandTopic)
break;
case 4:
device.processDpsCommand(message)
break;
case 5:
{
const dpsKey = splitTopic[topicLength-2]
device.processDpsKeyCommand(message, dpsKey)
}
break;
}
}
}
} catch (e) {
debugError(e)
}
})
}
// Call the main code
main()