From 710758dd1673816e495c91d4d9646ba020b97882 Mon Sep 17 00:00:00 2001 From: strohganoff Date: Fri, 14 Feb 2025 00:55:04 -0600 Subject: [PATCH] Fix up StreamDeckCommandSender class and its GlobalSettings methods to use the plugin registration UUID rather than the action UUID. --- streamdeck/command_sender.py | 12 ++++++------ streamdeck/manager.py | 2 +- tests/conftest.py | 6 ++++++ tests/plugin_manager/conftest.py | 3 +-- tests/test_command_sender.py | 19 +++++++++++-------- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/streamdeck/command_sender.py b/streamdeck/command_sender.py index cc2594a..7885d69 100644 --- a/streamdeck/command_sender.py +++ b/streamdeck/command_sender.py @@ -16,8 +16,9 @@ class StreamDeckCommandSender: """Class for sending command event messages to the Stream Deck software through a WebSocket client.""" - def __init__(self, client: WebSocketClient): + def __init__(self, client: WebSocketClient, plugin_registration_uuid: str): self._client = client + self._plugin_registration_uuid = plugin_registration_uuid def _send_event(self, event: str, **kwargs: Any) -> None: self._client.send_event({ @@ -38,18 +39,17 @@ def get_settings(self, context: str) -> None: context=context, ) - def set_global_settings(self, context: str, payload: dict[str, Any]) -> None: + def set_global_settings(self, payload: dict[str, Any]) -> None: self._send_event( event="setGlobalSettings", - context=context, + context=self._plugin_registration_uuid, payload=payload, ) - def get_global_settings(self, context: str) -> None: - """FYI: It seems like this causes the 'didReceiveGlobalSettings' event to only the Property Inspector.""" + def get_global_settings(self) -> None: self._send_event( event="getGlobalSettings", - context=context, + context=self._plugin_registration_uuid, ) def open_url(self, context: str, url: str) -> None: diff --git a/streamdeck/manager.py b/streamdeck/manager.py index 7bdd930..3f8bc3b 100644 --- a/streamdeck/manager.py +++ b/streamdeck/manager.py @@ -95,7 +95,7 @@ def run(self) -> None: and triggers the appropriate action handlers based on the received events. """ with WebSocketClient(port=self._port) as client: - command_sender = StreamDeckCommandSender(client) + command_sender = StreamDeckCommandSender(client, plugin_registration_uuid=self._registration_uuid) command_sender.send_action_registration(register_event=self._register_event, plugin_registration_uuid=self._registration_uuid) diff --git a/tests/conftest.py b/tests/conftest.py index 1f78807..ed6095a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import random +import uuid import pytest @@ -7,3 +8,8 @@ def port_number(): """Fixture to provide a random 4-digit port number for each test.""" return random.randint(1000, 9999) + + +@pytest.fixture +def plugin_registration_uuid() -> str: + return str(uuid.uuid1()) diff --git a/tests/plugin_manager/conftest.py b/tests/plugin_manager/conftest.py index 9b1a11a..b85d149 100644 --- a/tests/plugin_manager/conftest.py +++ b/tests/plugin_manager/conftest.py @@ -9,14 +9,13 @@ @pytest.fixture -def plugin_manager(port_number: int) -> PluginManager: +def plugin_manager(port_number: int, plugin_registration_uuid: str) -> PluginManager: """Fixture that provides a configured instance of PluginManager for testing. Returns: PluginManager: An instance of PluginManager with test parameters. """ plugin_uuid = "test-plugin-uuid" - plugin_registration_uuid = str(uuid.uuid1()) register_event = "registerPlugin" info = {"some": "info"} diff --git a/tests/test_command_sender.py b/tests/test_command_sender.py index 3eb8ffc..7e446a6 100644 --- a/tests/test_command_sender.py +++ b/tests/test_command_sender.py @@ -14,9 +14,9 @@ def mock_client() -> Mock: @pytest.fixture -def command_sender(mock_client: Mock) -> StreamDeckCommandSender: +def command_sender(mock_client: Mock, plugin_registration_uuid: str) -> StreamDeckCommandSender: """Fixture to provide an instance of StreamDeckCommandSender with a mocked client.""" - return StreamDeckCommandSender(client=mock_client) + return StreamDeckCommandSender(client=mock_client, plugin_registration_uuid=plugin_registration_uuid) @pytest.mark.parametrize( @@ -24,7 +24,7 @@ def command_sender(mock_client: Mock) -> StreamDeckCommandSender: [ ( "get_global_settings", - "fake_context", + None, # get_global_settings uses the command_sender's own plugin_registration_uuid attribute as the context. {}, "getGlobalSettings", {} @@ -115,7 +115,7 @@ def command_sender(mock_client: Mock) -> StreamDeckCommandSender: ), ( "set_global_settings", - "fake_context", + None, # set_global_settings uses the command_sender's own plugin_registration_uuid attribute as the context. {"payload": {"key": "value"}}, "setGlobalSettings", {"payload": {"key": "value"}}, @@ -147,7 +147,7 @@ def test_command_sender_methods( command_sender: StreamDeckCommandSender, mock_client: Mock, method_name: str, - context: str, + context: str | None, extra_args: dict, expected_event: str, expected_payload: dict, @@ -157,14 +157,17 @@ def test_command_sender_methods( assert hasattr(command_sender, method_name) method = getattr(command_sender, method_name) - method(context, **extra_args) + if context is not None: + method(context, **extra_args) + else: + method(**extra_args) # Build the expected data structure to send through the WebSocket expected_data = { - "context": context, + "context": context or command_sender._plugin_registration_uuid, "event": expected_event, **expected_payload, } # Assert that the client's send_event method was called with the expected data - mock_client.send_event.assert_called_once_with(expected_data) \ No newline at end of file + mock_client.send_event.assert_called_once_with(expected_data)