python-ring 0.10.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_ring/__init__.py +44 -0
- python_ring/auth.py +283 -0
- python_ring/chime.py +135 -0
- python_ring/cli.py +977 -0
- python_ring/const.py +289 -0
- python_ring/doorbot.py +551 -0
- python_ring/event.py +46 -0
- python_ring/exceptions.py +17 -0
- python_ring/generic.py +267 -0
- python_ring/group.py +145 -0
- python_ring/listen/__init__.py +13 -0
- python_ring/listen/eventlistener.py +449 -0
- python_ring/listen/listenerconfig.py +21 -0
- python_ring/other.py +298 -0
- python_ring/py.typed +0 -0
- python_ring/ring.py +476 -0
- python_ring/stickup_cam.py +253 -0
- python_ring/util.py +157 -0
- python_ring/webrtcdevice.py +168 -0
- python_ring/webrtcstream.py +433 -0
- python_ring-0.10.0.dist-info/METADATA +103 -0
- python_ring-0.10.0.dist-info/RECORD +25 -0
- python_ring-0.10.0.dist-info/WHEEL +4 -0
- python_ring-0.10.0.dist-info/entry_points.txt +2 -0
- python_ring-0.10.0.dist-info/licenses/LICENSE +165 -0
python_ring/doorbot.py
ADDED
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
# vim:sw=4:ts=4:et:
|
|
2
|
+
"""Python Ring Doorbell wrapper."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import asyncio
|
|
7
|
+
import logging
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import TYPE_CHECKING, Any, ClassVar
|
|
11
|
+
|
|
12
|
+
import aiofiles
|
|
13
|
+
from typing_extensions import deprecated
|
|
14
|
+
|
|
15
|
+
from python_ring.const import (
|
|
16
|
+
DEFAULT_VIDEO_DOWNLOAD_TIMEOUT,
|
|
17
|
+
DINGS_ENDPOINT,
|
|
18
|
+
DOORBELL_2_KINDS,
|
|
19
|
+
DOORBELL_3_KINDS,
|
|
20
|
+
DOORBELL_3_PLUS_KINDS,
|
|
21
|
+
DOORBELL_4_KINDS,
|
|
22
|
+
DOORBELL_BATTERY_KINDS,
|
|
23
|
+
DOORBELL_BATTERY_PRO_KINDS,
|
|
24
|
+
DOORBELL_ELITE_KINDS,
|
|
25
|
+
DOORBELL_EXISTING_DURATION_MAX,
|
|
26
|
+
DOORBELL_EXISTING_DURATION_MIN,
|
|
27
|
+
DOORBELL_EXISTING_TYPE,
|
|
28
|
+
DOORBELL_GEN2_KINDS,
|
|
29
|
+
DOORBELL_KINDS,
|
|
30
|
+
DOORBELL_PRO_2_KINDS,
|
|
31
|
+
DOORBELL_PRO_4K_BATTERY_KINDS,
|
|
32
|
+
DOORBELL_PRO_4K_WIRED_KINDS,
|
|
33
|
+
DOORBELL_PRO_KINDS,
|
|
34
|
+
DOORBELL_VOL_MAX,
|
|
35
|
+
DOORBELL_VOL_MIN,
|
|
36
|
+
DOORBELL_WIRED_KINDS,
|
|
37
|
+
DOORBELL_WIRED_PLUS_2_KINDS,
|
|
38
|
+
DOORBELLS_ENDPOINT,
|
|
39
|
+
FILE_EXISTS,
|
|
40
|
+
HEALTH_DOORBELL_ENDPOINT,
|
|
41
|
+
LIVE_STREAMING_ENDPOINT,
|
|
42
|
+
MOTION_SNOOZE_CLEAR_ENDPOINT,
|
|
43
|
+
MOTION_SNOOZE_ENDPOINT,
|
|
44
|
+
MSG_ALLOWED_VALUES,
|
|
45
|
+
MSG_BOOLEAN_REQUIRED,
|
|
46
|
+
MSG_EXISTING_TYPE,
|
|
47
|
+
MSG_EXPECTED_ATTRIBUTE_NOT_FOUND,
|
|
48
|
+
MSG_SNOOZE_OUTBOUND,
|
|
49
|
+
MSG_VOL_OUTBOUND,
|
|
50
|
+
PEEPHOLE_CAM_KINDS,
|
|
51
|
+
SETTINGS_ENDPOINT,
|
|
52
|
+
SNAPSHOT_ENDPOINT,
|
|
53
|
+
SNAPSHOT_TIMESTAMP_ENDPOINT,
|
|
54
|
+
SNOOZE_MINUTES_MAX,
|
|
55
|
+
URL_RECORDING,
|
|
56
|
+
URL_RECORDING_SHARE_PLAY,
|
|
57
|
+
RingCapability,
|
|
58
|
+
)
|
|
59
|
+
from python_ring.exceptions import RingError
|
|
60
|
+
from python_ring.generic import RingGeneric
|
|
61
|
+
from python_ring.webrtcdevice import RingWebRtcMixin
|
|
62
|
+
|
|
63
|
+
_LOGGER = logging.getLogger(__name__)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class RingDoorBell(RingWebRtcMixin, RingGeneric):
|
|
67
|
+
"""Implementation for Ring Doorbell."""
|
|
68
|
+
|
|
69
|
+
if TYPE_CHECKING:
|
|
70
|
+
from python_ring.ring import Ring
|
|
71
|
+
|
|
72
|
+
def __init__(self, ring: Ring, device_api_id: int, *, shared: bool = False) -> None:
|
|
73
|
+
"""Initialise the doorbell."""
|
|
74
|
+
super().__init__(ring, device_api_id)
|
|
75
|
+
self.shared = shared
|
|
76
|
+
self._webrtc_streams = {}
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def family(self) -> str:
|
|
80
|
+
"""Return Ring device family type."""
|
|
81
|
+
return "authorized_doorbots" if self.shared else "doorbots"
|
|
82
|
+
|
|
83
|
+
async def async_update_health_data(self) -> None:
|
|
84
|
+
"""Update health attrs."""
|
|
85
|
+
resp = await self._ring.async_query(
|
|
86
|
+
HEALTH_DOORBELL_ENDPOINT.format(self.device_api_id)
|
|
87
|
+
)
|
|
88
|
+
self._health_attrs = resp.json().get("device_health", {})
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def model(self) -> str: # noqa: C901, PLR0911, PLR0912
|
|
92
|
+
"""Return Ring device model name."""
|
|
93
|
+
if self.kind in DOORBELL_KINDS:
|
|
94
|
+
return "Doorbell"
|
|
95
|
+
if self.kind in DOORBELL_2_KINDS:
|
|
96
|
+
return "Doorbell 2"
|
|
97
|
+
if self.kind in DOORBELL_3_KINDS:
|
|
98
|
+
return "Doorbell 3"
|
|
99
|
+
if self.kind in DOORBELL_3_PLUS_KINDS:
|
|
100
|
+
return "Doorbell 3 Plus"
|
|
101
|
+
if self.kind in DOORBELL_4_KINDS:
|
|
102
|
+
return "Doorbell 4"
|
|
103
|
+
if self.kind in DOORBELL_PRO_KINDS:
|
|
104
|
+
return "Doorbell Pro"
|
|
105
|
+
if self.kind in DOORBELL_PRO_2_KINDS:
|
|
106
|
+
return "Doorbell Pro 2"
|
|
107
|
+
if self.kind in DOORBELL_PRO_4K_WIRED_KINDS:
|
|
108
|
+
return "Wired Doorbell 4K Pro"
|
|
109
|
+
if self.kind in DOORBELL_PRO_4K_BATTERY_KINDS:
|
|
110
|
+
return "Battery Doorbell 4K Pro"
|
|
111
|
+
if self.kind in DOORBELL_ELITE_KINDS:
|
|
112
|
+
return "Doorbell Elite"
|
|
113
|
+
if self.kind in DOORBELL_WIRED_KINDS:
|
|
114
|
+
return "Doorbell Wired"
|
|
115
|
+
if self.kind in DOORBELL_WIRED_PLUS_2_KINDS:
|
|
116
|
+
return "Wired Doorbell Plus (2nd Gen)"
|
|
117
|
+
if self.kind in DOORBELL_BATTERY_KINDS:
|
|
118
|
+
return "Battery Doorbell"
|
|
119
|
+
if self.kind in DOORBELL_BATTERY_PRO_KINDS:
|
|
120
|
+
return "Battery Doorbell Pro"
|
|
121
|
+
if self.kind in DOORBELL_GEN2_KINDS:
|
|
122
|
+
return "Doorbell (2nd Gen)"
|
|
123
|
+
if self.kind in PEEPHOLE_CAM_KINDS:
|
|
124
|
+
return "Peephole Cam"
|
|
125
|
+
return "Unknown Doorbell"
|
|
126
|
+
|
|
127
|
+
def has_capability(self, capability: RingCapability | str) -> bool: # noqa: PLR0911
|
|
128
|
+
"""Return if device has specific capability."""
|
|
129
|
+
capability = (
|
|
130
|
+
capability
|
|
131
|
+
if isinstance(capability, RingCapability)
|
|
132
|
+
else RingCapability.from_name(capability)
|
|
133
|
+
)
|
|
134
|
+
if capability == RingCapability.BATTERY:
|
|
135
|
+
return self.kind in (
|
|
136
|
+
DOORBELL_KINDS
|
|
137
|
+
+ DOORBELL_2_KINDS
|
|
138
|
+
+ DOORBELL_3_KINDS
|
|
139
|
+
+ DOORBELL_3_PLUS_KINDS
|
|
140
|
+
+ DOORBELL_4_KINDS
|
|
141
|
+
+ DOORBELL_GEN2_KINDS
|
|
142
|
+
+ DOORBELL_BATTERY_KINDS
|
|
143
|
+
+ DOORBELL_BATTERY_PRO_KINDS
|
|
144
|
+
+ DOORBELL_PRO_4K_BATTERY_KINDS
|
|
145
|
+
+ PEEPHOLE_CAM_KINDS
|
|
146
|
+
)
|
|
147
|
+
if capability == RingCapability.KNOCK:
|
|
148
|
+
return self.kind in PEEPHOLE_CAM_KINDS
|
|
149
|
+
if capability == RingCapability.PRE_ROLL:
|
|
150
|
+
return self.kind in (
|
|
151
|
+
DOORBELL_3_PLUS_KINDS
|
|
152
|
+
+ DOORBELL_WIRED_PLUS_2_KINDS
|
|
153
|
+
+ DOORBELL_PRO_4K_WIRED_KINDS
|
|
154
|
+
)
|
|
155
|
+
if capability == RingCapability.VOLUME:
|
|
156
|
+
return True
|
|
157
|
+
if capability == RingCapability.HISTORY:
|
|
158
|
+
return True
|
|
159
|
+
if capability in [
|
|
160
|
+
RingCapability.MOTION_DETECTION,
|
|
161
|
+
RingCapability.VIDEO,
|
|
162
|
+
RingCapability.DING,
|
|
163
|
+
]:
|
|
164
|
+
return self.kind in (
|
|
165
|
+
DOORBELL_KINDS
|
|
166
|
+
+ DOORBELL_2_KINDS
|
|
167
|
+
+ DOORBELL_3_KINDS
|
|
168
|
+
+ DOORBELL_3_PLUS_KINDS
|
|
169
|
+
+ DOORBELL_4_KINDS
|
|
170
|
+
+ DOORBELL_PRO_KINDS
|
|
171
|
+
+ DOORBELL_PRO_2_KINDS
|
|
172
|
+
+ DOORBELL_WIRED_KINDS
|
|
173
|
+
+ DOORBELL_BATTERY_KINDS
|
|
174
|
+
+ DOORBELL_GEN2_KINDS
|
|
175
|
+
+ DOORBELL_ELITE_KINDS
|
|
176
|
+
+ DOORBELL_BATTERY_PRO_KINDS
|
|
177
|
+
+ DOORBELL_PRO_4K_BATTERY_KINDS
|
|
178
|
+
+ DOORBELL_PRO_4K_WIRED_KINDS
|
|
179
|
+
+ DOORBELL_WIRED_PLUS_2_KINDS
|
|
180
|
+
+ PEEPHOLE_CAM_KINDS
|
|
181
|
+
)
|
|
182
|
+
return False
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def battery_life(self) -> int | None:
|
|
186
|
+
"""Return battery life."""
|
|
187
|
+
if (
|
|
188
|
+
bl1 := self._attrs.get("battery_life")
|
|
189
|
+
) is None and "battery_life_2" not in self._attrs:
|
|
190
|
+
return None
|
|
191
|
+
|
|
192
|
+
value = 0
|
|
193
|
+
if bl1:
|
|
194
|
+
value += int(bl1)
|
|
195
|
+
|
|
196
|
+
if bl2 := self._attrs.get("battery_life_2"): # Camera has two battery bays
|
|
197
|
+
value += int(bl2)
|
|
198
|
+
|
|
199
|
+
return min(value, 100)
|
|
200
|
+
|
|
201
|
+
@property
|
|
202
|
+
def battery_life_1(self) -> int | None:
|
|
203
|
+
"""Return the level of the first battery."""
|
|
204
|
+
if (value := self._attrs.get("battery_life")) is None:
|
|
205
|
+
return None
|
|
206
|
+
return min(int(value), 100)
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def battery_life_2(self) -> int | None:
|
|
210
|
+
"""Return the level of the second battery of dual battery devices."""
|
|
211
|
+
if (value := self._attrs.get("battery_life_2")) is None:
|
|
212
|
+
return None
|
|
213
|
+
return min(int(value), 100)
|
|
214
|
+
|
|
215
|
+
@property
|
|
216
|
+
def motion_snoozed(self) -> bool:
|
|
217
|
+
"""Return if motion alerts are snoozed."""
|
|
218
|
+
return bool(self._attrs.get("motion_snooze"))
|
|
219
|
+
|
|
220
|
+
@property
|
|
221
|
+
def motion_snooze_seconds_left(self) -> int:
|
|
222
|
+
"""Return the remaining time of a temporary motion snooze in seconds."""
|
|
223
|
+
snooze = self._attrs.get("motion_snooze") or {}
|
|
224
|
+
return int((snooze.get("temporary") or {}).get("seconds_left") or 0)
|
|
225
|
+
|
|
226
|
+
async def async_set_motion_snooze(self, minutes: int) -> None:
|
|
227
|
+
"""Snooze motion alerts for the given number of minutes."""
|
|
228
|
+
if not (isinstance(minutes, int) and 1 <= minutes <= SNOOZE_MINUTES_MAX):
|
|
229
|
+
raise RingError(MSG_SNOOZE_OUTBOUND.format(SNOOZE_MINUTES_MAX))
|
|
230
|
+
url = MOTION_SNOOZE_ENDPOINT.format(self.device_api_id)
|
|
231
|
+
await self._ring.async_query(url, method="POST", json={"time": minutes})
|
|
232
|
+
self._attrs["motion_snooze"] = {"temporary": {"seconds_left": minutes * 60}}
|
|
233
|
+
|
|
234
|
+
async def async_clear_motion_snooze(self) -> None:
|
|
235
|
+
"""Clear a motion alert snooze."""
|
|
236
|
+
url = MOTION_SNOOZE_CLEAR_ENDPOINT.format(self.device_api_id)
|
|
237
|
+
await self._ring.async_query(url, method="POST")
|
|
238
|
+
self._attrs["motion_snooze"] = None
|
|
239
|
+
|
|
240
|
+
def _get_chime_setting(self, setting: str) -> Any | None:
|
|
241
|
+
if (settings := self._attrs.get("settings")) and (
|
|
242
|
+
chime_settings := settings.get("chime_settings")
|
|
243
|
+
):
|
|
244
|
+
return chime_settings.get(setting)
|
|
245
|
+
return None
|
|
246
|
+
|
|
247
|
+
@property
|
|
248
|
+
def existing_doorbell_type(self) -> str | None:
|
|
249
|
+
"""
|
|
250
|
+
Return existing doorbell type.
|
|
251
|
+
|
|
252
|
+
0: Mechanical
|
|
253
|
+
1: Digital
|
|
254
|
+
2: Not Present
|
|
255
|
+
"""
|
|
256
|
+
try:
|
|
257
|
+
if (dtype := self._get_chime_setting("type")) is not None:
|
|
258
|
+
return DOORBELL_EXISTING_TYPE.get(dtype)
|
|
259
|
+
except AttributeError:
|
|
260
|
+
return None
|
|
261
|
+
else:
|
|
262
|
+
return None
|
|
263
|
+
|
|
264
|
+
def _validate_existing_doorbell_type(self) -> None:
|
|
265
|
+
"""Raise if the device reports an unsupported existing doorbell type."""
|
|
266
|
+
dtype = self._get_chime_setting("type")
|
|
267
|
+
if dtype is not None and dtype not in DOORBELL_EXISTING_TYPE:
|
|
268
|
+
msg = f"Unsupported existing doorbell type: {dtype}"
|
|
269
|
+
raise RingError(msg)
|
|
270
|
+
|
|
271
|
+
async def async_set_existing_doorbell_type(self, value: int) -> None:
|
|
272
|
+
"""
|
|
273
|
+
Return existing doorbell type.
|
|
274
|
+
|
|
275
|
+
0: Mechanical
|
|
276
|
+
1: Digital
|
|
277
|
+
2: Not Present
|
|
278
|
+
"""
|
|
279
|
+
self._validate_existing_doorbell_type()
|
|
280
|
+
if value not in DOORBELL_EXISTING_TYPE:
|
|
281
|
+
msg = f"value must be in {MSG_EXISTING_TYPE}"
|
|
282
|
+
raise RingError(msg)
|
|
283
|
+
params = {
|
|
284
|
+
"doorbot[description]": self.name,
|
|
285
|
+
"doorbot[settings][chime_settings][type]": value,
|
|
286
|
+
}
|
|
287
|
+
if self.existing_doorbell_type:
|
|
288
|
+
url = DOORBELLS_ENDPOINT.format(self.device_api_id)
|
|
289
|
+
await self._ring.async_query(url, extra_params=params, method="PUT")
|
|
290
|
+
|
|
291
|
+
@property
|
|
292
|
+
def existing_doorbell_type_enabled(self) -> bool | None:
|
|
293
|
+
"""Return if existing doorbell type is enabled."""
|
|
294
|
+
if self.existing_doorbell_type:
|
|
295
|
+
if self.existing_doorbell_type == DOORBELL_EXISTING_TYPE[2]:
|
|
296
|
+
return None
|
|
297
|
+
return self._get_chime_setting("enable")
|
|
298
|
+
return False
|
|
299
|
+
|
|
300
|
+
async def async_set_existing_doorbell_type_enabled(self, value: bool) -> None: # noqa: FBT001
|
|
301
|
+
"""Enable/disable the existing doorbell if Digital/Mechanical."""
|
|
302
|
+
self._validate_existing_doorbell_type()
|
|
303
|
+
if self.existing_doorbell_type:
|
|
304
|
+
if not isinstance(value, bool):
|
|
305
|
+
raise RingError(MSG_BOOLEAN_REQUIRED)
|
|
306
|
+
|
|
307
|
+
if self.existing_doorbell_type == DOORBELL_EXISTING_TYPE[2]:
|
|
308
|
+
msg = "In-Home chime is not present."
|
|
309
|
+
raise RingError(msg)
|
|
310
|
+
|
|
311
|
+
int_value = int(value)
|
|
312
|
+
|
|
313
|
+
params = {
|
|
314
|
+
"doorbot[description]": self.name,
|
|
315
|
+
"doorbot[settings][chime_settings][enable]": int_value,
|
|
316
|
+
}
|
|
317
|
+
url = DOORBELLS_ENDPOINT.format(self.device_api_id)
|
|
318
|
+
await self._ring.async_query(url, extra_params=params, method="PUT")
|
|
319
|
+
|
|
320
|
+
@property
|
|
321
|
+
def existing_doorbell_type_duration(self) -> int | None:
|
|
322
|
+
"""Return duration for Digital chime."""
|
|
323
|
+
if (
|
|
324
|
+
self.existing_doorbell_type
|
|
325
|
+
and self.existing_doorbell_type == DOORBELL_EXISTING_TYPE[1]
|
|
326
|
+
):
|
|
327
|
+
return self._get_chime_setting("duration")
|
|
328
|
+
return None
|
|
329
|
+
|
|
330
|
+
async def async_set_existing_doorbell_type_duration(self, value: int) -> None:
|
|
331
|
+
"""Set duration for Digital chime."""
|
|
332
|
+
self._validate_existing_doorbell_type()
|
|
333
|
+
if self.existing_doorbell_type:
|
|
334
|
+
if not (
|
|
335
|
+
(isinstance(value, int))
|
|
336
|
+
and (
|
|
337
|
+
DOORBELL_EXISTING_DURATION_MIN
|
|
338
|
+
<= value
|
|
339
|
+
<= DOORBELL_EXISTING_DURATION_MAX
|
|
340
|
+
)
|
|
341
|
+
):
|
|
342
|
+
raise RingError(
|
|
343
|
+
MSG_VOL_OUTBOUND.format(
|
|
344
|
+
DOORBELL_EXISTING_DURATION_MIN, DOORBELL_EXISTING_DURATION_MAX
|
|
345
|
+
)
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
if self.existing_doorbell_type == DOORBELL_EXISTING_TYPE[1]:
|
|
349
|
+
params = {
|
|
350
|
+
"doorbot[description]": self.name,
|
|
351
|
+
"doorbot[settings][chime_settings][duration]": value,
|
|
352
|
+
}
|
|
353
|
+
url = DOORBELLS_ENDPOINT.format(self.device_api_id)
|
|
354
|
+
await self._ring.async_query(url, extra_params=params, method="PUT")
|
|
355
|
+
|
|
356
|
+
async def async_get_last_recording_id(self) -> int | None:
|
|
357
|
+
"""Return the last recording ID."""
|
|
358
|
+
try:
|
|
359
|
+
res = await self.async_history(limit=1)
|
|
360
|
+
return res[0].get("id") if res else None
|
|
361
|
+
except (IndexError, TypeError):
|
|
362
|
+
return None
|
|
363
|
+
|
|
364
|
+
async def async_get_live_streaming_json(self) -> dict[str, Any] | None:
|
|
365
|
+
"""Return JSON for live streaming."""
|
|
366
|
+
url = LIVE_STREAMING_ENDPOINT.format(self.device_api_id)
|
|
367
|
+
req = await self._ring.async_query(
|
|
368
|
+
url, method="POST", base_uri="https://app.ring.com"
|
|
369
|
+
)
|
|
370
|
+
if req and req.status_code == 200:
|
|
371
|
+
url = DINGS_ENDPOINT
|
|
372
|
+
try:
|
|
373
|
+
resp = await self._ring.async_query(url)
|
|
374
|
+
return resp.json()[0]
|
|
375
|
+
except (IndexError, TypeError):
|
|
376
|
+
pass
|
|
377
|
+
return None
|
|
378
|
+
|
|
379
|
+
async def async_recording_download(
|
|
380
|
+
self,
|
|
381
|
+
recording_id: int,
|
|
382
|
+
filename: str | None = None,
|
|
383
|
+
*,
|
|
384
|
+
override: bool = False,
|
|
385
|
+
timeout: int = DEFAULT_VIDEO_DOWNLOAD_TIMEOUT,
|
|
386
|
+
) -> bytes | None:
|
|
387
|
+
"""Save a recording in MP4 format to a file or return raw."""
|
|
388
|
+
if not self.has_subscription:
|
|
389
|
+
msg = "Your Ring account does not have an active subscription."
|
|
390
|
+
_LOGGER.warning(msg)
|
|
391
|
+
return None
|
|
392
|
+
|
|
393
|
+
url = URL_RECORDING.format(recording_id)
|
|
394
|
+
try:
|
|
395
|
+
# Video download needs a longer timeout to get the large video file
|
|
396
|
+
req = await self._ring.async_query(url, timeout=timeout)
|
|
397
|
+
if req.status_code == 200:
|
|
398
|
+
if filename:
|
|
399
|
+
if Path(filename).is_file() and not override:
|
|
400
|
+
raise RingError(FILE_EXISTS.format(filename))
|
|
401
|
+
|
|
402
|
+
async with aiofiles.open(filename, "wb") as recording:
|
|
403
|
+
await recording.write(req.content)
|
|
404
|
+
return None
|
|
405
|
+
else:
|
|
406
|
+
return req.content
|
|
407
|
+
else:
|
|
408
|
+
msg = (
|
|
409
|
+
f"Could not get recording at url {url}, "
|
|
410
|
+
f"status code is {req.status_code}"
|
|
411
|
+
)
|
|
412
|
+
raise RingError(msg)
|
|
413
|
+
except OSError as error:
|
|
414
|
+
msg = f"Error downloading recording {recording_id}: {error}"
|
|
415
|
+
_LOGGER.exception(msg)
|
|
416
|
+
raise RingError(msg) from error
|
|
417
|
+
|
|
418
|
+
async def async_recording_url(self, recording_id: int) -> str | None:
|
|
419
|
+
"""Return HTTPS recording URL."""
|
|
420
|
+
if not self.has_subscription:
|
|
421
|
+
msg = "Your Ring account does not have an active subscription."
|
|
422
|
+
_LOGGER.warning(msg)
|
|
423
|
+
return None
|
|
424
|
+
|
|
425
|
+
url = URL_RECORDING_SHARE_PLAY.format(recording_id)
|
|
426
|
+
req = await self._ring.async_query(url)
|
|
427
|
+
data = req.json()
|
|
428
|
+
if req and req.status_code == 200 and data is not None:
|
|
429
|
+
return data["url"]
|
|
430
|
+
return None
|
|
431
|
+
|
|
432
|
+
@property
|
|
433
|
+
def subscribed(self) -> bool:
|
|
434
|
+
"""Return if is online."""
|
|
435
|
+
result = self._attrs.get("subscribed")
|
|
436
|
+
return result is not None
|
|
437
|
+
|
|
438
|
+
@property
|
|
439
|
+
def subscribed_motion(self) -> bool:
|
|
440
|
+
"""Return if is subscribed_motion."""
|
|
441
|
+
result = self._attrs.get("subscribed_motions")
|
|
442
|
+
return result is not None
|
|
443
|
+
|
|
444
|
+
@property
|
|
445
|
+
def has_subscription(self) -> bool:
|
|
446
|
+
"""Return boolean if the account has subscription."""
|
|
447
|
+
if features := self._attrs.get("features"):
|
|
448
|
+
return features.get("show_recordings", False)
|
|
449
|
+
return False
|
|
450
|
+
|
|
451
|
+
@property
|
|
452
|
+
def volume(self) -> int:
|
|
453
|
+
"""Return the volume."""
|
|
454
|
+
return self._attrs["settings"].get("doorbell_volume", 0)
|
|
455
|
+
|
|
456
|
+
async def async_set_volume(self, value: int) -> None:
|
|
457
|
+
"""Set the volume."""
|
|
458
|
+
if not (
|
|
459
|
+
(isinstance(value, int)) and (DOORBELL_VOL_MIN <= value <= DOORBELL_VOL_MAX)
|
|
460
|
+
):
|
|
461
|
+
raise RingError(MSG_VOL_OUTBOUND.format(DOORBELL_VOL_MIN, DOORBELL_VOL_MAX))
|
|
462
|
+
|
|
463
|
+
params = {
|
|
464
|
+
"doorbot[description]": self.name,
|
|
465
|
+
"doorbot[settings][doorbell_volume]": str(value),
|
|
466
|
+
}
|
|
467
|
+
url = DOORBELLS_ENDPOINT.format(self.device_api_id)
|
|
468
|
+
await self._ring.async_query(url, extra_params=params, method="PUT")
|
|
469
|
+
|
|
470
|
+
@property
|
|
471
|
+
def connection_status(self) -> str | None:
|
|
472
|
+
"""Return connection status."""
|
|
473
|
+
if alerts := self._attrs.get("alerts"):
|
|
474
|
+
return alerts.get("connection")
|
|
475
|
+
return None
|
|
476
|
+
|
|
477
|
+
@deprecated("Use async_take_snapshot() instead")
|
|
478
|
+
async def async_get_snapshot(
|
|
479
|
+
self, retries: int = 3, delay: int = 1, filename: str | None = None
|
|
480
|
+
) -> bytes | None:
|
|
481
|
+
"""Take a snapshot and download it."""
|
|
482
|
+
url = SNAPSHOT_TIMESTAMP_ENDPOINT
|
|
483
|
+
payload = {"doorbot_ids": [self._attrs.get("id")]}
|
|
484
|
+
await self._ring.async_query(url, method="POST", json=payload)
|
|
485
|
+
request_time = time.time()
|
|
486
|
+
for _ in range(retries):
|
|
487
|
+
await asyncio.sleep(delay)
|
|
488
|
+
resp = await self._ring.async_query(url, method="POST", json=payload)
|
|
489
|
+
response = resp.json()
|
|
490
|
+
if response["timestamps"][0]["timestamp"] / 1000 > request_time:
|
|
491
|
+
resp = await self._ring.async_query(
|
|
492
|
+
SNAPSHOT_ENDPOINT.format(self._attrs.get("id"))
|
|
493
|
+
)
|
|
494
|
+
snapshot = resp.content
|
|
495
|
+
if filename:
|
|
496
|
+
async with aiofiles.open(filename, "wb") as jpg:
|
|
497
|
+
await jpg.write(snapshot)
|
|
498
|
+
return None
|
|
499
|
+
return snapshot
|
|
500
|
+
return None
|
|
501
|
+
|
|
502
|
+
def _motion_detection_state(self) -> bool | None:
|
|
503
|
+
if settings := self._attrs.get("settings"):
|
|
504
|
+
return settings.get("motion_detection_enabled")
|
|
505
|
+
return None
|
|
506
|
+
|
|
507
|
+
@property
|
|
508
|
+
def motion_detection(self) -> bool:
|
|
509
|
+
"""Return motion detection enabled state."""
|
|
510
|
+
return state if (state := self._motion_detection_state()) else False
|
|
511
|
+
|
|
512
|
+
async def async_set_motion_detection(self, state: bool) -> None: # noqa: FBT001
|
|
513
|
+
"""Set the motion detection enabled state."""
|
|
514
|
+
values = [True, False]
|
|
515
|
+
if state not in values:
|
|
516
|
+
raise RingError(MSG_ALLOWED_VALUES.format("True, False"))
|
|
517
|
+
|
|
518
|
+
if self._motion_detection_state() is None:
|
|
519
|
+
_LOGGER.warning(
|
|
520
|
+
"%s",
|
|
521
|
+
MSG_EXPECTED_ATTRIBUTE_NOT_FOUND.format(
|
|
522
|
+
"settings[motion_detection_enabled]"
|
|
523
|
+
),
|
|
524
|
+
)
|
|
525
|
+
return
|
|
526
|
+
|
|
527
|
+
url = SETTINGS_ENDPOINT.format(self.device_api_id)
|
|
528
|
+
payload = {"motion_settings": {"motion_detection_enabled": state}}
|
|
529
|
+
|
|
530
|
+
await self._ring.async_query(url, method="PATCH", json=payload)
|
|
531
|
+
|
|
532
|
+
DEPRECATED_API_QUERIES: ClassVar = {
|
|
533
|
+
*RingGeneric.DEPRECATED_API_QUERIES,
|
|
534
|
+
"update_health_data",
|
|
535
|
+
"recording_download",
|
|
536
|
+
"recording_url",
|
|
537
|
+
"get_snapshot",
|
|
538
|
+
}
|
|
539
|
+
DEPRECATED_API_PROPERTY_GETTERS: ClassVar = {
|
|
540
|
+
*RingGeneric.DEPRECATED_API_PROPERTY_GETTERS,
|
|
541
|
+
"last_recording_id",
|
|
542
|
+
"live_streaming_json",
|
|
543
|
+
}
|
|
544
|
+
DEPRECATED_API_PROPERTY_SETTERS: ClassVar = {
|
|
545
|
+
*RingGeneric.DEPRECATED_API_PROPERTY_SETTERS,
|
|
546
|
+
"existing_doorbell_type",
|
|
547
|
+
"existing_doorbell_type_enabled",
|
|
548
|
+
"existing_doorbell_type_duration",
|
|
549
|
+
"volume",
|
|
550
|
+
"motion_detection",
|
|
551
|
+
}
|
python_ring/event.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Module for ring events."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any, NamedTuple
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class RingEvent:
|
|
11
|
+
"""Class for ring events."""
|
|
12
|
+
|
|
13
|
+
id: int
|
|
14
|
+
doorbot_id: int
|
|
15
|
+
device_name: str
|
|
16
|
+
device_kind: str
|
|
17
|
+
now: float
|
|
18
|
+
expires_in: float
|
|
19
|
+
kind: str
|
|
20
|
+
state: str
|
|
21
|
+
is_update: bool = False
|
|
22
|
+
detection_type: str | None = None
|
|
23
|
+
|
|
24
|
+
def __getitem__(self, key: str) -> Any:
|
|
25
|
+
"""Get a value by string."""
|
|
26
|
+
return getattr(self, key)
|
|
27
|
+
|
|
28
|
+
def get(self, key: str) -> Any | None:
|
|
29
|
+
"""Get a value by string and return None if not present."""
|
|
30
|
+
return getattr(self, key) if hasattr(self, key) else None
|
|
31
|
+
|
|
32
|
+
def get_key(self) -> RingEventKey:
|
|
33
|
+
"""Return the identificationkey for the event."""
|
|
34
|
+
return RingEventKey(self.id, self.doorbot_id, self.kind, self.now)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RingEventKey(NamedTuple):
|
|
38
|
+
"""Class to identify an event.
|
|
39
|
+
|
|
40
|
+
Used for determining if messages are updates to events.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
id: int
|
|
44
|
+
doorbot_id: int
|
|
45
|
+
kind: str
|
|
46
|
+
now: float
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""python-ring exceptions."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class RingError(Exception):
|
|
5
|
+
"""Base exception for device errors."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Requires2FAError(RingError):
|
|
9
|
+
"""Exception that 2FA is required."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class AuthenticationError(RingError):
|
|
13
|
+
"""Exception for ring authentication errors."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RingTimeout(RingError): # noqa: N818
|
|
17
|
+
"""Exception for ring authentication errors."""
|