-
Notifications
You must be signed in to change notification settings - Fork 106
/
base-station.js
76 lines (67 loc) · 2.89 KB
/
base-station.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import RingSocketDevice from './base-socket-device.js'
import utils from '../lib/utils.js'
export default class BaseStation extends RingSocketDevice {
constructor(deviceInfo) {
super(deviceInfo, 'alarm', 'acStatus')
this.deviceData.mdl = 'Alarm Base Station'
this.deviceData.name = this.device.location.name + ' Base Station'
this.detectVolumeAccess()
}
async detectVolumeAccess() {
const origVolume = (this.device.data.volume && !isNaN(this.device.data.volume) ? this.device.data.volume : 0)
const testVolume = (origVolume === 1) ? .99 : origVolume+.01
this.device.setVolume(testVolume)
await utils.sleep(1)
if (this.device.data.volume === testVolume) {
this.debug('Account has access to set volume on base station, enabling volume control')
this.device.setVolume(origVolume)
this.entity.volume = {
component: 'number',
category: 'config',
min: 0,
max: 100,
mode: 'slider',
icon: 'hass:volume-high'
}
} else {
this.debug('Account does not have access to set volume on base station, disabling volume control')
}
}
publishState(data) {
const isPublish = Boolean(data === undefined)
if (this.entity.hasOwnProperty('volume')) {
const currentVolume = (this.device.data.volume && !isNaN(this.device.data.volume) ? Math.round(100 * this.device.data.volume) : 0)
this.mqttPublish(this.entity.volume.state_topic, currentVolume.toString())
// Eventually remove this but for now this attempts to delete the old light component based volume control from Home Assistant
if (isPublish) {
this.mqttPublish('homeassistant/light/'+this.locationId+'/'+this.deviceId+'_audio/config', '', false)
}
}
this.publishAttributes()
}
// Process messages from MQTT command topic
processCommand(command, message) {
const entityKey = command.split('/')[0]
switch (command) {
case 'volume/command':
if (this.entity.hasOwnProperty(entityKey)) {
this.setVolumeLevel(message)
}
break;
default:
this.debug(`Received message to unknown command topic: ${command}`)
}
}
// Set volume level on received MQTT command message
setVolumeLevel(message) {
const volume = message
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 <= 100)) {
this.debug('Volume command received but out of range (0-100)')
} else {
this.device.setVolume(volume/100)
}
}
}