Skip to content

Commit

Permalink
Moving the disabled relay get status handling into on_api_command met…
Browse files Browse the repository at this point in the history
…hod.
  • Loading branch information
RobinTail committed Oct 19, 2023
1 parent 2f412a9 commit 6498291
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 7 additions & 2 deletions octoprint_octorelay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ def handle_list_all_command(self):
def handle_get_status_command(self, index: str) -> bool:
self._logger.debug(f"Getting the relay {index} state")
settings = self._settings.get([index], merged=True) # expensive
if not bool(settings["active"]):
raise HandlingException(400)
return Relay(
int(settings["relay_pin"] or 0),
bool(settings["inverted_output"])
).is_closed() if bool(settings["active"]) else False
).is_closed()

def handle_update_command(self, index: str, target: Optional[bool] = None) -> bool:
self._logger.debug(f"Requested to switch the relay {index} to {target}")
Expand Down Expand Up @@ -154,7 +156,10 @@ def on_api_command(self, command, data):
self._logger.info(f"Responding to {LIST_ALL_COMMAND} command: {response}")
return flask.jsonify(response)
if command == GET_STATUS_COMMAND: # API command to get relay status
is_closed = self.handle_get_status_command(data["pin"])
try:
is_closed = self.handle_get_status_command(data["pin"])
except HandlingException:
is_closed = False # todo should just abort in the next version
self._logger.info(f"Responding to {GET_STATUS_COMMAND} command: {is_closed}")
return flask.jsonify({"status": is_closed})
if command == UPDATE_COMMAND: # API command to toggle the relay
Expand Down
22 changes: 21 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,19 @@ def test_handle_get_status_command(self):
)
self.plugin_instance._settings.get.assert_called_with(["r4"], merged=True)

def test_handle_get_status_command__exception(self):
relay_settings_mock = {
"active": False,
"relay_pin": 17,
"inverted_output": False,
"label_text": "TEST",
"cmd_on": "CommandOnMock",
"cmd_off": "CommandOffMock"
}
self.plugin_instance._settings.get = Mock(return_value=relay_settings_mock)
with self.assertRaises(HandlingException):
self.plugin_instance.handle_get_status_command("r4")

@patch("os.system")
def test_handle_update_command(self, system_mock):
# Should toggle the relay state, execute command, update UI and return the resulting state
Expand Down Expand Up @@ -1031,7 +1044,7 @@ def test_on_api_command(self, jsonify_mock):
@patch("flask.abort")
@patch("flask.jsonify")
def test_on_api_command__update_exceptions(self, jsonify_mock, abort_mock):
# Should respond with a faulty HTTP code when handler raises
# Should respond with a faulty HTTP code or status error when handler raises
cases = [
{ "status": 403, "expectedMethod": abort_mock, "expectedArgument": 403 },
{ "status": 400, "expectedMethod": jsonify_mock, "expectedArgument": {"status": "error"} }
Expand All @@ -1041,6 +1054,13 @@ def test_on_api_command__update_exceptions(self, jsonify_mock, abort_mock):
self.plugin_instance.on_api_command("update", {"pin": "r4"})
case["expectedMethod"].assert_called_with(case["expectedArgument"])

@patch("flask.jsonify")
def test_om_api_command__get_status_exception(self, jsonify_mock):
# Should respond with status false
self.plugin_instance.handle_get_status_command = Mock(side_effect=HandlingException(400))
self.plugin_instance.on_api_command("getStatus", {"pin": "r4"})
jsonify_mock.assert_called_with({"status": False})

@patch("flask.abort")
def test_on_api_command__unknown(self, abort_mock):
# Should respond with status code 400 (bad request) to unknown commands
Expand Down

0 comments on commit 6498291

Please sign in to comment.