Skip to content

Commit

Permalink
fix(log): debug log for arm/disarm/lock/unlock responses
Browse files Browse the repository at this point in the history
  • Loading branch information
palazzem committed Feb 5, 2024
1 parent ad9de56 commit 8092d1b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/elmo/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from requests.exceptions import HTTPError

from .. import query as q
from ..__about__ import __version__
from ..utils import _camel_to_snake_case, _sanitize_session_id
from .decorators import require_lock, require_session
from .exceptions import (
Expand Down Expand Up @@ -48,6 +49,7 @@ def __init__(self, base_url=None, domain=None, session_id=None):
self._panel = None
self._lock = Lock()
# Debug
_LOGGER.debug(f"Client | Library version: {__version__}")
_LOGGER.debug(f"Client | Router: {self._router._base_url}")
_LOGGER.debug(f"Client | Domain: {self._domain}")

Expand Down Expand Up @@ -174,6 +176,7 @@ def lock(self, code, user_id=1):
# Main units that do not require a userId param, expects userId to be "1"
payload = {"userId": user_id, "password": code, "sessionId": self._session_id}
response = self._session.post(self._router.lock, data=payload)
_LOGGER.debug(f"Client | Lock response: {response.text}")

try:
response.raise_for_status()
Expand All @@ -192,7 +195,7 @@ def lock(self, code, user_id=1):
raise CodeError

self._lock.acquire()
_LOGGER.debug(f"Client | Lock acquired with response {body}")
_LOGGER.debug("Client | Lock successful")
try:
yield self
finally:
Expand All @@ -216,9 +219,10 @@ def unlock(self):
"""
payload = {"sessionId": self._session_id}
response = self._session.post(self._router.unlock, data=payload)
_LOGGER.debug(f"Client | Unlock response: {response.text}")
response.raise_for_status()

_LOGGER.debug(f"Client | Lock released with response {response.text}")
_LOGGER.debug("Client | Unlock successful")
# Release the lock only in case of success, so that if it fails
# the owner of the lock can properly unlock the system again
# (maybe with a retry)
Expand Down Expand Up @@ -267,15 +271,15 @@ def arm(self, sectors=None):

# Send the payload to arm sectors
response = self._session.post(self._router.send_command, data=payload)
_LOGGER.debug(f"Client | Arm response: {response.text}")
response.raise_for_status()
body = response.json()

# Errors returns 200 with "Successful == False" JSON key
if not body[0]["Successful"]:
_LOGGER.error(f"Client | Arming response: {body}")
raise CommandError

_LOGGER.debug(f"Client | Arming successful with response: {body}")
_LOGGER.debug("Client | Arm successful")
return True

@require_session
Expand Down Expand Up @@ -321,15 +325,15 @@ def disarm(self, sectors=None):

# Send the payload to disarm sectors
response = self._session.post(self._router.send_command, data=payload)
_LOGGER.debug(f"Client | Disarm response: {response.text}")
response.raise_for_status()
body = response.json()

# Errors returns 200 with "Successful == False" JSON key
if not body[0]["Successful"]:
_LOGGER.error(f"Client | Disarming response: {body}")
raise CommandError

_LOGGER.debug(f"Client | Disarming successful with response: {body}")
_LOGGER.debug("Client | Disarm successful")
return True

@require_session
Expand Down

0 comments on commit 8092d1b

Please sign in to comment.