-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathapi.py
More file actions
553 lines (497 loc) · 17.8 KB
/
api.py
File metadata and controls
553 lines (497 loc) · 17.8 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
"""Frigate API client."""
from __future__ import annotations
import asyncio
import datetime
import logging
import socket
from typing import Any, cast
import aiohttp
import async_timeout
from yarl import URL
from homeassistant.auth import jwt_wrapper
TIMEOUT = 10
REVIEW_SUMMARIZE_TIMEOUT = 60
_LOGGER: logging.Logger = logging.getLogger(__name__)
HEADERS = {"Content-type": "application/json; charset=UTF-8"}
# ==============================================================================
# Please do not add HomeAssistant specific imports/functionality to this module,
# so that this library can be optionally moved to a different repo at a later
# date.
# ==============================================================================
class FrigateApiClientError(Exception):
"""General FrigateApiClient error."""
class FrigateApiClient:
"""Frigate API client."""
def __init__(
self,
host: str,
session: aiohttp.ClientSession,
username: str | None = None,
password: str | None = None,
validate_ssl: bool = True,
) -> None:
"""Construct API Client."""
self._host = host
self._session = session
self._username = username
self._password = password
self._token_data: dict[str, Any] = {}
self.validate_ssl = validate_ssl
async def async_get_version(self) -> str:
"""Get data from the API."""
return cast(
str,
await self.api_wrapper(
"get", str(URL(self._host) / "api/version"), decode_json=False
),
)
async def async_get_stats(self) -> dict[str, Any]:
"""Get data from the API."""
return cast(
dict[str, Any],
await self.api_wrapper("get", str(URL(self._host) / "api/stats")),
)
async def async_get_event(
self,
event_id: str,
decode_json: bool = True,
) -> dict[str, Any]:
"""Get a single event by ID from the API."""
return cast(
dict[str, Any],
await self.api_wrapper(
"get",
str(URL(self._host) / f"api/events/{event_id}"),
decode_json=decode_json,
),
)
async def async_get_events(
self,
cameras: list[str] | None = None,
labels: list[str] | None = None,
sub_labels: list[str] | None = None,
zones: list[str] | None = None,
after: int | None = None,
before: int | None = None,
limit: int | None = None,
has_clip: bool | None = None,
has_snapshot: bool | None = None,
favorites: bool | None = None,
decode_json: bool = True,
) -> list[dict[str, Any]]:
"""Get data from the API."""
params = {
"cameras": ",".join(cameras) if cameras else None,
"labels": ",".join(labels) if labels else None,
"sub_labels": ",".join(sub_labels) if sub_labels else None,
"zones": ",".join(zones) if zones else None,
"after": after,
"before": before,
"limit": limit,
"has_clip": int(has_clip) if has_clip is not None else None,
"has_snapshot": int(has_snapshot) if has_snapshot is not None else None,
"include_thumbnails": 0,
"favorites": int(favorites) if favorites is not None else None,
}
return cast(
list[dict[str, Any]],
await self.api_wrapper(
"get",
str(
URL(self._host)
/ "api/events"
% {k: v for k, v in params.items() if v is not None}
),
decode_json=decode_json,
),
)
async def async_get_reviews(
self,
cameras: list[str] | None = None,
labels: list[str] | None = None,
zones: list[str] | None = None,
severity: str | None = None,
after: float | None = None,
before: float | None = None,
limit: int | None = None,
reviewed: bool | None = None,
decode_json: bool = True,
) -> list[dict[str, Any]]:
"""Get review items from the API."""
params = {
"cameras": ",".join(cameras) if cameras else None,
"labels": ",".join(labels) if labels else None,
"zones": ",".join(zones) if zones else None,
"severity": severity,
"after": after,
"before": before,
"limit": limit,
"reviewed": int(reviewed) if reviewed is not None else None,
}
return cast(
list[dict[str, Any]],
await self.api_wrapper(
"get",
str(
URL(self._host)
/ "api/review"
% {k: v for k, v in params.items() if v is not None}
),
decode_json=decode_json,
),
)
async def async_set_reviews_viewed(
self,
ids: list[str],
viewed: bool = True,
) -> dict[str, Any]:
"""Mark reviews as viewed/unviewed."""
return cast(
dict[str, Any],
await self.api_wrapper(
"post",
str(URL(self._host) / "api/reviews/viewed"),
data={"ids": ids, "reviewed": viewed},
),
)
async def async_get_event_summary(
self,
has_clip: bool | None = None,
has_snapshot: bool | None = None,
timezone: str | None = None,
decode_json: bool = True,
) -> list[dict[str, Any]]:
"""Get data from the API."""
params = {
"has_clip": int(has_clip) if has_clip is not None else None,
"has_snapshot": int(has_snapshot) if has_snapshot is not None else None,
"timezone": str(timezone) if timezone is not None else None,
}
return cast(
list[dict[str, Any]],
await self.api_wrapper(
"get",
str(
URL(self._host)
/ "api/events/summary"
% {k: v for k, v in params.items() if v is not None}
),
decode_json=decode_json,
),
)
async def async_get_config(self) -> dict[str, Any]:
"""Get data from the API."""
return cast(
dict[str, Any],
await self.api_wrapper("get", str(URL(self._host) / "api/config")),
)
async def async_get_faces(self) -> list[str]:
"""Get list of known faces."""
try:
result = await self.api_wrapper(
"get", str(URL(self._host) / "api/faces"), decode_json=True
)
if isinstance(result, dict):
return [name for name in result.keys() if name != "train"]
return []
except FrigateApiClientError:
return []
async def async_get_classification_model_classes(
self, model_name: str
) -> list[str]:
"""Get list of classification classes for a model."""
try:
result = await self.api_wrapper(
"get",
str(URL(self._host) / f"api/classification/{model_name}/dataset"),
decode_json=True,
)
if isinstance(result, dict) and "categories" in result:
categories = result["categories"]
if isinstance(categories, dict):
return [name for name in categories.keys() if name != "none"]
return []
except FrigateApiClientError:
return []
async def async_get_ptz_info(
self,
camera: str,
decode_json: bool = True,
) -> Any:
"""Get PTZ info."""
return await self.api_wrapper(
"get",
str(URL(self._host) / "api" / camera / "ptz/info"),
decode_json=decode_json,
)
async def async_get_path(self, path: str) -> Any:
"""Get data from the API."""
return await self.api_wrapper("get", str(URL(self._host) / f"{path}/"))
async def async_retain(
self, event_id: str, retain: bool, decode_json: bool = True
) -> dict[str, Any] | str:
"""Un/Retain an event."""
result = await self.api_wrapper(
"post" if retain else "delete",
str(URL(self._host) / f"api/events/{event_id}/retain"),
decode_json=decode_json,
)
return cast(dict[str, Any], result) if decode_json else result
async def async_export_recording(
self,
camera: str,
playback_factor: str,
start_time: float,
end_time: float,
name: str | None = None,
decode_json: bool = True,
) -> dict[str, Any] | str:
"""Export recording."""
result = await self.api_wrapper(
"post",
str(
URL(self._host)
/ f"api/export/{camera}/start/{start_time}/end/{end_time}"
),
data={"playback": playback_factor, "name": name},
decode_json=decode_json,
)
return cast(dict[str, Any], result) if decode_json else result
async def async_get_recordings_summary(
self, camera: str, timezone: str, decode_json: bool = True
) -> list[dict[str, Any]] | str:
"""Get recordings summary."""
params = {"timezone": timezone}
result = await self.api_wrapper(
"get",
str(
URL(self._host)
/ f"api/{camera}/recordings/summary"
% {k: v for k, v in params.items() if v is not None}
),
decode_json=decode_json,
)
return cast(list[dict[str, Any]], result) if decode_json else result
async def async_get_recordings(
self,
camera: str,
after: int | None = None,
before: int | None = None,
decode_json: bool = True,
) -> dict[str, Any] | str:
"""Get recordings."""
params = {
"after": after,
"before": before,
}
result = await self.api_wrapper(
"get",
str(
URL(self._host)
/ f"api/{camera}/recordings"
% {k: v for k, v in params.items() if v is not None}
),
decode_json=decode_json,
)
return cast(dict[str, Any], result) if decode_json else result
async def async_create_event(
self,
camera: str,
label: str,
sub_label: str = "",
duration: int | None = 30,
include_recording: bool = True,
) -> dict[str, Any]:
"""Create an event."""
return cast(
dict[str, Any],
await self.api_wrapper(
"post",
str(URL(self._host) / f"api/events/{camera}/{label}/create"),
data={
"sub_label": sub_label,
"duration": duration,
"include_recording": include_recording,
},
),
)
async def async_end_event(self, event_id: str) -> dict[str, Any]:
"""End an event."""
return cast(
dict[str, Any],
await self.api_wrapper(
"put",
str(URL(self._host) / f"api/events/{event_id}/end"),
),
)
async def async_review_summarize(
self,
start_time: float,
end_time: float,
decode_json: bool = True,
) -> dict[str, Any] | str:
"""Get review summary for a time period."""
result = await self.api_wrapper(
"post",
str(
URL(self._host)
/ f"api/review/summarize/start/{start_time}/end/{end_time}"
),
decode_json=decode_json,
timeout=REVIEW_SUMMARIZE_TIMEOUT,
)
return cast(dict[str, Any], result) if decode_json else result
async def async_chat_completion(
self,
query: str,
camera_name: str | None = None,
) -> dict[str, Any]:
"""Send a chat completion request to Frigate."""
data: dict[str, Any] = {
"messages": [{"role": "user", "content": query}],
"max_tool_iterations": 5,
"stream": False,
}
if camera_name:
data["include_live_image"] = camera_name
return cast(
dict[str, Any],
await self.api_wrapper(
"post",
str(URL(self._host) / "api/chat/completion"),
data=data,
timeout=REVIEW_SUMMARIZE_TIMEOUT,
),
)
async def _get_token(self) -> None:
"""
Obtain a new JWT token using the provided username and password.
Sends a POST request to the login endpoint and extracts the token
and expiration date from the response headers.
"""
response = await self.api_wrapper(
method="post",
url=str(URL(self._host) / "api/login"),
data={"user": self._username, "password": self._password},
decode_json=False,
is_login_request=True,
)
set_cookie_header = response.headers.get("Set-Cookie", "")
if not set_cookie_header:
raise KeyError("Missing Set-Cookie header in response")
for cookie_prop in set_cookie_header.split(";"):
cookie_prop = cookie_prop.strip()
if cookie_prop.startswith("frigate_token="):
jwt_token = cookie_prop.split("=", 1)[1]
self._token_data["token"] = jwt_token
try:
decoded_token = jwt_wrapper.unverified_hs256_token_decode(jwt_token)
except Exception as e:
raise ValueError(f"Failed to decode JWT token: {e}")
exp_timestamp = decoded_token.get("exp")
if not exp_timestamp:
raise KeyError("JWT is missing 'exp' claim")
self._token_data["expires"] = datetime.datetime.fromtimestamp(
exp_timestamp, datetime.UTC
)
break
else:
raise KeyError("Missing 'frigate_token' in Set-Cookie header")
async def _refresh_token_if_needed(self) -> None:
"""
Refresh the JWT token if it is expired or about to expire.
"""
if "expires" not in self._token_data:
await self._get_token()
return
current_time = datetime.datetime.now(datetime.UTC)
if current_time >= self._token_data["expires"]: # Compare UTC-aware datetimes
await self._get_token()
async def get_auth_headers(self) -> dict[str, str]:
"""
Get headers for API requests, including the JWT token if available.
Ensures that the token is refreshed if needed.
"""
headers = {}
if self._username and self._password:
await self._refresh_token_if_needed()
if "token" in self._token_data:
headers["Authorization"] = f"Bearer {self._token_data['token']}"
return headers
async def api_wrapper(
self,
method: str,
url: str,
data: dict | None = None,
headers: dict | None = None,
decode_json: bool = True,
is_login_request: bool = False,
timeout: int | None = None,
) -> Any:
"""Get information from the API."""
if data is None:
data = {}
if headers is None:
headers = {}
if not is_login_request:
headers.update(await self.get_auth_headers())
try:
timeout_value = timeout if timeout is not None else TIMEOUT
async with async_timeout.timeout(timeout_value):
func = getattr(self._session, method)
if func:
response = await func(
url,
headers=headers,
raise_for_status=True,
json=data,
ssl=self.validate_ssl,
)
response.raise_for_status()
if is_login_request:
return response
if decode_json:
return await response.json()
return await response.text()
except asyncio.TimeoutError as exc:
_LOGGER.error(
"Timeout error fetching information from %s: %s",
url,
exc,
)
raise FrigateApiClientError from exc
except aiohttp.ClientResponseError as exc:
if exc.status == 401:
_LOGGER.error(
"Unauthorized (401) error for URL %s: %s", url, exc.message
)
raise FrigateApiClientError(
"Unauthorized access - check credentials."
) from exc
elif exc.status == 403:
_LOGGER.error("Forbidden (403) error for URL %s: %s", url, exc.message)
raise FrigateApiClientError(
"Forbidden - insufficient permissions."
) from exc
else:
_LOGGER.error(
"Client response error (%d) for URL %s: %s",
exc.status,
url,
exc.message,
)
raise FrigateApiClientError from exc
except (KeyError, TypeError) as exc:
_LOGGER.error(
"Error parsing information from %s: %s",
url,
exc,
)
raise FrigateApiClientError from exc
except (aiohttp.ClientError, socket.gaierror) as exc:
_LOGGER.error(
"Error fetching information from %s: %s",
url,
exc,
)
raise FrigateApiClientError from exc