-
Notifications
You must be signed in to change notification settings - Fork 80
/
tuya-mqtt.js
177 lines (158 loc) · 5.45 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
#!/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()
// Setup Exit Handlers
process.on('exit', processExit.bind(0))
process.on('SIGINT', processExit.bind(0))
process.on('SIGTERM', processExit.bind(0))
process.on('uncaughtException', processExit.bind(1))
// Disconnect from and publish offline status for all devices on exit
async function processExit(exitCode) {
for (let tuyaDevice of tuyaDevices) {
tuyaDevice.device.disconnect()
}
if (exitCode || exitCode === 0) debug('Exit code: '+exitCode)
await utils.sleep(1)
process.exit()
}
// Get new deivce based on configured type
function getDevice(configDevice, mqttClient) {
const deviceInfo = {
configDevice: configDevice,
mqttClient: mqttClient,
topic: CONFIG.topic
}
switch (configDevice.type) {
case 'SimpleSwitch':
return new SimpleSwitch(deviceInfo)
break;
case 'SimpleDimmer':
return new SimpleDimmer(deviceInfo)
break;
case 'RGBTWLight':
return new RGBTWLight(deviceInfo)
break;
}
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
let mqttClient
try {
CONFIG = require('./config')
} 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 == 'undefined') {
CONFIG.retain = false
}
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)
}
mqttClient = mqtt.connect({
host: CONFIG.host,
port: CONFIG.port,
username: CONFIG.mqtt_user,
password: CONFIG.mqtt_pass,
})
mqttClient.on('connect', function (err) {
debug('Connection established to MQTT server')
let topic = CONFIG.topic + '#'
mqttClient.subscribe(topic)
mqttClient.subscribe('homeassistant/status')
mqttClient.subscribe('hass/status')
initDevices(configDevices, mqttClient)
})
mqttClient.on('reconnect', function (error) {
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) {
debug('Unable to connect to MQTT server', error)
})
mqttClient.on('message', function (topic, message) {
try {
message = message.toString()
const splitTopic = topic.split('/')
const topicLength = splitTopic.length
const commandTopic = splitTopic[topicLength - 1]
const deviceTopicLevel = splitTopic[1]
if (topic === 'homeassistant/status' || topic === 'hass/status' ) {
debug('Home Assistant 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)
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()