-
Notifications
You must be signed in to change notification settings - Fork 171
/
other.py
295 lines (247 loc) · 8.88 KB
/
other.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# coding: utf-8
# vim:sw=4:ts=4:et:
"""Python Ring Other (Intercom) wrapper."""
import json
import logging
import uuid
from ring_doorbell.const import (
DOORBELLS_ENDPOINT,
HEALTH_DOORBELL_ENDPOINT,
INTERCOM_ALLOWED_USERS,
INTERCOM_INVITATIONS_DELETE_ENDPOINT,
INTERCOM_INVITATIONS_ENDPOINT,
INTERCOM_KINDS,
INTERCOM_OPEN_ENDPOINT,
MIC_VOL_MAX,
MIC_VOL_MIN,
MSG_VOL_OUTBOUND,
OTHER_DOORBELL_VOL_MAX,
OTHER_DOORBELL_VOL_MIN,
SETTINGS_ENDPOINT,
VOICE_VOL_MAX,
VOICE_VOL_MIN,
)
from ring_doorbell.exceptions import RingError
from ring_doorbell.generic import RingGeneric
_LOGGER = logging.getLogger(__name__)
class Other(RingGeneric):
"""Implementation for Ring Intercom."""
def __init__(self, ring, device_id, shared=False):
super().__init__(ring, device_id)
self.shared = shared
@property
def family(self):
"""Return Ring device family type."""
return "other"
def update_health_data(self):
"""Update health attrs."""
self._health_attrs = (
self._ring.query(HEALTH_DOORBELL_ENDPOINT.format(self.id))
.json()
.get("device_health", {})
)
@property
def model(self):
"""Return Ring device model name."""
if self.kind in INTERCOM_KINDS:
return "Intercom"
return None
def has_capability(self, capability):
"""Return if device has specific capability."""
if capability == "open":
return self.kind in INTERCOM_KINDS
return False
@property
def battery_life(self):
"""Return battery life."""
if self.kind in INTERCOM_KINDS:
if self._attrs.get("battery_life") is None:
return None
value = int(self._attrs.get("battery_life", 0))
if value and value > 100:
value = 100
return value
return None
@property
def subscribed(self):
"""Return if is online."""
if self.kind in INTERCOM_KINDS:
result = self._attrs.get("subscribed")
if result is None:
return False
return True
return None
@property
def subscriptions(self):
"""Return event type subscriptions."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("subscriptions", []).get("event_types", [])
return None
@property
def has_subscription(self):
"""Return boolean if the account has subscription."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("features").get("show_recordings")
return None
@property
def unlock_duration(self):
"""Return time unlock switch is held closed"""
json.loads(
self._attrs.get("settings").get("intercom_settings").get("config")
).get("analog").get("unlock_duration")
@property
def doorbell_volume(self):
"""Return doorbell volume."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("settings").get("doorbell_volume")
return None
@doorbell_volume.setter
def doorbell_volume(self, value):
if not (
(isinstance(value, int))
and (OTHER_DOORBELL_VOL_MIN <= value <= OTHER_DOORBELL_VOL_MAX)
):
_LOGGER.error(
"%s",
MSG_VOL_OUTBOUND.format(OTHER_DOORBELL_VOL_MIN, OTHER_DOORBELL_VOL_MAX),
)
return False
params = {
"doorbot[description]": self.name,
"doorbot[settings][doorbell_volume]": str(value),
}
url = DOORBELLS_ENDPOINT.format(self.id)
self._ring.query(url, extra_params=params, method="PUT")
self._ring.update_devices()
return True
@property
def keep_alive_auto(self):
if self.kind in INTERCOM_KINDS:
return self._attrs.get("settings").get("keep_alive_auto")
return None
@keep_alive_auto.setter
def keep_alive_auto(self, value):
url = SETTINGS_ENDPOINT.format(self.id)
payload = {"keep_alive_settings": {"keep_alive_auto": value}}
self._ring.query(url, method="PATCH", json=payload)
self._ring.update_devices()
return True
@property
def mic_volume(self):
"""Return mic volume."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("settings").get("mic_volume")
return None
@mic_volume.setter
def mic_volume(self, value):
if not ((isinstance(value, int)) and (MIC_VOL_MIN <= value <= MIC_VOL_MAX)):
_LOGGER.error("%s", MSG_VOL_OUTBOUND.format(MIC_VOL_MIN, MIC_VOL_MAX))
return False
url = SETTINGS_ENDPOINT.format(self.id)
payload = {"volume_settings": {"mic_volume": value}}
self._ring.query(url, method="PATCH", json=payload)
self._ring.update_devices()
return True
@property
def voice_volume(self):
"""Return voice volume."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("settings").get("voice_volume")
return None
@voice_volume.setter
def voice_volume(self, value):
if not ((isinstance(value, int)) and (VOICE_VOL_MIN <= value <= VOICE_VOL_MAX)):
_LOGGER.error("%s", MSG_VOL_OUTBOUND.format(VOICE_VOL_MIN, VOICE_VOL_MAX))
return False
url = SETTINGS_ENDPOINT.format(self.id)
payload = {"volume_settings": {"voice_volume": value}}
self._ring.query(url, method="PATCH", json=payload)
self._ring.update_devices()
return True
@property
def clip_length_max(self):
# this value sets an effective refractory period on consecutive rigns
# eg if set to default value of 60, rings occuring with 60 seconds of
# first will not be detected
url = SETTINGS_ENDPOINT.format(self.id)
return (
self._ring.query(url, method="GET")
.json()
.get("video_settings")
.get("clip_length_max")
)
@clip_length_max.setter
def clip_length_max(self, value):
url = SETTINGS_ENDPOINT.format(self.id)
payload = {"video_settings": {"clip_length_max": value}}
try:
self._ring.query(url, method="PATCH", json=payload)
self._ring.update_devices()
return True
except Exception as ex:
raise RingError(f"Unknown error during query of url {url}: {ex}") from ex
@property
def connection_status(self):
"""Return connection status."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("alerts").get("connection")
return None
@property
def location_id(self):
"""Return location id."""
if self.kind in INTERCOM_KINDS:
return self._attrs.get("location_id", None)
return None
@property
def allowed_users(self):
"""Return list of users allowed or invited to access"""
if self.kind in INTERCOM_KINDS:
url = INTERCOM_ALLOWED_USERS.format(self.location_id)
return self._ring.query(url, method="GET").json()
return None
def open_door(self):
"""Open the door"""
if self.kind in INTERCOM_KINDS:
url = INTERCOM_OPEN_ENDPOINT.format(self.id)
request_id = str(uuid.uuid4())
# request_timestamp = int(time.time() * 1000)
payload = {
"command_name": "device_rpc",
"request": {
"id": request_id,
"jsonrpc": "2.0",
"method": "unlock_door",
"params": {
# "command_timeout": 5,
"door_id": 0,
# "issue_time": request_timestamp,
"user_id": 64658594,
},
},
}
response = self._ring.query(url, method="PUT", json=payload).json()
self._ring.update_devices()
if response.get("result", -1).get("code", -1) == 0:
return True
return False
def invite_access(self, email):
"""Invite user"""
if self.kind in INTERCOM_KINDS:
url = INTERCOM_INVITATIONS_ENDPOINT.format(self.location_id)
payload = {
"invitation": {
"doorbot_ids": [self.id],
"invited_email": email,
"group_ids": [],
}
}
self._ring.query(url, method="POST", json=payload)
return True
return False
def remove_access(self, user_id):
"""Remove user access or invitation"""
if self.kind in INTERCOM_KINDS:
url = INTERCOM_INVITATIONS_DELETE_ENDPOINT.format(self.location_id, user_id)
self._ring.query(url, method="DELETE")
return True
return False