Skip to content

Commit

Permalink
Cleanup ParamsetDescriptionCache
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ committed Aug 20, 2024
1 parent ecc116e commit 2da1a9b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 2024.8.10 (2024-08-20)

- Cleanup ParamsetDescriptionCache

# Version 2024.8.9 (2024-08-20)

- Avoid excessive memory usage in cache
Expand Down
43 changes: 24 additions & 19 deletions hahomematic/caches/persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def __init__(self, central: hmcu.CentralUnit) -> None:
)

# {(device_address, parameter), [channel_no]}
self._address_parameter_cache: Final[dict[tuple[str, str], set[int]]] = {}
self._address_parameter_cache: Final[dict[tuple[str, str], set[int | None]]] = {}

@property
def raw_paramset_descriptions(self) -> dict[str, dict[str, dict[str, dict[str, Any]]]]:
Expand All @@ -312,12 +312,17 @@ def add(
paramset_description
)

self._add_address_parameter(
channel_address=channel_address, paramsets=[paramset_description]
)

async def remove_device(self, device: HmDevice) -> None:
"""Remove device paramset descriptions from cache."""
if interface := self._raw_paramset_descriptions.get(device.interface_id):
for channel_address in device.channels:
if channel_address in interface:
del self._raw_paramset_descriptions[device.interface_id][channel_address]

await self.save()

def has_interface_id(self, interface_id: str) -> bool:
Expand Down Expand Up @@ -358,7 +363,7 @@ def is_in_multiple_channels(self, channel_address: str, parameter: str) -> bool:
if channels := self._address_parameter_cache.get(
(get_device_address(channel_address), parameter)
):
return len(set(channels)) > 1
return len(channels) > 1
return False

def get_channel_addresses_by_paramset_key(
Expand Down Expand Up @@ -387,20 +392,22 @@ def _init_address_parameter_list(self) -> None:
"""
for channel_paramsets in self._raw_paramset_descriptions.values():
for channel_address, paramsets in channel_paramsets.items():
device_address, channel_no = get_split_channel_address(channel_address)
if not channel_no:
continue

for paramset in paramsets.values():
if not paramset:
continue
for parameter in paramset:
if (
device_address,
parameter,
) not in self._address_parameter_cache:
self._address_parameter_cache[(device_address, parameter)] = set()
self._address_parameter_cache[(device_address, parameter)].add(channel_no)
self._add_address_parameter(
channel_address=channel_address, paramsets=list(paramsets.values())
)

def _add_address_parameter(
self, channel_address: str, paramsets: list[dict[str, Any]]
) -> None:
"""Add address parameter to cache."""
device_address, channel_no = get_split_channel_address(channel_address)
for paramset in paramsets:
if not paramset:
continue
for parameter in paramset:
if (device_address, parameter) not in self._address_parameter_cache:
self._address_parameter_cache[(device_address, parameter)] = set()
self._address_parameter_cache[(device_address, parameter)].add(channel_no)

async def load(self) -> DataOperationResult:
"""Load paramset descriptions from disk into paramset cache."""
Expand All @@ -413,6 +420,4 @@ async def load(self) -> DataOperationResult:

async def save(self) -> DataOperationResult:
"""Save current paramset descriptions to disk."""
result = await super().save()
self._init_address_parameter_list()
return result
return await super().save()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hahomematic"
version = "2024.8.9"
version = "2024.8.10"
license = {text = "MIT License"}
description = "Homematic interface for Home Assistant running on Python 3."
readme = "README.md"
Expand Down

0 comments on commit 2da1a9b

Please sign in to comment.