Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Frigate api and ws_api to multi selection instead of single selection #416

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions custom_components/frigate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ async def async_get_stats(self) -> dict[str, Any]:

async def async_get_events(
self,
camera: str | None = None,
label: str | None = None,
zone: str | None = None,
cameras: list[str] | None = None,
labels: list[str] | None = None,
zones: list[str] | None = None,
after: int | None = None,
before: int | None = None,
limit: int | None = None,
Expand All @@ -65,9 +65,9 @@ async def async_get_events(
) -> list[dict[str, Any]]:
"""Get data from the API."""
params = {
"camera": camera,
"label": label,
"zone": zone,
"cameras": ",".join(cameras) if cameras else None,
"labels": ",".join(labels) if labels else None,
"zones": ",".join(zones) if zones else None,
"after": after,
"before": before,
"limit": limit,
Expand Down
6 changes: 3 additions & 3 deletions custom_components/frigate/media_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,9 @@ async def async_browse_media(
events = await self._get_client(identifier).async_get_events(
after=identifier.after,
before=identifier.before,
camera=identifier.camera,
label=identifier.label,
zone=identifier.zone,
cameras=[identifier.camera] if identifier.camera else None,
labels=[identifier.label] if identifier.label else None,
zones=[identifier.zone] if identifier.zone else None,
limit=10000 if identifier.name.endswith(".all") else ITEM_LIMIT,
**media_kwargs,
)
Expand Down
16 changes: 8 additions & 8 deletions custom_components/frigate/ws_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ async def ws_get_recordings_summary(
{
vol.Required("type"): "frigate/events/get",
vol.Required("instance_id"): str,
vol.Optional("camera"): str,
vol.Optional("label"): str,
vol.Optional("zone"): str,
vol.Optional("cameras"): [str],
vol.Optional("labels"): [str],
vol.Optional("zones"): [str],
vol.Optional("after"): int,
vol.Optional("before"): int,
vol.Optional("limit"): int,
Expand All @@ -172,9 +172,9 @@ async def ws_get_events(
connection.send_result(
msg["id"],
await client.async_get_events(
msg.get("camera"),
msg.get("label"),
msg.get("zone"),
msg.get("cameras"),
msg.get("labels"),
msg.get("zones"),
msg.get("after"),
msg.get("before"),
msg.get("limit"),
Expand All @@ -187,8 +187,8 @@ async def ws_get_events(
connection.send_error(
msg["id"],
"frigate_error",
f"API error whilst retrieving events for camera "
f"{msg['camera']} for Frigate instance {msg['instance_id']}",
f"API error whilst retrieving events for cameras "
f"{msg['cameras']} for Frigate instance {msg['instance_id']}",
)


Expand Down
12 changes: 6 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ async def events_handler(request: web.Request) -> web.Response:
_assert_request_params(
request,
{
"camera": "test_camera",
"label": "test_label",
"zone": "test_zone",
"cameras": "test_camera1,test_camera2",
"labels": "test_label1,test_label2",
"zones": "test_zone1,test_zone2",
"after": "1",
"before": "2",
"limit": "3",
Expand All @@ -96,9 +96,9 @@ async def events_handler(request: web.Request) -> web.Response:

frigate_client = FrigateApiClient(str(server.make_url("/")), aiohttp_session)
assert events_in == await frigate_client.async_get_events(
camera="test_camera",
label="test_label",
zone="test_zone",
cameras=["test_camera1", "test_camera2"],
labels=["test_label1", "test_label2"],
zones=["test_zone1", "test_zone2"],
after=1,
before=2,
limit=3,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_media_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,9 @@ async def test_snapshots(hass: HomeAssistant) -> None:
assert client.async_get_events.call_args == call(
after=1622764800,
before=1622851200,
camera="front_door",
label="person",
zone=None,
cameras=["front_door"],
labels=["person"],
zones=None,
limit=50,
has_snapshot=True,
)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_ws_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ async def test_get_events_success(hass: HomeAssistant, hass_ws_client: Any) -> N
"id": 1,
"type": "frigate/events/get",
"instance_id": TEST_FRIGATE_INSTANCE_ID,
"camera": TEST_CAMERA,
"label": TEST_LABEL,
"zone": TEST_ZONE,
"cameras": [TEST_CAMERA],
"labels": [TEST_LABEL],
"zones": [TEST_ZONE],
"after": 1,
"before": 2,
"limit": 3,
Expand All @@ -269,7 +269,7 @@ async def test_get_events_success(hass: HomeAssistant, hass_ws_client: Any) -> N

response = await ws_client.receive_json()
mock_client.async_get_events.assert_called_with(
TEST_CAMERA, TEST_LABEL, TEST_ZONE, 1, 2, 3, True, True, decode_json=False
[TEST_CAMERA], [TEST_LABEL], [TEST_ZONE], 1, 2, 3, True, True, decode_json=False
)
assert response["success"]
assert response["result"] == events_success
Expand All @@ -287,7 +287,7 @@ async def test_get_events_instance_not_found(
"id": 1,
"type": "frigate/events/get",
"instance_id": "THIS-IS-NOT-A-REAL-INSTANCE-ID",
"camera": TEST_CAMERA,
"cameras": [TEST_CAMERA],
}

await ws_client.send_json(events_json)
Expand All @@ -307,7 +307,7 @@ async def test_get_events_api_error(hass: HomeAssistant, hass_ws_client: Any) ->
"id": 1,
"type": "frigate/events/get",
"instance_id": TEST_FRIGATE_INSTANCE_ID,
"camera": TEST_CAMERA,
"cameras": [TEST_CAMERA],
}

mock_client.async_get_events = AsyncMock(side_effect=FrigateApiClientError)
Expand Down