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
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# vim:sw=4:ts=4:et:
|
|
2
|
+
"""Python Ring Doorbell wrapper."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from typing import ClassVar
|
|
8
|
+
|
|
9
|
+
from python_ring.const import (
|
|
10
|
+
ELITE_CAM_140_KINDS,
|
|
11
|
+
ELITE_CAM_360_KINDS,
|
|
12
|
+
FLOODLIGHT_CAM_2K_KINDS,
|
|
13
|
+
FLOODLIGHT_CAM_KINDS,
|
|
14
|
+
FLOODLIGHT_CAM_PLUS_KINDS,
|
|
15
|
+
FLOODLIGHT_CAM_PRO_2_KINDS,
|
|
16
|
+
FLOODLIGHT_CAM_PRO_KINDS,
|
|
17
|
+
INDOOR_CAM_GEN2_KINDS,
|
|
18
|
+
INDOOR_CAM_KINDS,
|
|
19
|
+
INDOOR_CAM_PLUS_KINDS,
|
|
20
|
+
INDOOR_CAM_PTZ_KINDS,
|
|
21
|
+
LIGHTS_ENDPOINT,
|
|
22
|
+
MSG_ALLOWED_VALUES,
|
|
23
|
+
MSG_VOL_OUTBOUND,
|
|
24
|
+
OUTDOOR_CAM_PLUS_KINDS,
|
|
25
|
+
OUTDOOR_CAM_PRO_KINDS,
|
|
26
|
+
SIREN_DURATION_MAX,
|
|
27
|
+
SIREN_DURATION_MIN,
|
|
28
|
+
SIREN_ENDPOINT,
|
|
29
|
+
SPOTLIGHT_CAM_4K_PRO_KINDS,
|
|
30
|
+
SPOTLIGHT_CAM_BATTERY_KINDS,
|
|
31
|
+
SPOTLIGHT_CAM_PLUS_KINDS,
|
|
32
|
+
SPOTLIGHT_CAM_PRO_KINDS,
|
|
33
|
+
SPOTLIGHT_CAM_WIRED_KINDS,
|
|
34
|
+
STICKUP_CAM_BATTERY_KINDS,
|
|
35
|
+
STICKUP_CAM_ELITE_KINDS,
|
|
36
|
+
STICKUP_CAM_GEN3_KINDS,
|
|
37
|
+
STICKUP_CAM_KINDS,
|
|
38
|
+
RingCapability,
|
|
39
|
+
)
|
|
40
|
+
from python_ring.doorbot import RingDoorBell
|
|
41
|
+
from python_ring.exceptions import RingError
|
|
42
|
+
|
|
43
|
+
_LOGGER = logging.getLogger(__name__)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class RingStickUpCam(RingDoorBell):
|
|
47
|
+
"""Implementation for RingStickUpCam."""
|
|
48
|
+
|
|
49
|
+
def _reports_battery(self) -> bool:
|
|
50
|
+
"""Return if a kind that is sold with and without battery has one."""
|
|
51
|
+
if "battery_present" in self._attrs.get("health", {}):
|
|
52
|
+
return bool(self._attrs["health"]["battery_present"])
|
|
53
|
+
return self.battery_life is not None
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def family(self) -> str:
|
|
57
|
+
"""Return Ring device family type."""
|
|
58
|
+
return "stickup_cams"
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def model(self) -> str: # noqa: C901, PLR0911, PLR0912
|
|
62
|
+
"""Return Ring device model name."""
|
|
63
|
+
if self.kind in FLOODLIGHT_CAM_KINDS:
|
|
64
|
+
return "Floodlight Cam"
|
|
65
|
+
if self.kind in FLOODLIGHT_CAM_PRO_KINDS:
|
|
66
|
+
return "Floodlight Cam Pro"
|
|
67
|
+
if self.kind in FLOODLIGHT_CAM_PLUS_KINDS:
|
|
68
|
+
return "Floodlight Cam Plus"
|
|
69
|
+
if self.kind in FLOODLIGHT_CAM_2K_KINDS:
|
|
70
|
+
return "Floodlight Cam 2K"
|
|
71
|
+
if self.kind in FLOODLIGHT_CAM_PRO_2_KINDS:
|
|
72
|
+
return "Floodlight Cam Pro (2nd Gen)"
|
|
73
|
+
if self.kind in INDOOR_CAM_KINDS:
|
|
74
|
+
return "Indoor Cam"
|
|
75
|
+
if self.kind in INDOOR_CAM_GEN2_KINDS:
|
|
76
|
+
return "Indoor Cam (2nd Gen)"
|
|
77
|
+
if self.kind in INDOOR_CAM_PTZ_KINDS:
|
|
78
|
+
return "Pan-Tilt Indoor Cam"
|
|
79
|
+
if self.kind in INDOOR_CAM_PLUS_KINDS:
|
|
80
|
+
return "Indoor Cam Plus"
|
|
81
|
+
if self.kind in OUTDOOR_CAM_PLUS_KINDS:
|
|
82
|
+
return "Outdoor Cam Plus"
|
|
83
|
+
if self.kind in OUTDOOR_CAM_PRO_KINDS:
|
|
84
|
+
return "Outdoor Cam Pro"
|
|
85
|
+
if self.kind in SPOTLIGHT_CAM_BATTERY_KINDS:
|
|
86
|
+
return "Spotlight Cam {}".format(
|
|
87
|
+
self._attrs.get("ring_cam_setup_flow", "battery").title()
|
|
88
|
+
)
|
|
89
|
+
if self.kind in SPOTLIGHT_CAM_WIRED_KINDS:
|
|
90
|
+
return "Spotlight Cam {}".format(
|
|
91
|
+
self._attrs.get("ring_cam_setup_flow", "wired").title()
|
|
92
|
+
)
|
|
93
|
+
if self.kind in SPOTLIGHT_CAM_PLUS_KINDS:
|
|
94
|
+
return "Spotlight Cam Plus"
|
|
95
|
+
if self.kind in SPOTLIGHT_CAM_PRO_KINDS:
|
|
96
|
+
return "Spotlight Cam Pro"
|
|
97
|
+
if self.kind in SPOTLIGHT_CAM_4K_PRO_KINDS:
|
|
98
|
+
return "Spotlight Cam 4K Pro"
|
|
99
|
+
if self.kind in STICKUP_CAM_KINDS:
|
|
100
|
+
return "Stick Up Cam"
|
|
101
|
+
if self.kind in STICKUP_CAM_BATTERY_KINDS:
|
|
102
|
+
return "Stick Up Cam Battery"
|
|
103
|
+
if self.kind in STICKUP_CAM_ELITE_KINDS:
|
|
104
|
+
return "Stick Up Cam Wired"
|
|
105
|
+
if self.kind in STICKUP_CAM_GEN3_KINDS:
|
|
106
|
+
return "Stick Up Cam (3rd Gen)"
|
|
107
|
+
if self.kind in ELITE_CAM_140_KINDS:
|
|
108
|
+
return "Elite 140 4K"
|
|
109
|
+
if self.kind in ELITE_CAM_360_KINDS:
|
|
110
|
+
return "Elite 360 4K"
|
|
111
|
+
_LOGGER.error("Unknown kind: %s", self.kind)
|
|
112
|
+
return "Unknown Stickup Cam"
|
|
113
|
+
|
|
114
|
+
def has_capability(self, capability: RingCapability | str) -> bool: # noqa: PLR0911
|
|
115
|
+
"""Return if device has specific capability."""
|
|
116
|
+
capability = (
|
|
117
|
+
capability
|
|
118
|
+
if isinstance(capability, RingCapability)
|
|
119
|
+
else RingCapability.from_name(capability)
|
|
120
|
+
)
|
|
121
|
+
if capability == RingCapability.HISTORY:
|
|
122
|
+
return True
|
|
123
|
+
if capability == RingCapability.BATTERY:
|
|
124
|
+
if self.kind in (
|
|
125
|
+
INDOOR_CAM_PLUS_KINDS + OUTDOOR_CAM_PLUS_KINDS + OUTDOOR_CAM_PRO_KINDS
|
|
126
|
+
):
|
|
127
|
+
return self._reports_battery()
|
|
128
|
+
return self.kind in (
|
|
129
|
+
SPOTLIGHT_CAM_BATTERY_KINDS
|
|
130
|
+
+ STICKUP_CAM_KINDS
|
|
131
|
+
+ STICKUP_CAM_BATTERY_KINDS
|
|
132
|
+
+ STICKUP_CAM_GEN3_KINDS
|
|
133
|
+
)
|
|
134
|
+
if capability == RingCapability.LIGHT:
|
|
135
|
+
return self.kind in (
|
|
136
|
+
FLOODLIGHT_CAM_KINDS
|
|
137
|
+
+ FLOODLIGHT_CAM_PRO_KINDS
|
|
138
|
+
+ FLOODLIGHT_CAM_PLUS_KINDS
|
|
139
|
+
+ FLOODLIGHT_CAM_2K_KINDS
|
|
140
|
+
+ SPOTLIGHT_CAM_BATTERY_KINDS
|
|
141
|
+
+ SPOTLIGHT_CAM_WIRED_KINDS
|
|
142
|
+
+ SPOTLIGHT_CAM_PLUS_KINDS
|
|
143
|
+
+ SPOTLIGHT_CAM_PRO_KINDS
|
|
144
|
+
+ FLOODLIGHT_CAM_PRO_2_KINDS
|
|
145
|
+
+ SPOTLIGHT_CAM_4K_PRO_KINDS
|
|
146
|
+
)
|
|
147
|
+
if capability == RingCapability.SIREN:
|
|
148
|
+
return self.kind in (
|
|
149
|
+
FLOODLIGHT_CAM_KINDS
|
|
150
|
+
+ FLOODLIGHT_CAM_PRO_KINDS
|
|
151
|
+
+ FLOODLIGHT_CAM_PLUS_KINDS
|
|
152
|
+
+ FLOODLIGHT_CAM_2K_KINDS
|
|
153
|
+
+ INDOOR_CAM_KINDS
|
|
154
|
+
+ INDOOR_CAM_GEN2_KINDS
|
|
155
|
+
+ INDOOR_CAM_PTZ_KINDS
|
|
156
|
+
+ SPOTLIGHT_CAM_BATTERY_KINDS
|
|
157
|
+
+ SPOTLIGHT_CAM_WIRED_KINDS
|
|
158
|
+
+ SPOTLIGHT_CAM_PLUS_KINDS
|
|
159
|
+
+ SPOTLIGHT_CAM_PRO_KINDS
|
|
160
|
+
+ STICKUP_CAM_BATTERY_KINDS
|
|
161
|
+
+ STICKUP_CAM_ELITE_KINDS
|
|
162
|
+
+ STICKUP_CAM_GEN3_KINDS
|
|
163
|
+
+ ELITE_CAM_140_KINDS
|
|
164
|
+
+ ELITE_CAM_360_KINDS
|
|
165
|
+
+ FLOODLIGHT_CAM_PRO_2_KINDS
|
|
166
|
+
+ INDOOR_CAM_PLUS_KINDS
|
|
167
|
+
+ OUTDOOR_CAM_PLUS_KINDS
|
|
168
|
+
+ OUTDOOR_CAM_PRO_KINDS
|
|
169
|
+
+ SPOTLIGHT_CAM_4K_PRO_KINDS
|
|
170
|
+
)
|
|
171
|
+
if capability in [RingCapability.MOTION_DETECTION, RingCapability.VIDEO]:
|
|
172
|
+
return self.kind in (
|
|
173
|
+
FLOODLIGHT_CAM_KINDS
|
|
174
|
+
+ FLOODLIGHT_CAM_PRO_KINDS
|
|
175
|
+
+ FLOODLIGHT_CAM_PLUS_KINDS
|
|
176
|
+
+ FLOODLIGHT_CAM_2K_KINDS
|
|
177
|
+
+ INDOOR_CAM_KINDS
|
|
178
|
+
+ INDOOR_CAM_GEN2_KINDS
|
|
179
|
+
+ INDOOR_CAM_PTZ_KINDS
|
|
180
|
+
+ SPOTLIGHT_CAM_BATTERY_KINDS
|
|
181
|
+
+ SPOTLIGHT_CAM_WIRED_KINDS
|
|
182
|
+
+ SPOTLIGHT_CAM_PLUS_KINDS
|
|
183
|
+
+ SPOTLIGHT_CAM_PRO_KINDS
|
|
184
|
+
+ STICKUP_CAM_KINDS
|
|
185
|
+
+ STICKUP_CAM_BATTERY_KINDS
|
|
186
|
+
+ STICKUP_CAM_ELITE_KINDS
|
|
187
|
+
+ STICKUP_CAM_GEN3_KINDS
|
|
188
|
+
+ ELITE_CAM_140_KINDS
|
|
189
|
+
+ ELITE_CAM_360_KINDS
|
|
190
|
+
+ FLOODLIGHT_CAM_PRO_2_KINDS
|
|
191
|
+
+ INDOOR_CAM_PLUS_KINDS
|
|
192
|
+
+ OUTDOOR_CAM_PLUS_KINDS
|
|
193
|
+
+ OUTDOOR_CAM_PRO_KINDS
|
|
194
|
+
+ SPOTLIGHT_CAM_4K_PRO_KINDS
|
|
195
|
+
)
|
|
196
|
+
return False
|
|
197
|
+
|
|
198
|
+
@property
|
|
199
|
+
def lights(self) -> str:
|
|
200
|
+
"""Return lights status."""
|
|
201
|
+
return self._attrs.get("led_status", "")
|
|
202
|
+
|
|
203
|
+
async def async_set_lights(self, state: str) -> None:
|
|
204
|
+
"""Control the lights."""
|
|
205
|
+
values = ["on", "off"]
|
|
206
|
+
if state not in values:
|
|
207
|
+
raise RingError(MSG_ALLOWED_VALUES.format(", ".join(values)))
|
|
208
|
+
|
|
209
|
+
url = LIGHTS_ENDPOINT.format(self.device_api_id, state)
|
|
210
|
+
await self._ring.async_query(url, method="PUT")
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def light(self) -> bool:
|
|
214
|
+
"""Return lights status."""
|
|
215
|
+
return self._attrs.get("led_status", "") == "on"
|
|
216
|
+
|
|
217
|
+
async def async_set_light(self, value: bool) -> None: # noqa: FBT001
|
|
218
|
+
"""Control the lights."""
|
|
219
|
+
state = "on" if value else "off"
|
|
220
|
+
url = LIGHTS_ENDPOINT.format(self.device_api_id, state)
|
|
221
|
+
await self._ring.async_query(url, method="PUT")
|
|
222
|
+
|
|
223
|
+
@property
|
|
224
|
+
def siren(self) -> int:
|
|
225
|
+
"""Return siren status."""
|
|
226
|
+
if siren_status := self._attrs.get("siren_status"):
|
|
227
|
+
return siren_status.get("seconds_remaining", 0)
|
|
228
|
+
return 0
|
|
229
|
+
|
|
230
|
+
async def async_set_siren(self, duration: int) -> None:
|
|
231
|
+
"""Control the siren."""
|
|
232
|
+
if not (
|
|
233
|
+
(isinstance(duration, int))
|
|
234
|
+
and (SIREN_DURATION_MIN <= duration <= SIREN_DURATION_MAX)
|
|
235
|
+
):
|
|
236
|
+
raise RingError(
|
|
237
|
+
MSG_VOL_OUTBOUND.format(SIREN_DURATION_MIN, SIREN_DURATION_MAX)
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
if duration > 0:
|
|
241
|
+
state = "on"
|
|
242
|
+
params = {"duration": duration}
|
|
243
|
+
else:
|
|
244
|
+
state = "off"
|
|
245
|
+
params = {}
|
|
246
|
+
url = SIREN_ENDPOINT.format(self.device_api_id, state)
|
|
247
|
+
await self._ring.async_query(url, extra_params=params, method="PUT")
|
|
248
|
+
|
|
249
|
+
DEPRECATED_API_PROPERTY_SETTERS: ClassVar = {
|
|
250
|
+
*RingDoorBell.DEPRECATED_API_PROPERTY_SETTERS,
|
|
251
|
+
"lights",
|
|
252
|
+
"siren",
|
|
253
|
+
}
|
python_ring/util.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Module for common utility functions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import datetime
|
|
7
|
+
import logging
|
|
8
|
+
from contextlib import suppress
|
|
9
|
+
from functools import update_wrapper
|
|
10
|
+
from threading import Lock
|
|
11
|
+
from typing import TYPE_CHECKING, Any, Callable
|
|
12
|
+
from warnings import warn
|
|
13
|
+
|
|
14
|
+
from typing_extensions import ParamSpec, TypeVar
|
|
15
|
+
|
|
16
|
+
from python_ring.exceptions import RingError
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from collections.abc import Coroutine
|
|
20
|
+
|
|
21
|
+
from .auth import Auth
|
|
22
|
+
from .generic import RingGeneric
|
|
23
|
+
from .group import RingLightGroup
|
|
24
|
+
from .listen.eventlistener import RingEventListener
|
|
25
|
+
from .ring import Ring
|
|
26
|
+
|
|
27
|
+
_T = TypeVar(
|
|
28
|
+
"_T", bound=Auth | Ring | RingGeneric | RingLightGroup | RingEventListener
|
|
29
|
+
)
|
|
30
|
+
_R = TypeVar("_R")
|
|
31
|
+
_P = ParamSpec("_P")
|
|
32
|
+
|
|
33
|
+
_logger = logging.getLogger(__name__)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def parse_datetime(datetime_str: str) -> datetime.datetime:
|
|
37
|
+
"""Parse a datetime string into a datetime object.
|
|
38
|
+
|
|
39
|
+
Ring api has inconsistent datetime string patterns.
|
|
40
|
+
"""
|
|
41
|
+
# Check if the datetime string contains a period which precedes 'Z',
|
|
42
|
+
# indicating microseconds
|
|
43
|
+
if "." in datetime_str and datetime_str.endswith("Z"):
|
|
44
|
+
# String contains microseconds and ends with 'Z'
|
|
45
|
+
format_str = "%Y-%m-%dT%H:%M:%S.%fZ"
|
|
46
|
+
else:
|
|
47
|
+
# String does not contain microseconds, should end with 'Z'
|
|
48
|
+
# Could be updated to handle other formats
|
|
49
|
+
format_str = "%Y-%m-%dT%H:%M:%SZ"
|
|
50
|
+
try:
|
|
51
|
+
res = datetime.datetime.strptime(datetime_str, format_str).replace(
|
|
52
|
+
tzinfo=datetime.timezone.utc
|
|
53
|
+
)
|
|
54
|
+
except ValueError:
|
|
55
|
+
_logger.exception(
|
|
56
|
+
"Unable to parse datetime string %s, defaulting to now time", datetime_str
|
|
57
|
+
)
|
|
58
|
+
res = datetime.datetime.now(datetime.timezone.utc)
|
|
59
|
+
return res
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _DeprecatedSyncApiHandler:
|
|
63
|
+
def __init__(self, auth: Auth) -> None:
|
|
64
|
+
self.auth = auth
|
|
65
|
+
self._sync_lock = Lock()
|
|
66
|
+
|
|
67
|
+
async def run_and_close_session(
|
|
68
|
+
self,
|
|
69
|
+
async_method: Callable[_P, Coroutine[Any, Any, _R]],
|
|
70
|
+
*args: _P.args,
|
|
71
|
+
**kwargs: _P.kwargs,
|
|
72
|
+
) -> _R:
|
|
73
|
+
try:
|
|
74
|
+
self._sync_lock.acquire()
|
|
75
|
+
res = await async_method(*args, **kwargs)
|
|
76
|
+
finally:
|
|
77
|
+
with suppress(Exception):
|
|
78
|
+
await self.auth.async_close()
|
|
79
|
+
self._sync_lock.release()
|
|
80
|
+
|
|
81
|
+
return res
|
|
82
|
+
|
|
83
|
+
@staticmethod
|
|
84
|
+
def check_no_loop(classname: str, method_name: str) -> None:
|
|
85
|
+
current_loop = None
|
|
86
|
+
with suppress(RuntimeError):
|
|
87
|
+
current_loop = asyncio.get_running_loop()
|
|
88
|
+
if current_loop:
|
|
89
|
+
msg = (
|
|
90
|
+
f"You cannot call deprecated sync function {classname}.{method_name} "
|
|
91
|
+
"from within a running event loop."
|
|
92
|
+
)
|
|
93
|
+
raise RingError(msg)
|
|
94
|
+
|
|
95
|
+
def get_api_query(
|
|
96
|
+
self,
|
|
97
|
+
class_instance: _T,
|
|
98
|
+
method_name: str,
|
|
99
|
+
) -> Any:
|
|
100
|
+
"""Return deprecated sync api query attribute."""
|
|
101
|
+
classname = type(class_instance).__name__
|
|
102
|
+
|
|
103
|
+
def _deprecated_sync_function(
|
|
104
|
+
async_func: Callable[_P, Coroutine[Any, Any, _R]],
|
|
105
|
+
) -> Callable[_P, _R]:
|
|
106
|
+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
107
|
+
self.check_no_loop(classname, method_name)
|
|
108
|
+
msg = (
|
|
109
|
+
f"{classname}.{method_name} is deprecated, use "
|
|
110
|
+
f"{classname}.{async_method_name}"
|
|
111
|
+
)
|
|
112
|
+
warn(msg, DeprecationWarning, stacklevel=1)
|
|
113
|
+
return asyncio.run(
|
|
114
|
+
self.run_and_close_session(async_func, *args, **kwargs)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
return update_wrapper(wrapper, async_func)
|
|
118
|
+
|
|
119
|
+
async_method_name = f"async_{method_name}"
|
|
120
|
+
async_method = getattr(class_instance, async_method_name)
|
|
121
|
+
return _deprecated_sync_function(async_method)
|
|
122
|
+
|
|
123
|
+
def get_api_property(
|
|
124
|
+
self,
|
|
125
|
+
class_instance: _T,
|
|
126
|
+
method_name: str,
|
|
127
|
+
) -> Any:
|
|
128
|
+
"""Return deprecated sync api property value."""
|
|
129
|
+
classname = type(class_instance).__name__
|
|
130
|
+
self.check_no_loop(classname, method_name)
|
|
131
|
+
async_method_name = f"async_get_{method_name}"
|
|
132
|
+
msg = (
|
|
133
|
+
f"{classname}.{method_name} is deprecated, use "
|
|
134
|
+
f"{classname}.{async_method_name}"
|
|
135
|
+
)
|
|
136
|
+
warn(msg, DeprecationWarning, stacklevel=1)
|
|
137
|
+
async_method = getattr(class_instance, async_method_name)
|
|
138
|
+
return asyncio.run(self.run_and_close_session(async_method))
|
|
139
|
+
|
|
140
|
+
def set_api_property(
|
|
141
|
+
self,
|
|
142
|
+
class_instance: _T,
|
|
143
|
+
property_name: str,
|
|
144
|
+
value: Any,
|
|
145
|
+
) -> None:
|
|
146
|
+
"""Set sync api property value."""
|
|
147
|
+
classname = type(class_instance).__name__
|
|
148
|
+
|
|
149
|
+
self.check_no_loop(classname, property_name)
|
|
150
|
+
async_method_name = f"async_set_{property_name}"
|
|
151
|
+
msg = (
|
|
152
|
+
f"{classname}.{property_name} is deprecated, use "
|
|
153
|
+
f"{classname}.{async_method_name}"
|
|
154
|
+
)
|
|
155
|
+
warn(msg, DeprecationWarning, stacklevel=1)
|
|
156
|
+
async_method = getattr(class_instance, async_method_name)
|
|
157
|
+
asyncio.run(self.run_and_close_session(async_method, value))
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""WebRTC live-stream support shared by Ring devices that offer live view."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
import logging
|
|
7
|
+
import time
|
|
8
|
+
from typing import TYPE_CHECKING, Any
|
|
9
|
+
|
|
10
|
+
import aiofiles
|
|
11
|
+
|
|
12
|
+
from python_ring.const import API_URI_SNAPSHOT, ICE_SERVERS, TAKE_SNAPSHOT_ENDPOINT
|
|
13
|
+
from python_ring.exceptions import RingError
|
|
14
|
+
from python_ring.webrtcstream import RingWebRtcMessageCallback, RingWebRtcStream
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from python_ring.ring import Ring
|
|
18
|
+
|
|
19
|
+
_LOGGER = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class RingWebRtcMixin:
|
|
23
|
+
"""WebRTC stream management for devices that support live view.
|
|
24
|
+
|
|
25
|
+
Used by doorbells/cameras and by intercoms (``RingOther``), which use the
|
|
26
|
+
same signalling as cameras.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
_ring: Ring
|
|
30
|
+
device_api_id: int
|
|
31
|
+
_webrtc_streams: dict[str, RingWebRtcStream]
|
|
32
|
+
_attrs: dict[str, Any]
|
|
33
|
+
|
|
34
|
+
async def async_take_snapshot(
|
|
35
|
+
self, max_age: int = 30, max_wait: int = 10, filename: str | None = None
|
|
36
|
+
) -> bytes | None:
|
|
37
|
+
"""
|
|
38
|
+
Take a snapshot and download it.
|
|
39
|
+
|
|
40
|
+
If a snapshot already exists which is less that max_age seconds old, it will
|
|
41
|
+
be returned immediately. Otherwise, we'll wait at most max_wait seconds for
|
|
42
|
+
a fresh one before giving up. A 404 response from this method typically
|
|
43
|
+
indicates a server-side timeout (try increasing max_wait).
|
|
44
|
+
"""
|
|
45
|
+
request_time_s = time.time()
|
|
46
|
+
oldest_ms = int(request_time_s - max_age) * 1000
|
|
47
|
+
|
|
48
|
+
params = {
|
|
49
|
+
"after-ms": oldest_ms,
|
|
50
|
+
"max-wait-ms": max_wait * 1000,
|
|
51
|
+
"extras": "force",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
resp = await self._ring.async_query(
|
|
55
|
+
TAKE_SNAPSHOT_ENDPOINT.format(self._attrs.get("id")),
|
|
56
|
+
extra_params=params,
|
|
57
|
+
base_uri=API_URI_SNAPSHOT,
|
|
58
|
+
timeout=max_wait + 1,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
snapshot = resp.content
|
|
62
|
+
if filename:
|
|
63
|
+
async with aiofiles.open(filename, "wb") as jpg:
|
|
64
|
+
await jpg.write(snapshot)
|
|
65
|
+
return None
|
|
66
|
+
return snapshot
|
|
67
|
+
|
|
68
|
+
async def generate_webrtc_stream(
|
|
69
|
+
self, sdp_offer: str, *, keep_alive_timeout: int | None = 30
|
|
70
|
+
) -> str:
|
|
71
|
+
"""Generate the rtc stream."""
|
|
72
|
+
if session_id := RingWebRtcStream.get_sdp_session_id(sdp_offer):
|
|
73
|
+
|
|
74
|
+
async def _close_callback() -> None:
|
|
75
|
+
# The stream is already closing itself; only forget it.
|
|
76
|
+
self._webrtc_streams.pop(session_id, None)
|
|
77
|
+
|
|
78
|
+
stream = RingWebRtcStream(
|
|
79
|
+
self._ring,
|
|
80
|
+
self.device_api_id,
|
|
81
|
+
keep_alive_timeout=keep_alive_timeout,
|
|
82
|
+
on_close_callback=_close_callback,
|
|
83
|
+
)
|
|
84
|
+
# Register first so ICE candidates arriving during generate reach
|
|
85
|
+
# the stream (trickle-ICE race).
|
|
86
|
+
self._webrtc_streams[session_id] = stream
|
|
87
|
+
try:
|
|
88
|
+
sdp_answer = await stream.generate(sdp_offer)
|
|
89
|
+
# generate will raise if no sdp answer as no callback passed
|
|
90
|
+
assert sdp_answer # noqa: S101
|
|
91
|
+
except BaseException:
|
|
92
|
+
await self._discard_webrtc_stream(session_id, stream)
|
|
93
|
+
raise
|
|
94
|
+
return sdp_answer
|
|
95
|
+
msg = "Unable to generate the stream, could not extract session id from offer."
|
|
96
|
+
raise RingError(msg)
|
|
97
|
+
|
|
98
|
+
async def generate_async_webrtc_stream(
|
|
99
|
+
self,
|
|
100
|
+
sdp_offer: str,
|
|
101
|
+
session_id: str,
|
|
102
|
+
on_message_callback: RingWebRtcMessageCallback,
|
|
103
|
+
*,
|
|
104
|
+
keep_alive_timeout: int | None = 60 * 5,
|
|
105
|
+
) -> None:
|
|
106
|
+
"""Generate the rtc stream. Will callback with answers and ICE candidates."""
|
|
107
|
+
|
|
108
|
+
async def _close_callback() -> None:
|
|
109
|
+
# Called from the stream's own reader task when Ring ends the
|
|
110
|
+
# session. The stream is already closing itself, so only forget
|
|
111
|
+
# it: calling close() again would re-enter _close().
|
|
112
|
+
self._webrtc_streams.pop(session_id, None)
|
|
113
|
+
|
|
114
|
+
stream = RingWebRtcStream(
|
|
115
|
+
self._ring,
|
|
116
|
+
self.device_api_id,
|
|
117
|
+
on_message_callback=on_message_callback,
|
|
118
|
+
keep_alive_timeout=keep_alive_timeout,
|
|
119
|
+
on_close_callback=_close_callback,
|
|
120
|
+
)
|
|
121
|
+
self._webrtc_streams[session_id] = stream
|
|
122
|
+
try:
|
|
123
|
+
await stream.generate(sdp_offer)
|
|
124
|
+
except BaseException:
|
|
125
|
+
await self._discard_webrtc_stream(session_id, stream)
|
|
126
|
+
raise
|
|
127
|
+
|
|
128
|
+
async def _discard_webrtc_stream(
|
|
129
|
+
self, session_id: str, stream: RingWebRtcStream
|
|
130
|
+
) -> None:
|
|
131
|
+
"""Forget and close a stream whose generation failed or was cancelled."""
|
|
132
|
+
self._webrtc_streams.pop(session_id, None)
|
|
133
|
+
with contextlib.suppress(Exception):
|
|
134
|
+
await stream.close()
|
|
135
|
+
|
|
136
|
+
async def on_webrtc_candidate(
|
|
137
|
+
self, session_id: str, candidate: str, multi_line_index: int
|
|
138
|
+
) -> None:
|
|
139
|
+
"""Send an ICE candidate."""
|
|
140
|
+
if stream := self._webrtc_streams.get(session_id):
|
|
141
|
+
await stream.on_ice_candidate(candidate, multi_line_index)
|
|
142
|
+
else:
|
|
143
|
+
_LOGGER.debug(
|
|
144
|
+
"Ignoring ICE candidate for unknown or closed WebRTC session %s",
|
|
145
|
+
session_id,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
async def close_webrtc_stream(self, session_id: str) -> None:
|
|
149
|
+
"""Close the rtc stream."""
|
|
150
|
+
stream = self._webrtc_streams.pop(session_id, None)
|
|
151
|
+
if stream:
|
|
152
|
+
await stream.close()
|
|
153
|
+
|
|
154
|
+
def sync_close_webrtc_stream(self, session_id: str) -> None:
|
|
155
|
+
"""Close the rtc stream."""
|
|
156
|
+
stream = self._webrtc_streams.pop(session_id, None)
|
|
157
|
+
if stream:
|
|
158
|
+
stream.sync_close()
|
|
159
|
+
|
|
160
|
+
async def keep_alive_webrtc_stream(self, sdp_session_id: str) -> None:
|
|
161
|
+
"""Keep alive the rtc stream."""
|
|
162
|
+
stream = self._webrtc_streams.get(sdp_session_id, None)
|
|
163
|
+
if stream:
|
|
164
|
+
await stream.keep_alive()
|
|
165
|
+
|
|
166
|
+
def get_ice_servers(self) -> list[str]:
|
|
167
|
+
"""Return the ICE servers."""
|
|
168
|
+
return ICE_SERVERS
|