Skip to content

Commit

Permalink
#503 Better invalid command handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 committed Sep 23, 2024
1 parent 4f0e887 commit dedc352
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 6 additions & 0 deletions paradox/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class PAICriticalException(PAIException):
class AuthenticationFailed(PAICriticalException):
pass


class CodeLockout(PAICriticalException):
pass


class PanelNotDetected(PAICriticalException):
pass

Expand All @@ -46,6 +48,10 @@ class SerialConnectionOpenFailed(PAICriticalException):
pass


class InvalidCommand(PAIException):
pass


def async_loop_unhandled_exception_handler(loop, context):
exception = context.get("exception")

Expand Down
18 changes: 9 additions & 9 deletions paradox/interfaces/text/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from paradox.config import config as cfg
from paradox.event import Event, EventLevel, Notification
from paradox.exceptions import InvalidCommand
from paradox.interfaces import AsyncInterface
from paradox.lib import ps
from paradox.lib.event_filter import EventFilter, EventTagFilter, LiveEventRegexpFilter
Expand Down Expand Up @@ -61,7 +62,7 @@ async def handle_command(self, message_raw):

element_type = tokens[0].lower()
element = tokens[1]
command = self.normalize_payload(tokens[2].lower())
command = self.normalize_command(tokens[2].lower())

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

# TODO: Remove this (to panels?)
@staticmethod
def normalize_payload(message):
message = message.strip().lower()
def normalize_command(command):
command = command.strip().lower()

if message in ["true", "on", "1", "enable"]:
if command in ["true", "on", "1", "enable"]:
return "on"
elif message in ["false", "off", "0", "disable"]:
elif command in ["false", "off", "0", "disable"]:
return "off"
elif message in [
elif command in [
"pulse",
"arm",
"disarm",
Expand All @@ -109,9 +109,9 @@ def normalize_payload(message):
"bypass",
"clear_bypass",
]:
return message
return command

return None
raise InvalidCommand(f'Invalid command: "{command}"')


class ConfiguredAbstractTextInterface(AbstractTextInterface):
Expand Down

0 comments on commit dedc352

Please sign in to comment.