forked from tsightler/ring-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercom.js
203 lines (185 loc) · 6.75 KB
/
intercom.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import RingPolledDevice from './base-polled-device.js'
export default class Lock extends RingPolledDevice {
constructor(deviceInfo) {
super(deviceInfo, 'intercom')
this.deviceData.mdl = 'Intercom'
this.data = {
lock: {
state: 'LOCKED',
publishedState: null,
unlockTimeout: false
},
ding: {
state: 'OFF',
publishedState: null,
timeout: false
}
}
this.entity = {
...this.entity,
lock: {
component: 'lock'
},
ding: {
component: 'binary_sensor',
attributes: true,
icon: 'mdi:doorbell'
},
info: {
component: 'sensor',
device_class: 'timestamp',
value_template: '{{ value_json["lastUpdate"] | default("") }}'
}
}
this.device.onUnlocked.subscribe(() => {
this.setDoorUnlocked()
})
this.device.onDing.subscribe(() => {
this.processDing()
})
}
async initAttributeEntities() {
// If device is battery powered publish battery entity
if (this.device.batteryLevel !== null) {
this.entity.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("") }}'
}
}
const deviceHealth = await this.getHealth()
if (deviceHealth && !(deviceHealth?.network_connection && deviceHealth.network_connection === 'ethernet')) {
this.entity.wireless = {
component: 'sensor',
device_class: 'signal_strength',
unit_of_measurement: 'dBm',
parent_state_topic: 'info/state',
attributes: 'wireless',
value_template: '{{ value_json["wirelessSignal"] | default("") }}'
}
}
}
publishState(data) {
const isPublish = Boolean(data === undefined)
this.publishDingState(isPublish)
this.publishLockState(isPublish)
if (isPublish) {
this.publishAttributes()
}
}
publishDingState(isPublish) {
if (this.data.ding.state !== this.data.ding.publishedState || isPublish) {
this.mqttPublish(this.entity.ding.state_topic, this.data.ding.state)
this.data.ding.publishedState = this.data.ding.state
}
}
publishLockState(isPublish) {
if (this.data.lock.state !== this.data.lock.publishedState || isPublish) {
this.mqttPublish(this.entity.lock.state_topic, this.data.lock.state)
this.data.lock.publishedState = this.data.lock.state
}
}
// Publish device data to info topic
async publishAttributes() {
try {
const deviceHealth = await this.getHealth()
const attributes = {
...this.device?.batteryLevel
? { batteryLevel: this.device.batteryLevel } : {},
firmwareStatus: deviceHealth.firmware,
lastUpdate: deviceHealth.updated_at.slice(0,-6)+"Z",
wirelessNetwork: deviceHealth.wifi_name,
wirelessSignal: deviceHealth.latest_signal_strength
}
this.mqttPublish(this.entity.info.state_topic, JSON.stringify(attributes), 'attr')
this.publishAttributeEntities(attributes)
} catch(error) {
this.debug('Could not publish attributes due to no health data')
}
}
processDing() {
if (this.data.ding.timeout) {
clearTimeout(this.data.ding.timeout)
this.data.ding.timeout = false
}
this.data.ding.state = 'ON'
this.publishDingState()
this.data.ding.timeout = setTimeout(() => {
this.data.ding.state = 'OFF'
this.publishDingState()
this.data.ding.timeout = false
}, 20000)
}
setDoorUnlocked() {
if (this.data.lock.unlockTimeout) {
clearTimeout(this.data.lock.unlockTimeout)
this.data.lock.unlockTimeout = false
}
this.data.lock.state = 'UNLOCKED'
this.publishLockState()
this.data.lock.unlockTimeout = setTimeout(() => {
this.data.lock.state = 'LOCKED'
this.publishLockState()
this.data.lock.unlockTimeout = false
}, 5000)
}
async getHealth() {
try {
const response = await this.device.restClient.request({
url: this.device.doorbotUrl('health')
})
if (response.hasOwnProperty('device_health')) {
return response.device_health
} else {
this.debug('Failed to parse response from device health query')
this.debug(JSON.stringify(response))
}
} catch(error) {
this.debug('Failed to retrieve health data for Intercom')
this.debug(error)
}
return false
}
// Process messages from MQTT command topic
processCommand(command, message) {
switch (command) {
case 'lock/command':
this.setLockState(message)
break;
default:
this.debug(`Received message to unknown command topic: ${command}`)
}
}
// Set lock target state on received MQTT command message
async setLockState(message) {
const command = message.toLowerCase()
switch(command) {
case 'lock':
if (this.data.lock.state === 'UNLOCKED') {
this.debug('Received lock door command, setting locked state')
this.data.lock.state === 'LOCKED'
this.publishLockState()
} else {
this.debug('Received lock door command, but door is already locked')
}
break;
case 'unlock':
this.debug('Received unlock door command, sending unlock command to intercom')
try {
await this.device.unlock()
this.debug('Request to unlock door was successful')
this.setDoorUnlocked()
} catch(error) {
this.debug(error)
this.debug('Request to unlock door failed')
}
break;
default:
this.debug('Received invalid command for lock')
}
}
}