From 7b496cb72b3b5efffad18bb86f58355e910122e7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 16 Jun 2024 22:06:08 -0500 Subject: [PATCH] refactor: reduce code to remove keys (#78) --- src/uiprotect/data/base.py | 15 ++++------ src/uiprotect/data/devices.py | 55 +++++++++++++++-------------------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/uiprotect/data/base.py b/src/uiprotect/data/base.py index bbe48eb3..72a0e056 100644 --- a/src/uiprotect/data/base.py +++ b/src/uiprotect/data/base.py @@ -965,16 +965,13 @@ def unifi_dict( exclude: set[str] | None = None, ) -> dict[str, Any]: data = super().unifi_dict(data=data, exclude=exclude) - - if "wiredConnectionState" in data and data["wiredConnectionState"] is None: - del data["wiredConnectionState"] - if "wifiConnectionState" in data and data["wifiConnectionState"] is None: - del data["wifiConnectionState"] - if ( - "bluetoothConnectionState" in data - and data["bluetoothConnectionState"] is None + for key in ( + "wiredConnectionState", + "wifiConnectionState", + "bluetoothConnectionState", ): - del data["bluetoothConnectionState"] + if key in data and data[key] is None: + del data[key] return data @classmethod diff --git a/src/uiprotect/data/devices.py b/src/uiprotect/data/devices.py index 7ed3307d..5c6525d6 100644 --- a/src/uiprotect/data/devices.py +++ b/src/uiprotect/data/devices.py @@ -1062,27 +1062,21 @@ def unifi_dict( ] data = super().unifi_dict(data=data, exclude=exclude) + for key in ( + "lastRingEventId", + "lastSmartDetect", + "lastSmartAudioDetect", + "lastSmartDetectEventId", + "lastSmartAudioDetectEventId", + "lastSmartDetects", + "lastSmartAudioDetects", + "lastSmartDetectEventIds", + "lastSmartAudioDetectEventIds", + "talkbackStream", + ): + if key in data: + del data[key] - if "lastRingEventId" in data: - del data["lastRingEventId"] - if "lastSmartDetect" in data: - del data["lastSmartDetect"] - if "lastSmartAudioDetect" in data: - del data["lastSmartAudioDetect"] - if "lastSmartDetectEventId" in data: - del data["lastSmartDetectEventId"] - if "lastSmartAudioDetectEventId" in data: - del data["lastSmartAudioDetectEventId"] - if "lastSmartDetects" in data: - del data["lastSmartDetects"] - if "lastSmartAudioDetects" in data: - del data["lastSmartAudioDetects"] - if "lastSmartDetectEventIds" in data: - del data["lastSmartDetectEventIds"] - if "lastSmartAudioDetectEventIds" in data: - del data["lastSmartAudioDetectEventIds"] - if "talkbackStream" in data: - del data["talkbackStream"] if "lcdMessage" in data and data["lcdMessage"] is None: data["lcdMessage"] = {} @@ -2820,18 +2814,15 @@ def unifi_dict( exclude: set[str] | None = None, ) -> dict[str, Any]: data = super().unifi_dict(data=data, exclude=exclude) - - if "lastMotionEventId" in data: - del data["lastMotionEventId"] - if "lastContactEventId" in data: - del data["lastContactEventId"] - if "lastValueEventId" in data: - del data["lastValueEventId"] - if "lastAlarmEventId" in data: - del data["lastAlarmEventId"] - if "extremeValueDetectedAt" in data: - del data["extremeValueDetectedAt"] - + for key in ( + "lastMotionEventId", + "lastContactEventId", + "lastValueEventId", + "lastAlarmEventId", + "extremeValueDetectedAt", + ): + if key in data: + del data[key] return data @property