Skip to content

Commit

Permalink
Feat: At-command target (#260)
Browse files Browse the repository at this point in the history
* Feature draft.

* Adjusting the original test.

* Ref: log the actually received command.

* Ref: naming, ordering.

* Testing the cases.

* Fix quotes.

* Ref: reset mock in loop.

* Negative case.

* Readme: listing the feature.

* Changelog: proposed 4.1.0.

* Fix: split only when the command is confirmed.

* Ref: smarter unpack with defaults.

* Fix case insensitivity.

* Fix defaults.

* Fix unused rest var.

* Ref: smarter mapping of the target_str.
  • Loading branch information
RobinTail authored Feb 19, 2024
1 parent 2a40c7c commit 135c57f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Version 4

### 4.1.0

- New feature: target for GCODE command (at-command):
- You can now switch the relay to the desired state;
- `@OCTORELAY r1 ON` — to switch the first relay on;
- `@OCTORELAY r1 OFF` — to switch the first relay off;
- `@OCTORELAY r1` — to toggle the first relay.

### 4.0.1

- Technical update: no new features, no fixes.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ You can toggle the relays ON and OFF the following ways:

- By clicking the control buttons on the navigation bar.
- The icon you choose for the button will display the current state.
- By sending GCODE command `@OCTORELAY r#`.
- Where `#` is relay index from `1` to `8`.
- By sending GCODE command `@OCTORELAY r# [ON|OFF]`.
- Where `#` is relay index from `1` to `8`;
- While `[ON|OFF]` is an optional target.
- Or by querying the API (see below).

## OctoRelay API
Expand Down
9 changes: 5 additions & 4 deletions octoprint_octorelay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,13 @@ def update_ui(self):
self._plugin_manager.send_plugin_message(self._identifier, self.model)

# pylint: disable=useless-return
def process_at_command(self, _comm, _phase, command, parameters, *args, **kwargs):
self._logger.info(f"Received @{AT_COMMAND} command with params: {parameters}")
def process_at_command(self, _comm, _phase, command, params_str, *args, **kwargs):
self._logger.info(f"Received @{command} command with params: {params_str}")
if command == AT_COMMAND:
index = parameters
[index, target_str, *_] = params_str.split() + [""] * 2 # unpack with default empty strings
target = { "ON": True, "OFF": False }.get(target_str.upper()) # mapped or None
if index in RELAY_INDEXES:
self.toggle_relay(index)
self.toggle_relay(index, target)
return None # meaning no further actions required

def get_update_information(self):
Expand Down
14 changes: 11 additions & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,18 @@ def test_on_api_command__unknown(self, abort_mock):
abort_mock.assert_called_with(400, description="Unknown command")

def test_process_at_command(self):
# Should toggle the relay having index supplied as a parameter
# Should toggle or switch the relay having index and optional target supplied as a parameters
self.plugin_instance.toggle_relay = Mock()
self.assertIsNone(self.plugin_instance.process_at_command(None, None, "OCTORELAY", "r4"))
self.plugin_instance.toggle_relay.assert_called_with("r4")
cases = [
{ "params": "r4", "expected": ["r4", None] },
{ "params": "r4 ON", "expected": ["r4", True] },
{ "params": " r4 off ", "expected": ["r4", False] },
{ "params": " r4 ... ", "expected": ["r4", None] }
]
for case in cases:
self.plugin_instance.toggle_relay.reset_mock()
self.assertIsNone(self.plugin_instance.process_at_command(None, None, "OCTORELAY", case["params"]))
self.plugin_instance.toggle_relay.assert_called_with(*case["expected"])

def test_get_additional_permissions(self):
# Should return the list of the plugin custom permissions
Expand Down

0 comments on commit 135c57f

Please sign in to comment.