Skip to content

Commit

Permalink
Address more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lash-L committed Nov 11, 2024
1 parent 0dd4552 commit e6b413f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion custom_components/bermuda/bermuda_device_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def calculate_data(self):
dist_total += local_min

if dist_total > 0: # Calculate the minimised-windowed-average
movavg = dist_total / len(self.hist_distance_by_interval)
movavg = dist_total / dist_count
else: # we have only a single measurement.
movavg = local_min
# The average is only helpful if it's lower than the actual reading.
Expand Down
43 changes: 28 additions & 15 deletions custom_components/bermuda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
format_mac,
)
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util import slugify
from homeassistant.util.dt import get_age, now
Expand Down Expand Up @@ -212,6 +213,8 @@ def __init__(
self.devices: dict[str, BermudaDevice] = {}
# self.updaters: dict[str, BermudaPBDUCoordinator] = {}

# Run it once so it will schedule itself in the future.
hass.loop.call_soon_threadsafe(hass.async_create_task, self.purge_redactions(hass))
self.area_reg = ar.async_get(hass)

# Restore the scanners saved in config entry data. We maintain
Expand Down Expand Up @@ -1239,13 +1242,15 @@ def redaction_list_update(self):
i = len(self.redactions) # not entirely accurate but we don't care.

# SCANNERS
for address in self.scanner_list:
if address.upper() not in self.redactions:
for non_lower_address in self.scanner_list:
address = non_lower_address.lower()
if address not in self.redactions:
i += 1
self.redactions[address.upper()] = f"{address[:2]}::SCANNER_{i}::{address[-2:]}"
self.redactions[address] = f"{address[:2]}::SCANNER_{i}::{address[-2:]}"
# CONFIGURED DEVICES
for address in self.options.get(CONF_DEVICES, []):
if address.upper() not in self.redactions:
for non_lower_address in self.options.get(CONF_DEVICES, []):
address = non_lower_address.lower()
if address not in self.redactions:
i += 1
if address.count("_") == 2:
self.redactions[address] = f"{address[:4]}::CFG_iBea_{i}::{address[32:]}"
Expand All @@ -1255,8 +1260,9 @@ def redaction_list_update(self):
# Don't know what it is, but not a mac.
self.redactions[address] = f"CFG_OTHER_{1}_{address}"
# EVERYTHING ELSE
for address, device in self.devices.items():
if address.upper() not in self.redactions:
for non_lower_address, device in self.devices.items():
address = non_lower_address.lower()
if address not in self.redactions:
# Only add if they are not already there.
i += 1
if device.address_type == ADDR_TYPE_PRIVATE_BLE_DEVICE:
Expand All @@ -1269,6 +1275,15 @@ def redaction_list_update(self):
# Don't know what it is.
self.redactions[address] = f"OTHER_{1}_{address}"

async def purge_redactions(self, hass: HomeAssistant):
"""Empty redactions and free up some memory."""
self.redactions = {}
async_call_later(
hass,
1 * 1 * 10,
lambda _: hass.loop.call_soon_threadsafe(hass.async_create_task, self.purge_redactions(hass)),
)

def redact_data(self, data):
"""
Wash any collection of data of any MAC addresses.
Expand All @@ -1281,17 +1296,15 @@ def redact_data(self, data):
# Initialise the list of addresses if not already done.
self.redaction_list_update()
if isinstance(data, str):
data = data.lower()
# the end of the recursive wormhole, do the actual work:
if ":" in data:
if data not in self.redactions:
if data.upper() not in self.redactions:
for find, fix in list(self.redactions.items()):
if find in data:
self.redactions[data] = re.sub(find, fix, data, flags=re.IGNORECASE)
data = self.redactions[data]
break
else:
data = self.redactions[data.upper()]
for find, fix in list(self.redactions.items()):
if find in data:
self.redactions[data] = data.replace(find, fix)
data = self.redactions[data]
break
else:
data = self.redactions[data]
# redactions done, now replace any remaining MAC addresses
Expand Down

0 comments on commit e6b413f

Please sign in to comment.