From 9da56d2c0f094d31b0cf8cba07c4c07fd96c64ea Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 11 Jun 2024 22:51:56 -0500 Subject: [PATCH] refactor: consolidate logic to remove keys (#44) --- src/uiprotect/data/bootstrap.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/uiprotect/data/bootstrap.py b/src/uiprotect/data/bootstrap.py index 12afb3f0..b4c83480 100644 --- a/src/uiprotect/data/bootstrap.py +++ b/src/uiprotect/data/bootstrap.py @@ -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"), @@ -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 @@ -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) @@ -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