Skip to content

Commit

Permalink
fix: small tweaks to compact code (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 23, 2024
1 parent 0b601f7 commit aa136ba
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/uiprotect/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,28 +582,24 @@ def revert_changes(self, data_before_changes: dict[str, Any]) -> None:
setattr(self, key, data_before_changes[key])

def can_create(self, user: User) -> bool:
if self.model is None:
return True

return user.can(self.model, PermissionNode.CREATE, self)
if (model := self.model) is not None:
return user.can(model, PermissionNode.CREATE, self)
return True

def can_read(self, user: User) -> bool:
if self.model is None:
return True

return user.can(self.model, PermissionNode.READ, self)
if (model := self.model) is not None:
return user.can(model, PermissionNode.READ, self)
return True

def can_write(self, user: User) -> bool:
if self.model is None:
return True

return user.can(self.model, PermissionNode.WRITE, self)
if (model := self.model) is not None:
return user.can(model, PermissionNode.WRITE, self)
return True

def can_delete(self, user: User) -> bool:
if self.model is None:
return True

return user.can(self.model, PermissionNode.DELETE, self)
if (model := self.model) is not None:
return user.can(model, PermissionNode.DELETE, self)
return True

async def queue_update(self, callback: Callable[[], None]) -> None:
"""
Expand All @@ -615,9 +611,8 @@ async def queue_update(self, callback: Callable[[], None]) -> None:
self._update_sync.queue.put_nowait(callback)

self._update_sync.event.set()
await asyncio.sleep(
0.001,
) # release execution so other `queue_update` calls can abort
# release execution so other `queue_update` calls can abort
await asyncio.sleep(0.001)
self._update_sync.event.clear()

try:
Expand Down Expand Up @@ -930,8 +925,8 @@ def _get_unifi_remaps(cls) -> dict[str, str]:
}

async def _api_update(self, data: dict[str, Any]) -> None:
if self.model is not None:
return await self._api.update_device(self.model, self.id, data)
if (model := self.model) is not None:
return await self._api.update_device(model, self.id, data)
return None

def unifi_dict(
Expand Down Expand Up @@ -974,10 +969,9 @@ def is_bluetooth(self) -> bool:

@property
def bridge(self) -> Bridge | None:
if self.bridge_id is None:
return None

return self._api.bootstrap.bridges[self.bridge_id]
if (bridge_id := self.bridge_id) is not None:
return self._api.bootstrap.bridges[bridge_id]
return None

@property
def protect_url(self) -> str:
Expand Down Expand Up @@ -1066,7 +1060,6 @@ def unifi_dict(

@property
def last_motion_event(self) -> Event | None:
if self.last_motion_event_id is None:
return None

return self._api.bootstrap.events.get(self.last_motion_event_id)
if (last_motion_event_id := self.last_motion_event_id) is not None:
return self._api.bootstrap.events.get(last_motion_event_id)
return None

0 comments on commit aa136ba

Please sign in to comment.