Skip to content

Commit

Permalink
Merge pull request #78 from dknowles2/more-lock-attrs
Browse files Browse the repository at this point in the history
Expose a few more lock attributes
  • Loading branch information
dknowles2 committed Jul 30, 2023
2 parents 5ad3a1b + 0939131 commit 813fc23
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
50 changes: 40 additions & 10 deletions pyschlage/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class Lock(Mutable):
is_jammed: bool | None = False
"""Whether the lock has identified itself as jammed or None if lock is unavailable."""

beeper_enabled: bool | None = False
"""Whether the keypress beep is enabled."""

lock_and_leave_enabled: bool | None = False
"""Whether lock-and-leave (a.k.a. "1-Touch Locking) feature is enabled."""

auto_lock_time: int | None = None
"""Time (in seconds) after which the lock will automatically lock itself."""

firmware_version: str | None = None
"""The firmware version installed on the lock or None if lock is unavailable."""

Expand All @@ -67,21 +76,25 @@ def from_json(cls, auth, json):
:meta private:
"""
is_locked = is_jammed = None
if "lockState" in json["attributes"]:
is_locked = json["attributes"]["lockState"] == 1
is_jammed = json["attributes"]["lockState"] == 2
attributes = json["attributes"]
if "lockState" in attributes:
is_locked = attributes["lockState"] == 1
is_jammed = attributes["lockState"] == 2

return cls(
_auth=auth,
device_id=json["deviceId"],
name=json["name"],
model_name=json["modelName"],
device_type=json["devicetypeId"],
battery_level=json["attributes"].get("batteryLevel"),
battery_level=attributes.get("batteryLevel"),
is_locked=is_locked,
is_jammed=is_jammed,
firmware_version=json["attributes"].get("mainFirmwareVersion"),
mac_address=json["attributes"].get("macAddress"),
beeper_enabled=attributes.get("beeperEnabled") == 1,
lock_and_leave_enabled=attributes.get("lockAndLeaveEnabled") == 1,
auto_lock_time=attributes.get("autoLockTime", 0),
firmware_version=attributes.get("mainFirmwareVersion"),
mac_address=attributes.get("macAddress"),
_cat=json["CAT"],
)

Expand All @@ -100,17 +113,20 @@ def refresh(self):
path = self.request_path(self.device_id)
self._update_with(self._auth.request("get", path).json())

def _put_attributes(self, attributes):
path = self.request_path(self.device_id)
json = {"attributes": attributes}
resp = self._auth.request("put", path, json=json)
self._update_with(resp.json())

def _send_command(self, command: str, data=dict):
path = f"{self.request_path(self.device_id)}/commands"
json = {"data": data, "name": command}
self._auth.request("post", path, json=json)

def _toggle(self, lock_state: int):
if self._is_wifi_lock():
path = self.request_path(self.device_id)
json = {"attributes": {"lockState": lock_state}}
resp = self._auth.request("put", path, json=json)
self._update_with(resp.json())
self._put_attributes({"lockState": lock_state})
else:
data = {
"CAT": self._cat,
Expand Down Expand Up @@ -185,3 +201,17 @@ def add_access_code(self, code: AccessCode):
resp = self._auth.request("post", path, json=code.to_json())
code._auth = self._auth
code._update_with(resp.json(), self.device_id)

def set_beeper(self, enabled: bool):
"""Sets the beeper_enabled setting."""
self._put_attributes({"beeperEnabled": 1 if enabled else 0})

def set_lock_and_leave(self, enabled: bool):
"""Sets the lock_and_leave setting."""
self._put_attributes({"lockAndLeave": 1 if enabled else 0})

def set_auto_lock_time(self, auto_lock_time: int):
"""Sets the auto_lock_time setting."""
if auto_lock_time not in (0, 15, 30, 60, 120, 240):
raise ValueError("auto_lock_time must be one of: (0, 15, 30, 60, 120, 240)")
self._put_attributes({"autoLockTime": auto_lock_time})
7 changes: 5 additions & 2 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ def test_from_json(self, mock_auth, lock_json):
assert lock.battery_level == 95
assert lock.is_locked
assert lock._cat == "01234"
assert lock.is_jammed == False
assert lock.is_jammed is False
assert lock.beeper_enabled is True
assert lock.lock_and_leave_enabled is True
assert lock.auto_lock_time == 0
assert lock.firmware_version == "10.00.00264232"
assert lock.mac_address == "AA:BB:CC:00:11:22"

def test_from_json_is_jammed(self, mock_auth, lock_json):
lock_json["attributes"]["lockState"] = 2
lock = Lock.from_json(mock_auth, lock_json)
assert lock.is_locked == False
assert lock.is_locked is False
assert lock.is_jammed

def test_from_json_wifi_lock_unavailable(
Expand Down

0 comments on commit 813fc23

Please sign in to comment.