mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-10-28 19:01:58 +08:00
Release 5.0.0
This commit is contained in:
17
lib/mqtt.js
17
lib/mqtt.js
@@ -16,7 +16,7 @@ class Mqtt {
|
||||
debug('Starting connection to MQTT broker...')
|
||||
this.client = await this.connect()
|
||||
if (this.client.connected) {
|
||||
this.connected = true
|
||||
this.updateMqttState(true)
|
||||
debug('MQTT connection established, sending config/state information in 5 seconds.')
|
||||
}
|
||||
// Monitor configured/default Home Assistant status topic
|
||||
@@ -51,10 +51,10 @@ class Mqtt {
|
||||
// On MQTT connect/reconnect send config/state information after delay
|
||||
this.client.on('connect', async function () {
|
||||
if (!mqtt.connected) {
|
||||
mqtt.connected = true
|
||||
mqtt.updateMqttState(true)
|
||||
debug('MQTT connection established, processing locations...')
|
||||
}
|
||||
mqtt.ring.processLocations(mqtt)
|
||||
mqtt.ring.processLocations(mqtt.client)
|
||||
})
|
||||
|
||||
this.client.on('reconnect', function () {
|
||||
@@ -63,12 +63,12 @@ class Mqtt {
|
||||
} else {
|
||||
debug('Attempting to reconnect to MQTT broker...')
|
||||
}
|
||||
mqtt.connected = false
|
||||
mqtt.updateMqttState(false)
|
||||
})
|
||||
|
||||
this.client.on('error', function (error) {
|
||||
debug('Unable to connect to MQTT broker.', error.message)
|
||||
mqtt.connected = false
|
||||
mqtt.updateMqttState(false)
|
||||
})
|
||||
|
||||
// Process MQTT messages from subscribed command topics
|
||||
@@ -83,12 +83,17 @@ class Mqtt {
|
||||
if (topic === this.config.hass_topic || topic === 'hass/status' || topic === 'hassio/status') {
|
||||
debug('Home Assistant state topic '+topic+' received message: '+message)
|
||||
if (message === 'online') {
|
||||
this.ring.republishDevices()
|
||||
this.ring.republishDevices(this.client)
|
||||
}
|
||||
} else {
|
||||
this.ring.processDeviceCommand(topic, message)
|
||||
}
|
||||
}
|
||||
|
||||
updateMqttState(state) {
|
||||
this.connected = state
|
||||
this.ring.updateMqttState(state)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new Mqtt()
|
||||
Reference in New Issue
Block a user