mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 08:13:23 +00:00
Update to 3.0.2 (#63)
* Update 3.0.2 * Fix (hopefully) uninitialized key values for devices which block get requests for some DPS values based on operating mode (seen with at least one RGBTW light that will not return color DPS key while in white mode). * Modify HSB/HSBHEX guessing in RGBTW light to attempt to deal with issue above * Implement automatic reconnect when device disconnects socket on it's side * Update package dependencies to latest versions
This commit is contained in:
@@ -44,6 +44,7 @@ class TuyaDevice {
|
||||
|
||||
// Missed heartbeat monitor
|
||||
this.heartbeatsMissed = 0
|
||||
this.reconnecting = false
|
||||
|
||||
// Build the MQTT topic for this device (friendly name or device id)
|
||||
if (this.options.name) {
|
||||
@@ -85,19 +86,19 @@ class TuyaDevice {
|
||||
})
|
||||
|
||||
// On disconnect perform device specific disconnect
|
||||
this.device.on('disconnected', () => {
|
||||
this.device.on('disconnected', async () => {
|
||||
this.connected = false
|
||||
this.publishMqtt(this.baseTopic+'status', 'offline')
|
||||
debug('Disconnected from device ' + this.toString())
|
||||
await utils.sleep(5)
|
||||
this.reconnect()
|
||||
})
|
||||
|
||||
// On connect error call reconnect
|
||||
this.device.on('error', async (err) => {
|
||||
debugError(err)
|
||||
await utils.sleep(1)
|
||||
if (!this.device.isConnected()) {
|
||||
this.reconnect()
|
||||
}
|
||||
this.reconnect()
|
||||
})
|
||||
|
||||
// On heartbeat reset heartbeat timer
|
||||
@@ -112,9 +113,9 @@ class TuyaDevice {
|
||||
this.connected = false
|
||||
for (let topic in this.deviceTopics) {
|
||||
const key = this.deviceTopics[topic].key
|
||||
if (!this.dps[key]) { this.dps[key] = {} }
|
||||
try {
|
||||
const result = await this.device.get({"dps": key})
|
||||
this.dps[key].val = result
|
||||
this.dps[key].val = await this.device.get({"dps": key})
|
||||
this.dps[key].updated = true
|
||||
} catch {
|
||||
debugError('Could not get value for device DPS key '+key)
|
||||
@@ -596,10 +597,22 @@ class TuyaDevice {
|
||||
|
||||
// Retry connection every 10 seconds if unable to connect
|
||||
async reconnect() {
|
||||
debugError('Error connecting to device id '+this.options.id+'...retry in 10 seconds.')
|
||||
await utils.sleep(10)
|
||||
if (this.connected) { return }
|
||||
this.connectDevice()
|
||||
if (!this.device.isConnected() && !this.reconnecting) {
|
||||
this.reconnecting = true
|
||||
debugError('Error connecting to device id '+this.options.id+'...retry in 10 seconds.')
|
||||
await utils.sleep(10)
|
||||
if (this.connected) { return }
|
||||
this.connectDevice()
|
||||
this.reconnecting = false
|
||||
}
|
||||
}
|
||||
|
||||
// Republish device discovery/state data (used for Home Assistant state topic)
|
||||
async republish() {
|
||||
const status = (this.device.isConnected()) ? 'online' : 'offline'
|
||||
this.publishMqtt(this.baseTopic+'status', status)
|
||||
await utils.sleep(1)
|
||||
this.init()
|
||||
}
|
||||
|
||||
// Republish device discovery/state data (used for Home Assistant state topic)
|
||||
|
||||
Reference in New Issue
Block a user