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/other.py ADDED
@@ -0,0 +1,298 @@
1
+ # vim:sw=4:ts=4:et:
2
+ """Python Ring Other (Intercom) wrapper."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import json
7
+ import logging
8
+ import uuid
9
+ from typing import TYPE_CHECKING, Any, ClassVar
10
+
11
+ from python_ring.const import (
12
+ DOORBELLS_ENDPOINT,
13
+ HEALTH_DOORBELL_ENDPOINT,
14
+ INTERCOM_ALLOWED_USERS,
15
+ INTERCOM_INVITATIONS_DELETE_ENDPOINT,
16
+ INTERCOM_INVITATIONS_ENDPOINT,
17
+ INTERCOM_KINDS,
18
+ INTERCOM_OPEN_ENDPOINT,
19
+ INTERCOM_VIDEO_KINDS,
20
+ MIC_VOL_MAX,
21
+ MIC_VOL_MIN,
22
+ MSG_VOL_OUTBOUND,
23
+ OTHER_DOORBELL_VOL_MAX,
24
+ OTHER_DOORBELL_VOL_MIN,
25
+ SETTINGS_ENDPOINT,
26
+ VOICE_VOL_MAX,
27
+ VOICE_VOL_MIN,
28
+ RingCapability,
29
+ )
30
+ from python_ring.exceptions import RingError
31
+ from python_ring.generic import RingGeneric
32
+ from python_ring.webrtcdevice import RingWebRtcMixin
33
+
34
+ _LOGGER = logging.getLogger(__name__)
35
+
36
+
37
+ class RingOther(RingWebRtcMixin, RingGeneric):
38
+ """Implementation for Ring Intercom."""
39
+
40
+ if TYPE_CHECKING:
41
+ from python_ring.ring import Ring
42
+
43
+ def __init__(self, ring: Ring, device_api_id: int, *, shared: bool = False) -> None:
44
+ """Initialise the other devices."""
45
+ super().__init__(ring, device_api_id)
46
+ self.shared = shared
47
+ self._webrtc_streams = {}
48
+
49
+ @property
50
+ def family(self) -> str:
51
+ """Return Ring device family type."""
52
+ return "other"
53
+
54
+ async def async_update_health_data(self) -> None:
55
+ """Update health attrs."""
56
+ resp = await self._ring.async_query(
57
+ HEALTH_DOORBELL_ENDPOINT.format(self.device_api_id)
58
+ )
59
+ self._health_attrs = resp.json().get("device_health", {})
60
+
61
+ @property
62
+ def model(self) -> str:
63
+ """Return Ring device model name."""
64
+ if self.kind in INTERCOM_KINDS:
65
+ return "Intercom"
66
+ return "Unknown Other"
67
+
68
+ def has_capability(self, capability: RingCapability | str) -> bool:
69
+ """Return if device has specific capability."""
70
+ capability = (
71
+ capability
72
+ if isinstance(capability, RingCapability)
73
+ else RingCapability.from_name(capability)
74
+ )
75
+ if capability in [
76
+ RingCapability.OPEN,
77
+ RingCapability.HISTORY,
78
+ RingCapability.DING,
79
+ ]:
80
+ return self.kind in INTERCOM_KINDS
81
+ if capability == RingCapability.VIDEO:
82
+ return self.kind in INTERCOM_VIDEO_KINDS
83
+ return False
84
+
85
+ @property
86
+ def battery_life(self) -> int | None:
87
+ """Return battery life."""
88
+ if self.kind in INTERCOM_KINDS:
89
+ if self._attrs.get("battery_life") is None:
90
+ return None
91
+
92
+ value = int(self._attrs.get("battery_life", 0))
93
+ if value and value > 100:
94
+ value = 100
95
+
96
+ return value
97
+ return None
98
+
99
+ @property
100
+ def subscribed(self) -> bool:
101
+ """Return if is online."""
102
+ if self.kind in INTERCOM_KINDS:
103
+ result = self._attrs.get("subscribed")
104
+ return result is not None
105
+ return False
106
+
107
+ @property
108
+ def has_subscription(self) -> bool:
109
+ """Return boolean if the account has subscription."""
110
+ if self.kind in INTERCOM_KINDS and (features := self._attrs.get("features")):
111
+ return features.get("show_recordings", False)
112
+ return False
113
+
114
+ @property
115
+ def unlock_duration(self) -> str | None:
116
+ """Return time unlock switch is held closed."""
117
+ return (
118
+ json.loads(self._attrs["settings"]["intercom_settings"]["config"])
119
+ .get("analog", {})
120
+ .get("unlock_duration")
121
+ )
122
+
123
+ @property
124
+ def doorbell_volume(self) -> int:
125
+ """Return doorbell volume."""
126
+ if self.kind in INTERCOM_KINDS:
127
+ return self._attrs["settings"].get("doorbell_volume", 0)
128
+ return 0
129
+
130
+ async def async_set_doorbell_volume(self, value: int) -> None:
131
+ """Set the doorbell volume."""
132
+ if not (
133
+ (isinstance(value, int))
134
+ and (OTHER_DOORBELL_VOL_MIN <= value <= OTHER_DOORBELL_VOL_MAX)
135
+ ):
136
+ raise RingError(
137
+ MSG_VOL_OUTBOUND.format(OTHER_DOORBELL_VOL_MIN, OTHER_DOORBELL_VOL_MAX)
138
+ )
139
+
140
+ params = {
141
+ "doorbot[settings][doorbell_volume]": str(value),
142
+ }
143
+ url = DOORBELLS_ENDPOINT.format(self.device_api_id)
144
+ await self._ring.async_query(url, extra_params=params, method="PUT")
145
+
146
+ @property
147
+ def keep_alive_auto(self) -> float | None:
148
+ """The keep alive auto setting."""
149
+ if self.kind in INTERCOM_KINDS:
150
+ return self._attrs["settings"].get("keep_alive_auto")
151
+ return None
152
+
153
+ async def async_set_keep_alive_auto(self, value: float) -> None:
154
+ """Update the keep alive auto setting."""
155
+ url = SETTINGS_ENDPOINT.format(self.device_api_id)
156
+ payload = {"keep_alive_settings": {"keep_alive_auto": value}}
157
+
158
+ await self._ring.async_query(url, method="PATCH", json=payload)
159
+
160
+ @property
161
+ def mic_volume(self) -> int | None:
162
+ """Return mic volume."""
163
+ if self.kind in INTERCOM_KINDS:
164
+ return self._attrs["settings"].get("mic_volume")
165
+ return None
166
+
167
+ async def async_set_mic_volume(self, value: int) -> None:
168
+ """Set the mic volume."""
169
+ if not ((isinstance(value, int)) and (MIC_VOL_MIN <= value <= MIC_VOL_MAX)):
170
+ raise RingError(MSG_VOL_OUTBOUND.format(MIC_VOL_MIN, MIC_VOL_MAX))
171
+
172
+ url = SETTINGS_ENDPOINT.format(self.device_api_id)
173
+ payload = {"volume_settings": {"mic_volume": value}}
174
+
175
+ await self._ring.async_query(url, method="PATCH", json=payload)
176
+
177
+ @property
178
+ def voice_volume(self) -> int | None:
179
+ """Return voice volume."""
180
+ if self.kind in INTERCOM_KINDS:
181
+ return self._attrs["settings"].get("voice_volume")
182
+ return None
183
+
184
+ async def async_set_voice_volume(self, value: int) -> None:
185
+ """Set the voice volume."""
186
+ if not ((isinstance(value, int)) and (VOICE_VOL_MIN <= value <= VOICE_VOL_MAX)):
187
+ raise RingError(MSG_VOL_OUTBOUND.format(VOICE_VOL_MIN, VOICE_VOL_MAX))
188
+
189
+ url = SETTINGS_ENDPOINT.format(self.device_api_id)
190
+ payload = {"volume_settings": {"voice_volume": value}}
191
+
192
+ await self._ring.async_query(url, method="PATCH", json=payload)
193
+
194
+ async def async_get_clip_length_max(self) -> int | None:
195
+ """Get the Maximum clip length."""
196
+ url = SETTINGS_ENDPOINT.format(self.device_api_id)
197
+ resp = await self._ring.async_query(url, method="GET")
198
+ return resp.json().get("video_settings", {}).get("clip_length_max")
199
+
200
+ async def async_set_clip_length_max(self, value: int) -> None:
201
+ """Set the maximum clip length.
202
+
203
+ This value sets an effective refractory period on consecutive rigns
204
+ eg if set to default value of 60, rings occuring with 60 seconds of
205
+ first will not be detected.
206
+ """
207
+ url = SETTINGS_ENDPOINT.format(self.device_api_id)
208
+ payload = {"video_settings": {"clip_length_max": value}}
209
+ await self._ring.async_query(url, method="PATCH", json=payload)
210
+
211
+ @property
212
+ def connection_status(self) -> str | None:
213
+ """Return connection status."""
214
+ if self.kind in INTERCOM_KINDS:
215
+ return self._attrs.get("alerts", {}).get("connection")
216
+ return None
217
+
218
+ async def async_get_allowed_users(self) -> list[dict[str, Any]] | None:
219
+ """Return list of users allowed or invited to access."""
220
+ if self.kind in INTERCOM_KINDS:
221
+ url = INTERCOM_ALLOWED_USERS.format(self.location_id)
222
+ resp = await self._ring.async_query(url, method="GET")
223
+ return resp.json()
224
+
225
+ return None
226
+
227
+ async def async_open_door(self, user_id: int = -1) -> bool:
228
+ """Open the door."""
229
+ if self.kind in INTERCOM_KINDS:
230
+ url = INTERCOM_OPEN_ENDPOINT.format(self.device_api_id)
231
+ request_id = str(uuid.uuid4())
232
+ # params can also accept:
233
+ # issue_time: in seconds
234
+ # command_timeout: in seconds
235
+ payload = {
236
+ "command_name": "device_rpc",
237
+ "request": {
238
+ "id": request_id,
239
+ "jsonrpc": "2.0",
240
+ "method": "unlock_door",
241
+ "params": {
242
+ "door_id": 0,
243
+ "user_id": user_id,
244
+ },
245
+ },
246
+ }
247
+ resp = await self._ring.async_query(url, method="PUT", json=payload)
248
+ response = resp.json()
249
+ if response.get("result", {}).get("code", -1) == 0:
250
+ return True
251
+
252
+ return False
253
+
254
+ async def async_invite_access(self, email: str) -> bool:
255
+ """Invite user."""
256
+ if self.kind in INTERCOM_KINDS:
257
+ url = INTERCOM_INVITATIONS_ENDPOINT.format(self.location_id)
258
+ payload = {
259
+ "invitation": {
260
+ "doorbot_ids": [self.device_api_id],
261
+ "invited_email": email,
262
+ "group_ids": [],
263
+ }
264
+ }
265
+ await self._ring.async_query(url, method="POST", json=payload)
266
+ return True
267
+
268
+ return False
269
+
270
+ async def async_remove_access(self, user_id: int) -> bool:
271
+ """Remove user access or invitation."""
272
+ if self.kind in INTERCOM_KINDS:
273
+ url = INTERCOM_INVITATIONS_DELETE_ENDPOINT.format(self.location_id, user_id)
274
+ await self._ring.async_query(url, method="DELETE")
275
+ return True
276
+
277
+ return False
278
+
279
+ DEPRECATED_API_QUERIES: ClassVar = {
280
+ *RingGeneric.DEPRECATED_API_QUERIES,
281
+ "update_health_data",
282
+ "open_door",
283
+ "invite_access",
284
+ "remove_access",
285
+ }
286
+ DEPRECATED_API_PROPERTY_GETTERS: ClassVar = {
287
+ *RingGeneric.DEPRECATED_API_PROPERTY_GETTERS,
288
+ "clip_length_max",
289
+ "allowed_users",
290
+ }
291
+ DEPRECATED_API_PROPERTY_SETTERS: ClassVar = {
292
+ *RingGeneric.DEPRECATED_API_PROPERTY_SETTERS,
293
+ "doorbell_volume",
294
+ "keep_alive_auto",
295
+ "mic_volume",
296
+ "voice_volume",
297
+ "clip_length_max",
298
+ }
python_ring/py.typed ADDED
File without changes