mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-10-29 11:12:26 +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...')
|
debug('Starting connection to MQTT broker...')
|
||||||
this.client = await this.connect()
|
this.client = await this.connect()
|
||||||
if (this.client.connected) {
|
if (this.client.connected) {
|
||||||
this.connected = true
|
this.updateMqttState(true)
|
||||||
debug('MQTT connection established, sending config/state information in 5 seconds.')
|
debug('MQTT connection established, sending config/state information in 5 seconds.')
|
||||||
}
|
}
|
||||||
// Monitor configured/default Home Assistant status topic
|
// Monitor configured/default Home Assistant status topic
|
||||||
@@ -51,10 +51,10 @@ class Mqtt {
|
|||||||
// On MQTT connect/reconnect send config/state information after delay
|
// On MQTT connect/reconnect send config/state information after delay
|
||||||
this.client.on('connect', async function () {
|
this.client.on('connect', async function () {
|
||||||
if (!mqtt.connected) {
|
if (!mqtt.connected) {
|
||||||
mqtt.connected = true
|
mqtt.updateMqttState(true)
|
||||||
debug('MQTT connection established, processing locations...')
|
debug('MQTT connection established, processing locations...')
|
||||||
}
|
}
|
||||||
mqtt.ring.processLocations(mqtt)
|
mqtt.ring.processLocations(mqtt.client)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.client.on('reconnect', function () {
|
this.client.on('reconnect', function () {
|
||||||
@@ -63,12 +63,12 @@ class Mqtt {
|
|||||||
} else {
|
} else {
|
||||||
debug('Attempting to reconnect to MQTT broker...')
|
debug('Attempting to reconnect to MQTT broker...')
|
||||||
}
|
}
|
||||||
mqtt.connected = false
|
mqtt.updateMqttState(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.client.on('error', function (error) {
|
this.client.on('error', function (error) {
|
||||||
debug('Unable to connect to MQTT broker.', error.message)
|
debug('Unable to connect to MQTT broker.', error.message)
|
||||||
mqtt.connected = false
|
mqtt.updateMqttState(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Process MQTT messages from subscribed command topics
|
// 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') {
|
if (topic === this.config.hass_topic || topic === 'hass/status' || topic === 'hassio/status') {
|
||||||
debug('Home Assistant state topic '+topic+' received message: '+message)
|
debug('Home Assistant state topic '+topic+' received message: '+message)
|
||||||
if (message === 'online') {
|
if (message === 'online') {
|
||||||
this.ring.republishDevices()
|
this.ring.republishDevices(this.client)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.ring.processDeviceCommand(topic, message)
|
this.ring.processDeviceCommand(topic, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateMqttState(state) {
|
||||||
|
this.connected = state
|
||||||
|
this.ring.updateMqttState(state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new Mqtt()
|
module.exports = new Mqtt()
|
||||||
27
lib/ring.js
27
lib/ring.js
@@ -31,6 +31,7 @@ class Ring {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.locations = new Array()
|
this.locations = new Array()
|
||||||
this.devices = new Array()
|
this.devices = new Array()
|
||||||
|
this.mqttConnected = false
|
||||||
this.republishCount = 6 // Republish config/state this many times after startup or HA start/restart
|
this.republishCount = 6 // Republish config/state this many times after startup or HA start/restart
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,13 +50,9 @@ class Ring {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Loop through each location and call publishLocation for supported/connected devices
|
// Loop through each location and call publishLocation for supported/connected devices
|
||||||
async processLocations(mqtt) {
|
async processLocations(mqttClient) {
|
||||||
if (mqtt) {
|
|
||||||
this.mqtt = mqtt
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update Ring location and device data
|
// Update Ring location and device data
|
||||||
await this.updateRingData()
|
await this.updateRingData(mqttClient)
|
||||||
|
|
||||||
// For each location get existing alarm & camera devices
|
// For each location get existing alarm & camera devices
|
||||||
this.locations.forEach(async location => {
|
this.locations.forEach(async location => {
|
||||||
@@ -98,7 +95,7 @@ class Ring {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update all Ring location/device data
|
// Update all Ring location/device data
|
||||||
async updateRingData() {
|
async updateRingData(mqttClient) {
|
||||||
// Small delay makes debug output more readable
|
// Small delay makes debug output more readable
|
||||||
await utils.sleep(1)
|
await utils.sleep(1)
|
||||||
|
|
||||||
@@ -152,7 +149,7 @@ class Ring {
|
|||||||
if (ringDevice) {
|
if (ringDevice) {
|
||||||
foundMessage = ' Existing device: '
|
foundMessage = ' Existing device: '
|
||||||
} else {
|
} else {
|
||||||
ringDevice = await this.getDevice(device, allDevices)
|
ringDevice = await this.getDevice(device, allDevices, mqttClient)
|
||||||
switch (ringDevice) {
|
switch (ringDevice) {
|
||||||
case 'not-supported':
|
case 'not-supported':
|
||||||
// Save unsupported device type
|
// Save unsupported device type
|
||||||
@@ -199,11 +196,11 @@ class Ring {
|
|||||||
|
|
||||||
|
|
||||||
// Return supported device
|
// Return supported device
|
||||||
async getDevice(device, allDevices) {
|
async getDevice(device, allDevices, mqttClient) {
|
||||||
const deviceInfo = {
|
const deviceInfo = {
|
||||||
device: device,
|
device: device,
|
||||||
allDevices: allDevices,
|
allDevices: allDevices,
|
||||||
mqttClient: this.mqtt.client,
|
mqttClient: mqttClient,
|
||||||
config: this.config
|
config: this.config
|
||||||
}
|
}
|
||||||
if (device instanceof RingCamera) {
|
if (device instanceof RingCamera) {
|
||||||
@@ -283,7 +280,7 @@ class Ring {
|
|||||||
// Publish devices/cameras for given location
|
// Publish devices/cameras for given location
|
||||||
async publishDevices(location) {
|
async publishDevices(location) {
|
||||||
this.republishCount = (this.republishCount < 1) ? 1 : this.republishCount
|
this.republishCount = (this.republishCount < 1) ? 1 : this.republishCount
|
||||||
while (this.republishCount > 0 && this.mqtt.connected) {
|
while (this.republishCount > 0 && this.mqttConnected) {
|
||||||
try {
|
try {
|
||||||
const devices = await this.devices.filter(d => d.locationId == location.locationId)
|
const devices = await this.devices.filter(d => d.locationId == location.locationId)
|
||||||
if (devices && devices.length) {
|
if (devices && devices.length) {
|
||||||
@@ -300,7 +297,7 @@ class Ring {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async republishDevices() {
|
async republishDevices(mqttClient) {
|
||||||
// Republish devices and state if restart of HA is detected
|
// Republish devices and state if restart of HA is detected
|
||||||
if (this.republishCount > 0) {
|
if (this.republishCount > 0) {
|
||||||
debug('Home Assisntat restart detected during existing republish cycle')
|
debug('Home Assisntat restart detected during existing republish cycle')
|
||||||
@@ -310,7 +307,7 @@ class Ring {
|
|||||||
debug('Home Assistant restart detected, resending device config/state in 5 seconds')
|
debug('Home Assistant restart detected, resending device config/state in 5 seconds')
|
||||||
await utils.sleep(5)
|
await utils.sleep(5)
|
||||||
this.republishCount = 6
|
this.republishCount = 6
|
||||||
this.processLocations()
|
this.processLocations(mqttClient)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,6 +332,10 @@ class Ring {
|
|||||||
async rssShutdown() {
|
async rssShutdown() {
|
||||||
await rss.shutdown()
|
await rss.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateMqttState(state) {
|
||||||
|
this.mqttConnected = state
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new Ring()
|
module.exports = new Ring()
|
||||||
Reference in New Issue
Block a user