-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add fixtures for Axis rtsp client and adapt tests to use them #47901
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
emontnemery
merged 2 commits into
home-assistant:dev
from
Kane610:axis-create_rtspclient_fixture
Apr 9, 2021
Merged
Changes from all commits
Commits
Show all changes
2 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
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,2 +1,112 @@ | ||
| """axis conftest.""" | ||
| """Axis conftest.""" | ||
|
|
||
| from typing import Optional | ||
| from unittest.mock import patch | ||
|
|
||
| from axis.rtsp import ( | ||
| SIGNAL_DATA, | ||
| SIGNAL_FAILED, | ||
| SIGNAL_PLAYING, | ||
| STATE_PLAYING, | ||
| STATE_STOPPED, | ||
| ) | ||
| import pytest | ||
|
|
||
| from tests.components.light.conftest import mock_light_profiles # noqa: F401 | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def mock_axis_rtspclient(): | ||
| """No real RTSP communication allowed.""" | ||
| with patch("axis.streammanager.RTSPClient") as rtsp_client_mock: | ||
|
|
||
| rtsp_client_mock.return_value.session.state = STATE_STOPPED | ||
|
|
||
| async def start_stream(): | ||
| """Set state to playing when calling RTSPClient.start.""" | ||
| rtsp_client_mock.return_value.session.state = STATE_PLAYING | ||
|
|
||
| rtsp_client_mock.return_value.start = start_stream | ||
|
|
||
| def stop_stream(): | ||
| """Set state to stopped when calling RTSPClient.stop.""" | ||
| rtsp_client_mock.return_value.session.state = STATE_STOPPED | ||
|
|
||
| rtsp_client_mock.return_value.stop = stop_stream | ||
|
|
||
| def make_rtsp_call(data: Optional[dict] = None, state: str = ""): | ||
| """Generate a RTSP call.""" | ||
| axis_streammanager_session_callback = rtsp_client_mock.call_args[0][4] | ||
|
|
||
| if data: | ||
| rtsp_client_mock.return_value.rtp.data = data | ||
| axis_streammanager_session_callback(signal=SIGNAL_DATA) | ||
| elif state: | ||
| axis_streammanager_session_callback(signal=state) | ||
| else: | ||
| raise NotImplementedError | ||
|
|
||
| yield make_rtsp_call | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def mock_rtsp_event(mock_axis_rtspclient): | ||
| """Fixture to allow mocking received RTSP events.""" | ||
|
|
||
| def send_event( | ||
| topic: str, | ||
| data_type: str, | ||
| data_value: str, | ||
| operation: str = "Initialized", | ||
| source_name: str = "", | ||
| source_idx: str = "", | ||
| ) -> None: | ||
| source = "" | ||
| if source_name != "" and source_idx != "": | ||
| source = f'<tt:SimpleItem Name="{source_name}" Value="{source_idx}"/>' | ||
|
|
||
| event = f"""<?xml version="1.0" encoding="UTF-8"?> | ||
| <tt:MetadataStream xmlns:tt="http://www.onvif.org/ver10/schema"> | ||
| <tt:Event> | ||
| <wsnt:NotificationMessage xmlns:tns1="http://www.onvif.org/ver10/topics" | ||
| xmlns:tnsaxis="http://www.axis.com/2009/event/topics" | ||
| xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" | ||
| xmlns:wsa5="http://www.w3.org/2005/08/addressing"> | ||
| <wsnt:Topic Dialect="http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple"> | ||
| {topic} | ||
| </wsnt:Topic> | ||
| <wsnt:ProducerReference> | ||
| <wsa5:Address> | ||
| uri://bf32a3b9-e5e7-4d57-a48d-1b5be9ae7b16/ProducerReference | ||
| </wsa5:Address> | ||
| </wsnt:ProducerReference> | ||
| <wsnt:Message> | ||
| <tt:Message UtcTime="2020-11-03T20:21:48.346022Z" | ||
| PropertyOperation="{operation}"> | ||
| <tt:Source>{source}</tt:Source> | ||
| <tt:Key></tt:Key> | ||
| <tt:Data> | ||
| <tt:SimpleItem Name="{data_type}" Value="{data_value}"/> | ||
| </tt:Data> | ||
| </tt:Message> | ||
| </wsnt:Message> | ||
| </wsnt:NotificationMessage> | ||
| </tt:Event> | ||
| </tt:MetadataStream> | ||
| """ | ||
|
|
||
| mock_axis_rtspclient(data=event.encode("utf-8")) | ||
|
|
||
| yield send_event | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def mock_rtsp_signal_state(mock_axis_rtspclient): | ||
| """Fixture to allow mocking RTSP state signalling.""" | ||
|
|
||
| def send_signal(connected: bool) -> None: | ||
| """Signal state change of RTSP connection.""" | ||
| signal = SIGNAL_PLAYING if connected else SIGNAL_FAILED | ||
| mock_axis_rtspclient(state=signal) | ||
|
|
||
| yield send_signal |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could consider unsubscribing in
async_unload_entryinstead, it makes it easier to follow the flow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I will consider how to do this