Skip to content

Commit

Permalink
Per-device duration for smart lights
Browse files Browse the repository at this point in the history
  • Loading branch information
tsightler committed Apr 25, 2022
1 parent 835ab9d commit 6fd8c46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
1 change: 0 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"mqtt_url": "mqtt://localhost:1883",
"disarm_code": "",
"beam_duration": 0,
"livestream_user": "",
"livestream_pass": "",
"enable_cameras": true,
Expand Down
29 changes: 21 additions & 8 deletions devices/beam.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Beam extends RingSocketDevice {
constructor(deviceInfo) {
super(deviceInfo, 'lighting')

this.data = {}

// Setup device topics based on capabilities.
switch (this.device.data.deviceType) {
case 'group.light-group.beams':
Expand Down Expand Up @@ -38,6 +40,14 @@ class Beam extends RingSocketDevice {
}

initLightEntity() {
const savedState = this.getSavedState()

if (savedState?.beam_duration) {
this.data.beam_duration = savedState?.beam_duration
} else {
this.data.beam_duration = this.device.data.hasOwnProperty('onDuration') ? this.device.data.onDuration : 0
}

this.entity.light = {
component: 'light',
...this.device.data.deviceType === 'switch.multilevel.beams' ? { brightness_scale: 100 } : {}
Expand All @@ -52,11 +62,14 @@ class Beam extends RingSocketDevice {
icon: 'hass:timer'
}

if (utils.config.hasOwnProperty('beam_duration') && utils.config.beam_duration > 0) {
this.entity.beam_duration.state = utils.config.beam_duration
} else {
this.entity.beam_duration.state = this.device.data.hasOwnProperty('onDuration') ? this.device.data.onDuration : 0
this.updateDeviceState()
}

updateDeviceState() {
const stateData = {
beam_duration: this.data.beam_duration
}
this.setSavedState(stateData)
}

publishState() {
Expand All @@ -71,7 +84,7 @@ class Beam extends RingSocketDevice {
const switchLevel = (this.device.data.level && !isNaN(this.device.data.level) ? Math.round(100 * this.device.data.level) : 0)
this.mqttPublish(this.entity.light.brightness_state_topic, switchLevel.toString())
}
this.mqttPublish(this.entity.beam_duration.state_topic, this.entity.beam_duration.state.toString())
this.mqttPublish(this.entity.beam_duration.state_topic, this.data.beam_duration)
}
if (!this.isLightGroup) {
this.publishAttributes()
Expand Down Expand Up @@ -109,7 +122,7 @@ class Beam extends RingSocketDevice {
switch(command) {
case 'on':
case 'off': {
const duration = this.entity.beam_duration.state ? Math.min(this.entity.beam_duration.state, 32767) : undefined
const duration = this.data.beam_duration ? Math.min(this.data.beam_duration, 32767) : undefined
const on = command === 'on' ? true : false
if (this.isLightGroup && this.groupId) {
this.device.location.setLightGroup(this.groupId, on, duration)
Expand Down Expand Up @@ -145,8 +158,8 @@ class Beam extends RingSocketDevice {
} else if (!(duration >= 0 && duration <= 32767)) {
this.debug('Light duration command received but out of range (0-32767)')
} else {
this.entity.beam_duration.state = parseInt(duration)
this.mqttPublish(this.entity.beam_duration.state_topic, this.entity.beam_duration.state.toString())
this.data.beam_duration = parseInt(duration)
this.mqttPublish(this.entity.beam_duration.state_topic, this.data.beam_duration)
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Config {
if (!this.data.enable_cameras) { this.data.enable_cameras = false }
if (!this.data.enable_modes) { this.data.enable_modes = false }
if (!this.data.enable_panic) { this.data.enable_panic = false }
if (!this.data.beam_duration) { this.data.beam_duration = 0 }
if (!this.data.disarm_code) { this.data.disarm_code = '' }

// Export MQTTURL environment variable even if only using config file (standalone install)
Expand Down Expand Up @@ -85,7 +84,6 @@ class Config {
"ring_topic": process.env.MQTTRINGTOPIC,
"hass_topic": process.env.MQTTHASSTOPIC,
"disarm_code": process.env.DISARMCODE,
"beam_duration": process.env.BEAMDURATION,
"enable_cameras": process.env.ENABLECAMERAS,
"livestream_user": process.env.LIVESTREAMUSER,
"livestream_pass": process.env.LIVESTREAMPASSWORD,
Expand Down Expand Up @@ -127,7 +125,14 @@ class Config {

async updateConfig() {
try {
delete this.data.ring_token
if (this.data.hasOwnProperty('ring_token')) {
delete this.data.ring_token
}

if (this.data.hasOwnProperty('beam_duration')) {
delete this.data.beam_duration
}

await writeFileAtomic(this.file, JSON.stringify(this.data, null, 4))
debug('Successfully saved updated config file: '+this.file)
} catch (err) {
Expand Down

0 comments on commit 6fd8c46

Please sign in to comment.