Files
ring-mqtt/devices/siren.js
tsightler b8338e30de Release 5.1.0 (#537)
* 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
2023-02-02 20:59:09 -05:00

87 lines
3.3 KiB
JavaScript

import RingSocketDevice from './base-socket-device.js'
export default class Siren extends RingSocketDevice {
constructor(deviceInfo) {
super(deviceInfo, 'alarm')
this.deviceData.mdl = (this.device.data.deviceType === 'siren.outdoor-strobe') ? 'Outdoor Siren' : 'Siren'
this.entity = {
...this.entity,
siren: {
component: 'switch',
icon: 'mdi:alarm-light',
isLegacyEntity: true // Legacy compatibility
},
...(this.device.data.deviceType === 'siren.outdoor-strobe') ? {
volume: {
component: 'number',
min: 0,
max: 4,
icon: 'hass:volume-high'
}
} : {}
}
}
publishState(data) {
const isPublish = data === undefined ? true : false
if (isPublish) {
// Eventually remove this but for now this attempts to delete the old siren binary_sensor
this.mqttPublish('homeassistant/binary_sensor/'+this.locationId+'/'+this.deviceId+'_siren/config', '', false)
}
const sirenState = this.device.data.sirenStatus === 'active' ? 'ON' : 'OFF'
this.mqttPublish(this.entity.siren.state_topic, sirenState)
if (this.entity.hasOwnProperty('volume')) {
const currentVolume = (this.device.data.volume && !isNaN(this.device.data.volume) ? Math.round(1 * this.device.data.volume) : 0)
this.mqttPublish(this.entity.volume.state_topic, currentVolume)
}
this.publishAttributes()
}
// Process messages from MQTT command topic
processCommand(command, message) {
switch (command) {
case 'siren/command':
this.setSirenState(message)
break;
case 'volume/command':
if (this.entity.hasOwnProperty('volume')) {
this.setVolumeLevel(message)
}
break;
default:
this.debug(`Received message to unknown command topic: ${command}`)
}
}
setSirenState(message) {
const command = message.toLowerCase()
switch(command) {
case 'on':
case 'off':
this.debug(`Received set siren state ${message}`)
if (this.device.data.deviceType === 'siren.outdoor-strobe') {
this.device.sendCommand((command ==='on') ? 'siren-test.start' : 'siren-test.stop')
} else {
this.device.setInfo({ device: { v1: { on: (command === 'on') ? true : false } } })
}
break;
default:
this.debug('Received invalid siren state command')
}
}
// Set volume level on received MQTT command message
setVolumeLevel(message) {
const volume = message / 1
this.debug(`Received set volume level to ${volume}`)
if (isNaN(message)) {
this.debug('Volume command received but value is not a number')
} else if (!(message >= 0 && message <= 4)) {
this.debug('Volume command received but out of range (0-4)')
} else {
this.device.setInfo({ device: { v1: { volume } } })
}
}
}