Skip to content
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 streamdeck/command_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion streamdeck/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import uuid

import pytest

Expand All @@ -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())
3 changes: 1 addition & 2 deletions tests/plugin_manager/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
19 changes: 11 additions & 8 deletions tests/test_command_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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(
("method_name", "context", "extra_args", "expected_event", "expected_payload"),
[
(
"get_global_settings",
"fake_context",
None, # get_global_settings uses the command_sender's own plugin_registration_uuid attribute as the context.
{},
"getGlobalSettings",
{}
Expand Down Expand Up @@ -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"}},
Expand Down Expand Up @@ -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,
Expand All @@ -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)
mock_client.send_event.assert_called_once_with(expected_data)