Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding/updating access codes. #203

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"python.formatting.provider": "black",
"python.pythonPath": "/usr/local/bin/python",
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"cSpell.words": [
"Schlage"
]
}
12 changes: 9 additions & 3 deletions pyschlage/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ def from_json(cls, auth: Auth, device: Device, json: dict[str, Any]) -> AccessCo
else:
schedule = TemporarySchedule.from_json(json)

access_code_length = json.get("accessCodeLength") or 4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be

json.get("accessCodeLength", 4)

(The second argument to get() is the default value if the key is not in the dictionary)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

return AccessCode(
_auth=auth,
_json=json,
_device=device,
access_code_id=json["accesscodeId"],
name=json["friendlyName"],
# TODO: We assume codes are always 4 characters.
code=f"{json['accessCode']:04}",
code=f"{json['accessCode']:0{access_code_length}}",
notify_on_use=bool(json["notification"]),
disabled=bool(json.get("disabled", None)),
schedule=schedule,
Expand All @@ -216,7 +216,9 @@ def to_json(self) -> dict:
json = {
"friendlyName": self.name,
"accessCode": int(self.code),
"accessCodeLength": len(str(self.code)),
rockymountainnative marked this conversation as resolved.
Show resolved Hide resolved
"notification": int(self.notify_on_use),
"notificationEnabled": self.notify_on_use,
"disabled": int(self.disabled),
"activationSecs": _MIN_TIME,
"expirationSecs": _MAX_TIME,
Expand Down Expand Up @@ -245,7 +247,11 @@ def save(self):

command = "updateaccesscode" if self.access_code_id else "addaccesscode"
resp = self._device.send_command(command, self.to_json())
self._update_with(self._device, resp.json())

resp_json = resp.json()
rockymountainnative marked this conversation as resolved.
Show resolved Hide resolved
if "accesscodeId" in resp_json:
self.access_code_id = resp_json["accesscodeId"]

self.device_id = self._device.device_id
if self._notification is None:
self._notification = Notification(
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ def access_code_json():
return {
"accessCode": 123,
"accesscodeId": "__access_code_uuid__",
"accessCodeLength": 4,
"activationSecs": 0,
"disabled": 0,
"expirationSecs": 4294967295,
"friendlyName": "Friendly name",
"notification": 0,
"notificationEnabled": False,
"schedule1": {
"daysOfWeek": "7F",
"endHour": 23,
Expand Down
4 changes: 0 additions & 4 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ def test_save(

new_json = deepcopy(access_code_json)
rockymountainnative marked this conversation as resolved.
Show resolved Hide resolved
new_json["accessCode"] = "1122"
# Simulate another change that happened out of band.
new_json["friendlyName"] = "New name"

with patch(
"pyschlage.code.Notification", autospec=True
Expand All @@ -106,8 +104,6 @@ def test_save(
"updateaccesscode", old_json
)
assert code.code == "1122"
# Ensure the name was updated.
assert code.name == "New name"

def test_delete(self, mock_auth: Mock, access_code_json: dict[str, Any]):
with pytest.raises(NotAuthenticatedError):
Expand Down