Skip to content

Commit

Permalink
refactor: consolidate logic to remove keys (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 12, 2024
1 parent 49e0a67 commit 9da56d2
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/uiprotect/data/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"recordingSchedules",
}
IGNORE_DEVICE_KEYS = {"nvrMac", "guid"}
STATS_AND_IGNORE_DEVICE_KEYS = STATS_KEYS | IGNORE_DEVICE_KEYS

CAMERA_EVENT_ATTR_MAP: dict[EventType, tuple[str, str]] = {
EventType.MOTION: ("last_motion", "last_motion_event_id"),
Expand All @@ -79,11 +80,6 @@
}


def _remove_stats_keys(data: dict[str, Any]) -> None:
for key in STATS_KEYS.intersection(data):
del data[key]


def _process_light_event(event: Event) -> None:
if event.light is None:
return
Expand Down Expand Up @@ -429,7 +425,8 @@ def _process_nvr_update(
ignore_stats: bool,
) -> WSSubscriptionMessage | None:
if ignore_stats:
_remove_stats_keys(data)
for key in STATS_KEYS.intersection(data):
del data[key]
# nothing left to process
if not data:
self._create_stat(packet, None, True)
Expand Down Expand Up @@ -467,9 +464,10 @@ def _process_device_update(
ignore_stats: bool,
) -> WSSubscriptionMessage | None:
model_type = action["modelKey"]
if ignore_stats:
_remove_stats_keys(data)
for key in IGNORE_DEVICE_KEYS.intersection(data):
remove_keys = (
STATS_AND_IGNORE_DEVICE_KEYS if ignore_stats else IGNORE_DEVICE_KEYS
)
for key in remove_keys.intersection(data):
del data[key]
# `last_motion` from cameras update every 100 milliseconds when a motion event is active
# this overrides the behavior to only update `last_motion` when a new event starts
Expand Down

0 comments on commit 9da56d2

Please sign in to comment.