From 20c86eb6591b388713406af2a1fa79ea57432022 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Sun, 2 Sep 2018 21:21:26 +0000 Subject: [PATCH] Factor out string to DoorState conversion, and support present command Signed-off-by: Ralf Ramsauer --- doorlockd.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/doorlockd.py b/doorlockd.py index b613725..99c45f9 100755 --- a/doorlockd.py +++ b/doorlockd.py @@ -138,6 +138,16 @@ class DoorState(Enum): Present = 2 Closed = 3 + def from_string(string): + if string == 'lock': + return DoorState.Closed + elif string == 'unlock': + return DoorState.Open + elif string == 'present': + return DoorState.Present + + return None + def is_open(self): if self != DoorState.Closed: return True @@ -429,15 +439,11 @@ def api(): 'Invalid username or password format') credentials = AuthMethod.LDAP_USER_PW, user, password - desired_state = None if command == 'status': return json_response(logic.try_auth(credentials)) - elif command == 'lock': - desired_state = DoorState.Closed - elif command == 'unlock': - desired_state = DoorState.Open + desired_state = DoorState.from_string(command) if not desired_state: return json_response(LogicResponse.Inval, "Invalid command requested")