Skip to content

Commit dedc352

Browse files
committed
#503 Better invalid command handling.
1 parent 4f0e887 commit dedc352

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

paradox/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ class PAICriticalException(PAIException):
3535
class AuthenticationFailed(PAICriticalException):
3636
pass
3737

38+
3839
class CodeLockout(PAICriticalException):
3940
pass
4041

42+
4143
class PanelNotDetected(PAICriticalException):
4244
pass
4345

@@ -46,6 +48,10 @@ class SerialConnectionOpenFailed(PAICriticalException):
4648
pass
4749

4850

51+
class InvalidCommand(PAIException):
52+
pass
53+
54+
4955
def async_loop_unhandled_exception_handler(loop, context):
5056
exception = context.get("exception")
5157

paradox/interfaces/text/core.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from paradox.config import config as cfg
44
from paradox.event import Event, EventLevel, Notification
5+
from paradox.exceptions import InvalidCommand
56
from paradox.interfaces import AsyncInterface
67
from paradox.lib import ps
78
from paradox.lib.event_filter import EventFilter, EventTagFilter, LiveEventRegexpFilter
@@ -61,7 +62,7 @@ async def handle_command(self, message_raw):
6162

6263
element_type = tokens[0].lower()
6364
element = tokens[1]
64-
command = self.normalize_payload(tokens[2].lower())
65+
command = self.normalize_command(tokens[2].lower())
6566

6667
# Process a Zone Command
6768
if element_type == "zone":
@@ -91,16 +92,15 @@ async def handle_command(self, message_raw):
9192
logger.info(f"OK: {message_raw}")
9293
return "OK"
9394

94-
# TODO: Remove this (to panels?)
9595
@staticmethod
96-
def normalize_payload(message):
97-
message = message.strip().lower()
96+
def normalize_command(command):
97+
command = command.strip().lower()
9898

99-
if message in ["true", "on", "1", "enable"]:
99+
if command in ["true", "on", "1", "enable"]:
100100
return "on"
101-
elif message in ["false", "off", "0", "disable"]:
101+
elif command in ["false", "off", "0", "disable"]:
102102
return "off"
103-
elif message in [
103+
elif command in [
104104
"pulse",
105105
"arm",
106106
"disarm",
@@ -109,9 +109,9 @@ def normalize_payload(message):
109109
"bypass",
110110
"clear_bypass",
111111
]:
112-
return message
112+
return command
113113

114-
return None
114+
raise InvalidCommand(f'Invalid command: "{command}"')
115115

116116

117117
class ConfiguredAbstractTextInterface(AbstractTextInterface):

0 commit comments

Comments
 (0)