Skip to content

Commit 7c9cb42

Browse files
committed
Improve code
1 parent 11fe01f commit 7c9cb42

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

plugwise/data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ def _detect_low_batteries(
7474
self, device_id: str, device: DeviceData, data: DeviceData
7575
) -> list[str]:
7676
"""Helper-function updating the battery_state binary_sensor status from a Battery-is-low message."""
77-
matches = ["Battery", "below"]
7877
mac_address_list: list[str] = []
78+
mac_pattern = "(?:[0-9A-F]{2}){7}(?:[0-9A-F]{2})"
79+
matches = ["Battery", "below"]
7980
if self._notifications:
8081
for msg_id, notification in list(self._notifications.items()):
8182
mac_address: str | None = None
82-
if "message" in notification and all(x in (msg := notification.get("message")) for x in matches):
83-
mac_pattern = "(?:[0-9A-F]{2}){7}(?:[0-9A-F]{2})"
84-
if msg is not None:
85-
mac_address = re.findall(mac_pattern, msg)[0] # re.findall() outputs a list
83+
message: str | None = notification.get("message")
84+
if message is not None and all(x in message for x in matches):
85+
mac_address = re.findall(mac_pattern, message)[0] # re.findall() outputs a list
8686

8787
if mac_address is not None:
8888
self._notifications.pop(msg_id)

0 commit comments

Comments
 (0)