From dedc352b9babcac198821003bcbc6c79a99f57e0 Mon Sep 17 00:00:00 2001 From: Jevgeni Kiski Date: Mon, 23 Sep 2024 12:09:28 +0300 Subject: [PATCH] #503 Better invalid command handling. --- paradox/exceptions.py | 6 ++++++ paradox/interfaces/text/core.py | 18 +++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/paradox/exceptions.py b/paradox/exceptions.py index fdd36ee7..90c816dd 100644 --- a/paradox/exceptions.py +++ b/paradox/exceptions.py @@ -35,9 +35,11 @@ class PAICriticalException(PAIException): class AuthenticationFailed(PAICriticalException): pass + class CodeLockout(PAICriticalException): pass + class PanelNotDetected(PAICriticalException): pass @@ -46,6 +48,10 @@ class SerialConnectionOpenFailed(PAICriticalException): pass +class InvalidCommand(PAIException): + pass + + def async_loop_unhandled_exception_handler(loop, context): exception = context.get("exception") diff --git a/paradox/interfaces/text/core.py b/paradox/interfaces/text/core.py index 2408f6ce..0cee55da 100644 --- a/paradox/interfaces/text/core.py +++ b/paradox/interfaces/text/core.py @@ -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 @@ -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": @@ -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", @@ -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):