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

Update lock handling #90

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 2 additions & 9 deletions yalesmartalarmclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,9 @@ def get_all_devices(self, retry: int = 3) -> dict[str, Any]:
raise
return cast(dict[str, Any], devices["data"])

def get_locks(self, retry: int = 3) -> list[YaleLock]:
def get_locks(self) -> list[YaleLock]:
"""Return all locks."""
try:
return list(self.lock_api.locks())
except Exception:
_LOGGER.debug("Retry %d on path self.lock_api.locks()", 4 - retry)
if retry > 0:
time.sleep(5)
return self.get_locks(retry - 1)
raise
return self.lock_api.locks

def get_cycle(self, retry: int = 3) -> dict[str, Any]:
"""Return full cycle."""
Expand Down
27 changes: 25 additions & 2 deletions yalesmartalarmclient/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ class YaleDoorManAPI:
def __init__(self, auth: YaleAuth) -> None:
"""Initialize the module."""
self.auth = auth
self.locks = list(self.get_locks())

def locks(self) -> Iterator[YaleLock]:
def get_locks(self) -> Iterator[YaleLock]:
"""Iterate through the locks we have available for this user.

Yields: the locks
Expand All @@ -269,6 +270,28 @@ def locks(self) -> Iterator[YaleLock]:
lock = YaleLock(device, lock_api=self)
yield lock

def update_locks(self, devices: list[dict[str, Any]]) -> None:
"""Update the locks we have available for this user.

Args:
devices: list of device data

Returns:
None

Example:
>>> from yalesmartalarmclient.client import YaleSmartAlarmClient
>>> client = YaleSmartAlarmClient(username="", password="")
>>> client.update_locks(devices)

"""

for device in devices:
if device["type"] == YaleLock.DEVICE_TYPE:
lock = self.get(device["name"])
if lock:
lock.update(device)

def get(self, name: str) -> YaleLock | None:
"""Get a single lock with matching name.

Expand All @@ -287,7 +310,7 @@ def get(self, name: str) -> YaleLock | None:
myfrontdoor [YaleLockState.UNLOCKED]

"""
for lock in self.locks():
for lock in self.locks:
if lock == name:
return lock
return None
Expand Down
Loading