Files
ring-mqtt/devices/multi-level-switch.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

66 lines
2.3 KiB
JavaScript

import RingSocketDevice from './base-socket-device.js'
export default class MultiLevelSwitch extends RingSocketDevice {
constructor(deviceInfo) {
super(deviceInfo, 'alarm')
this.deviceData.mdl = 'Dimming Light'
this.entity.light = {
component: 'light',
brightness_scale: 100,
isLegacyEntity: true // Legacy compatibility
}
}
publishState() {
const switchState = this.device.data.on ? "ON" : "OFF"
const switchLevel = (this.device.data.level && !isNaN(this.device.data.level) ? Math.round(100 * this.device.data.level) : 0)
this.mqttPublish(this.entity.light.state_topic, switchState)
this.mqttPublish(this.entity.light.brightness_state_topic, switchLevel.toString())
this.publishAttributes()
}
// Process messages from MQTT command topic
processCommand(command, message) {
switch (command) {
case 'light/command':
this.setSwitchState(message)
break;
case 'light/brightness_command':
this.setSwitchLevel(message)
break;
default:
this.debug(`Received message to unknown command topic: ${command}`)
}
}
// Set switch target state on received MQTT command message
setSwitchState(message) {
this.debug(`Received set switch state ${message}`)
const command = message.toLowerCase()
switch(command) {
case 'on':
case 'off': {
const on = (command === 'on') ? true : false
this.device.setInfo({ device: { v1: { on } } })
break;
}
default:
this.debug('Received invalid command for switch!')
}
}
// Set switch target state on received MQTT command message
setSwitchLevel(message) {
const level = message
this.debug(`Received set switch level to ${level}%`)
if (isNaN(message)) {
this.debug('Brightness command received but not a number!')
} else if (!(message >= 0 && message <= 100)) {
this.debug('Brightness command received but out of range (0-100)!')
} else {
this.device.setInfo({ device: { v1: { level: level / 100 } } })
}
}
}