Skip to content

Commit 3572911

Browse files
authored
Merge pull request #6 from strohganoff/fix/global-commands-context
2 parents bb1cd1b + 710758d commit 3572911

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

streamdeck/command_sender.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
class StreamDeckCommandSender:
1818
"""Class for sending command event messages to the Stream Deck software through a WebSocket client."""
19-
def __init__(self, client: WebSocketClient):
19+
def __init__(self, client: WebSocketClient, plugin_registration_uuid: str):
2020
self._client = client
21+
self._plugin_registration_uuid = plugin_registration_uuid
2122

2223
def _send_event(self, event: str, **kwargs: Any) -> None:
2324
self._client.send_event({
@@ -38,18 +39,17 @@ def get_settings(self, context: str) -> None:
3839
context=context,
3940
)
4041

41-
def set_global_settings(self, context: str, payload: dict[str, Any]) -> None:
42+
def set_global_settings(self, payload: dict[str, Any]) -> None:
4243
self._send_event(
4344
event="setGlobalSettings",
44-
context=context,
45+
context=self._plugin_registration_uuid,
4546
payload=payload,
4647
)
4748

48-
def get_global_settings(self, context: str) -> None:
49-
"""FYI: It seems like this causes the 'didReceiveGlobalSettings' event to only the Property Inspector."""
49+
def get_global_settings(self) -> None:
5050
self._send_event(
5151
event="getGlobalSettings",
52-
context=context,
52+
context=self._plugin_registration_uuid,
5353
)
5454

5555
def open_url(self, context: str, url: str) -> None:

streamdeck/manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def run(self) -> None:
9595
and triggers the appropriate action handlers based on the received events.
9696
"""
9797
with WebSocketClient(port=self._port) as client:
98-
command_sender = StreamDeckCommandSender(client)
98+
command_sender = StreamDeckCommandSender(client, plugin_registration_uuid=self._registration_uuid)
9999

100100
command_sender.send_action_registration(register_event=self._register_event, plugin_registration_uuid=self._registration_uuid)
101101

tests/conftest.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import uuid
23

34
import pytest
45

@@ -7,3 +8,8 @@
78
def port_number():
89
"""Fixture to provide a random 4-digit port number for each test."""
910
return random.randint(1000, 9999)
11+
12+
13+
@pytest.fixture
14+
def plugin_registration_uuid() -> str:
15+
return str(uuid.uuid1())

tests/plugin_manager/conftest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99

1010

1111
@pytest.fixture
12-
def plugin_manager(port_number: int) -> PluginManager:
12+
def plugin_manager(port_number: int, plugin_registration_uuid: str) -> PluginManager:
1313
"""Fixture that provides a configured instance of PluginManager for testing.
1414
1515
Returns:
1616
PluginManager: An instance of PluginManager with test parameters.
1717
"""
1818
plugin_uuid = "test-plugin-uuid"
19-
plugin_registration_uuid = str(uuid.uuid1())
2019
register_event = "registerPlugin"
2120
info = {"some": "info"}
2221

tests/test_command_sender.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ def mock_client() -> Mock:
1414

1515

1616
@pytest.fixture
17-
def command_sender(mock_client: Mock) -> StreamDeckCommandSender:
17+
def command_sender(mock_client: Mock, plugin_registration_uuid: str) -> StreamDeckCommandSender:
1818
"""Fixture to provide an instance of StreamDeckCommandSender with a mocked client."""
19-
return StreamDeckCommandSender(client=mock_client)
19+
return StreamDeckCommandSender(client=mock_client, plugin_registration_uuid=plugin_registration_uuid)
2020

2121

2222
@pytest.mark.parametrize(
2323
("method_name", "context", "extra_args", "expected_event", "expected_payload"),
2424
[
2525
(
2626
"get_global_settings",
27-
"fake_context",
27+
None, # get_global_settings uses the command_sender's own plugin_registration_uuid attribute as the context.
2828
{},
2929
"getGlobalSettings",
3030
{}
@@ -115,7 +115,7 @@ def command_sender(mock_client: Mock) -> StreamDeckCommandSender:
115115
),
116116
(
117117
"set_global_settings",
118-
"fake_context",
118+
None, # set_global_settings uses the command_sender's own plugin_registration_uuid attribute as the context.
119119
{"payload": {"key": "value"}},
120120
"setGlobalSettings",
121121
{"payload": {"key": "value"}},
@@ -147,7 +147,7 @@ def test_command_sender_methods(
147147
command_sender: StreamDeckCommandSender,
148148
mock_client: Mock,
149149
method_name: str,
150-
context: str,
150+
context: str | None,
151151
extra_args: dict,
152152
expected_event: str,
153153
expected_payload: dict,
@@ -157,14 +157,17 @@ def test_command_sender_methods(
157157
assert hasattr(command_sender, method_name)
158158

159159
method = getattr(command_sender, method_name)
160-
method(context, **extra_args)
160+
if context is not None:
161+
method(context, **extra_args)
162+
else:
163+
method(**extra_args)
161164

162165
# Build the expected data structure to send through the WebSocket
163166
expected_data = {
164-
"context": context,
167+
"context": context or command_sender._plugin_registration_uuid,
165168
"event": expected_event,
166169
**expected_payload,
167170
}
168171

169172
# Assert that the client's send_event method was called with the expected data
170-
mock_client.send_event.assert_called_once_with(expected_data)
173+
mock_client.send_event.assert_called_once_with(expected_data)

0 commit comments

Comments
 (0)