mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-09-26 21:01:12 +08:00

* Use MQTT for start-stream debug messages * Fix ANSI colors * Refactor event URL management * Fix subscription detection * Improve event URL expiry handling by parsing Amazon S3 expire time * Convert to ESM/replace colors with chalk * Force colors for chalk * Migrate to ESM * Fix stop of keepalive stream * Add transcoded event selections * Update event URL on raw/trancoded toggle * Switch to per-camera livecall threads * Customized WebRTC functions Mostly copied from ring-client-api with port to pure Javascript, removal of unneeded features and additional debugging modified for use as worker thread with ring-mqtt. Allows easier testing with updated Werift versions. * Add nightlight enable/disable * Include nightlight state as attribute * Only pro versions have nightlight * Tweak battery level reporting for dual battery cameras * Release 5.1.0
125 lines
6.7 KiB
JavaScript
125 lines
6.7 KiB
JavaScript
import RingDevice from './base-ring-device.js'
|
|
import utils from '../lib/utils.js'
|
|
|
|
// Base class for devices that communicate with hubs via websocket (alarm/smart lighting)
|
|
export default class RingSocketDevice extends RingDevice {
|
|
constructor(deviceInfo, category, primaryAttribute) {
|
|
super(deviceInfo, category, primaryAttribute, deviceInfo.device.id, deviceInfo.device.location.locationId)
|
|
|
|
// Set default device data for Home Assistant device registry
|
|
this.deviceData = {
|
|
ids: [ this.deviceId ],
|
|
name: this.device.name,
|
|
mf: (this.device.data && this.device.data.manufacturerName) ? this.device.data.manufacturerName : 'Ring',
|
|
mdl: this.device.deviceType
|
|
}
|
|
|
|
this.device.onData.subscribe((data) => {
|
|
if (this.isOnline()) { this.publishState(data) }
|
|
})
|
|
}
|
|
|
|
// Publish device discovery, set online, and send all state data
|
|
async publish(locationConnected) {
|
|
if (locationConnected) {
|
|
await this.publishDiscovery()
|
|
// Sleep for a few seconds to give HA time to process discovery message
|
|
await utils.sleep(2)
|
|
await this.online()
|
|
this.publishState()
|
|
}
|
|
}
|
|
|
|
// Create device discovery data
|
|
initAttributeEntities(primaryAttribute) {
|
|
this.entity = {
|
|
...this.entity,
|
|
info: {
|
|
component: 'sensor',
|
|
...primaryAttribute
|
|
? { value_template: `{{ value_json["${primaryAttribute}"] | default("") }}` }
|
|
: { value_template: '{{ value_json["commStatus"] | default("") }}' }
|
|
},
|
|
...this.device.data.hasOwnProperty('batteryLevel') ? {
|
|
battery: {
|
|
component: 'sensor',
|
|
device_class: 'battery',
|
|
unit_of_measurement: '%',
|
|
state_class: 'measurement',
|
|
parent_state_topic: 'info/state',
|
|
attributes: 'battery',
|
|
value_template: '{{ value_json["batteryLevel"] | default("") }}'
|
|
}
|
|
} : {},
|
|
...this.device.data.hasOwnProperty('tamperStatus') ? {
|
|
tamper: {
|
|
component: 'binary_sensor',
|
|
device_class: 'problem',
|
|
parent_state_topic: 'info/state',
|
|
value_template: '{% if value_json["tamperStatus"] is equalto "tamper" %} ON {% else %} OFF {% endif %}'
|
|
}
|
|
} : {},
|
|
... this.device.data.hasOwnProperty('networkConnection') && this.device.data.networkConnection === 'wlan0'
|
|
&& this.device.data.hasOwnProperty('networks') && this.device.data.networks.hasOwnProperty('wlan0') ? {
|
|
wireless: {
|
|
component: 'sensor',
|
|
device_class: 'signal_strength',
|
|
unit_of_measurement: 'dBm',
|
|
parent_state_topic: 'info/state',
|
|
attributes: 'wireless',
|
|
value_template: '{{ value_json["wirelessSignal"] | default("") }}'
|
|
}
|
|
} : {}
|
|
}
|
|
}
|
|
|
|
// Publish device info
|
|
async publishAttributes() {
|
|
// Get full set of device data and publish to info topic JSON attributes
|
|
const attributes = {
|
|
... this.device.data.hasOwnProperty('acStatus') ? { acStatus: this.device.data.acStatus } : {},
|
|
... this.device.data.hasOwnProperty('alarmInfo') && this.device.data.alarmInfo !== null && this.device.data.alarmInfo.hasOwnProperty('state')
|
|
? { alarmState: this.device.data.alarmInfo.state }
|
|
: (this.device.deviceType === 'security-panel')
|
|
? { alarmState: 'all-clear' } : {},
|
|
... this.device.data.hasOwnProperty('batteryLevel')
|
|
? { batteryLevel: this.device.data.batteryLevel === 99 ? 100 : this.device.data.batteryLevel } : {},
|
|
... this.device.data.hasOwnProperty('batteryStatus') && this.device.data.batteryStatus !== 'none'
|
|
? { batteryStatus: this.device.data.batteryStatus } : {},
|
|
... (this.device.data.hasOwnProperty('auxBattery') && this.device.data.auxBattery.hasOwnProperty('level'))
|
|
? { auxBatteryLevel: this.device.data.auxBattery.level === 99 ? 100 : this.device.data.auxBattery.level } : {},
|
|
... (this.device.data.hasOwnProperty('auxBattery') && this.device.data.auxBattery.hasOwnProperty('status'))
|
|
? { auxBatteryStatus: this.device.data.auxBattery.status } : {},
|
|
... this.device.data.hasOwnProperty('brightness')
|
|
? { brightness: this.device.data.brightness } : {},
|
|
... this.device.data.hasOwnProperty('chirps') && this.device.deviceType == 'security-keypad'
|
|
? { chirps: this.device.data.chirps } : {},
|
|
... this.device.data.hasOwnProperty('commStatus')
|
|
? { commStatus: this.device.data.commStatus } : {},
|
|
... this.device.data.hasOwnProperty('firmwareUpdate')
|
|
? { firmwareStatus: this.device.data.firmwareUpdate.state } : {},
|
|
... this.device.data.hasOwnProperty('lastCommTime')
|
|
? { lastCommTime: utils.getISOTime(this.device.data.lastUpdate) } : {},
|
|
... this.device.data.hasOwnProperty('lastUpdate')
|
|
? { lastUpdate: utils.getISOTime(this.device.data.lastUpdate) } : {},
|
|
... this.device.data.hasOwnProperty('linkQuality')
|
|
? { linkQuality: this.device.data.linkQuality } : {},
|
|
... this.device.data.hasOwnProperty('powerSave')
|
|
? { powerSave: this.device.data.powerSave } : {},
|
|
... this.device.data.hasOwnProperty('serialNumber')
|
|
? { serialNumber: this.device.data.serialNumber } : {},
|
|
... this.device.data.hasOwnProperty('tamperStatus')
|
|
? { tamperStatus: this.device.data.tamperStatus } : {},
|
|
... this.device.data.hasOwnProperty('volume')
|
|
? {volume: this.device.data.volume } : {},
|
|
... this.device.data.hasOwnProperty('maxVolume')
|
|
? {maxVolume: this.device.data.maxVolume } : {},
|
|
... this.device.data.hasOwnProperty('networkConnection') && this.device.data.networkConnection === 'wlan0'
|
|
&& this.device.data.hasOwnProperty('networks') && this.device.data.networks.hasOwnProperty('wlan0')
|
|
? { wirelessNetwork: this.device.data.networks.wlan0.ssid, wirelessSignal: this.device.data.networks.wlan0.rssi } : {}
|
|
}
|
|
this.mqttPublish(this.entity.info.state_topic, JSON.stringify(attributes), 'attr')
|
|
this.publishAttributeEntities(attributes)
|
|
}
|
|
}
|