-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
User camera unique id in go2rtc if available #168603
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,27 @@ | ||
| """Go2rtc utility functions.""" | ||
|
|
||
| from pathlib import Path | ||
| import string | ||
| from urllib.parse import quote | ||
|
|
||
| from homeassistant.components.camera import Camera | ||
|
|
||
| _HA_MANAGED_UNIX_SOCKET_FILE = "go2rtc.sock" | ||
| # Go2rtc is not validating the camera identifier, but some characters (e.g. : or #) | ||
| # have special meaning in URLs and could cause issues. | ||
| _SAFE_CHARS = string.ascii_letters + string.digits + "._-" | ||
|
|
||
|
|
||
| def get_go2rtc_unix_socket_path(path: str | Path) -> str: | ||
| """Get the Go2rtc unix socket path.""" | ||
| if not isinstance(path, Path): | ||
| path = Path(path) | ||
| return str(path / _HA_MANAGED_UNIX_SOCKET_FILE) | ||
|
|
||
|
|
||
| def get_camera_identifier(camera: Camera) -> str: | ||
| """Get the Go2rtc camera identifier.""" | ||
| attr = camera.entity_id | ||
| if camera.unique_id is not None: | ||
| attr = f"{camera.platform.platform_name}_{camera.unique_id}" | ||
| return quote(attr, safe=_SAFE_CHARS) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """Go2rtc utility function tests.""" | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from homeassistant.components.camera import Camera | ||
| from homeassistant.components.go2rtc.util import get_camera_identifier | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("unique_id", "entity_id", "expected"), | ||
| [ | ||
| # Prefer unique_id over entity_id | ||
| ("unique123", "camera.test", "test_unique123"), | ||
| # Fall back to entity_id when unique_id is None | ||
| (None, "camera.test", "camera.test"), | ||
| # Safe characters pass through | ||
| ("abc-def_ghi.123", "camera.test", "test_abc-def_ghi.123"), | ||
| # Special characters are percent-encoded | ||
| ("cam#1", "camera.test", "test_cam%231"), | ||
| ("cam:1", "camera.test", "test_cam%3A1"), | ||
| ("cam/1", "camera.test", "test_cam%2F1"), | ||
| ("cam?1", "camera.test", "test_cam%3F1"), | ||
| ("cam&1", "camera.test", "test_cam%261"), | ||
| ("cam=1", "camera.test", "test_cam%3D1"), | ||
| ("cam%1", "camera.test", "test_cam%251"), | ||
| ("cam 1", "camera.test", "test_cam%201"), | ||
| ("cam@1", "camera.test", "test_cam%401"), | ||
| ("cam_1", "camera.test", "test_cam_1"), | ||
| ("cam%231", "camera.test", "test_cam%25231"), | ||
| # Non-ASCII: UTF-8 byte-wise encoding (€ = E2 82 AC) | ||
| ("cam€1", "camera.test", "test_cam%E2%82%AC1"), | ||
| ], | ||
| ) | ||
| def test_get_camera_identifier( | ||
| unique_id: str | None, entity_id: str, expected: str | ||
| ) -> None: | ||
| """Test get_camera_identifier sanitizes and prefers unique_id.""" | ||
| camera = Mock(spec_set=Camera) | ||
| camera.platform.platform_name = "test" | ||
| camera.unique_id = unique_id | ||
| camera.entity_id = entity_id | ||
| assert get_camera_identifier(camera) == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.