Skip to content

Commit

Permalink
Return defaultdict from list_devices (#1990)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ authored Jan 17, 2025
1 parent 71aa870 commit 81fdbf5
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 2025.1.10 (2025-01-17)

- Return regular dict from list_devices

# Version 2025.1.9 (2025-01-15)

- Load cached files as defaultdicts
Expand Down
18 changes: 9 additions & 9 deletions hahomematic/caches/persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
from hahomematic.model.device import Device
from hahomematic.support import (
check_or_create_directory,
defaultdict_from_dict,
delete_file,
get_device_address,
get_split_channel_address,
hash_sha256,
regular_to_default_dict_hook,
)

_LOGGER: Final = logging.getLogger(__name__)
Expand All @@ -49,7 +49,7 @@ class BasePersistentCache(ABC):
def __init__(
self,
central: hmcu.CentralUnit,
persistent_cache: defaultdict[str, Any],
persistent_cache: dict[str, Any],
) -> None:
"""Initialize the base class of the persistent cache."""
self._save_load_semaphore: Final = asyncio.Semaphore()
Expand Down Expand Up @@ -113,7 +113,7 @@ async def load(self) -> DataOperationResult:

def _perform_load() -> DataOperationResult:
with open(file=self._file_path, encoding=UTF_8) as file_pointer:
data = json.loads(file_pointer.read(), object_hook=defaultdict_from_dict)
data = json.loads(file_pointer.read(), object_hook=regular_to_default_dict_hook)
if (converted_hash := hash_sha256(value=data)) == self.last_hash_saved:
return DataOperationResult.NO_LOAD
self._persistent_cache.clear()
Expand Down Expand Up @@ -145,15 +145,15 @@ class DeviceDescriptionCache(BasePersistentCache):
def __init__(self, central: hmcu.CentralUnit) -> None:
"""Initialize the device description cache."""
# {interface_id, [device_descriptions]}
self._raw_device_descriptions: Final[defaultdict[str, list[DeviceDescription]]] = defaultdict(list)
self._raw_device_descriptions: Final[dict[str, list[DeviceDescription]]] = defaultdict(list)
super().__init__(
central=central,
persistent_cache=self._raw_device_descriptions,
)
# {interface_id, {device_address, [channel_address]}}
self._addresses: Final[defaultdict[str, defaultdict[str, set[str]]]] = defaultdict(lambda: defaultdict(set))
self._addresses: Final[dict[str, dict[str, set[str]]]] = defaultdict(lambda: defaultdict(set))
# {interface_id, {address, device_descriptions}}
self._device_descriptions: Final[defaultdict[str, dict[str, DeviceDescription]]] = defaultdict(dict)
self._device_descriptions: Final[dict[str, dict[str, DeviceDescription]]] = defaultdict(dict)

def add_device(self, interface_id: str, device_description: DeviceDescription) -> None:
"""Add a device to the cache."""
Expand Down Expand Up @@ -269,9 +269,9 @@ class ParamsetDescriptionCache(BasePersistentCache):
def __init__(self, central: hmcu.CentralUnit) -> None:
"""Init the paramset description cache."""
# {interface_id, {channel_address, paramsets}}
self._raw_paramset_descriptions: Final[
defaultdict[str, defaultdict[str, defaultdict[ParamsetKey, dict[str, ParameterData]]]]
] = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
self._raw_paramset_descriptions: Final[dict[str, dict[str, dict[ParamsetKey, dict[str, ParameterData]]]]] = (
defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
)
super().__init__(
central=central,
persistent_cache=self._raw_paramset_descriptions,
Expand Down
8 changes: 4 additions & 4 deletions hahomematic/caches/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ def __init__(
self._custom_un_ignore_values_parameters: Final[set[str]] = set()

# model, channel_no, paramset_key, parameter
self._custom_un_ignore_complex: Final[
defaultdict[str, defaultdict[int | str | None, defaultdict[str, set[str]]]]
] = defaultdict(lambda: defaultdict(lambda: defaultdict(set)))
self._custom_un_ignore_complex: Final[dict[str, dict[int | str | None, dict[str, set[str]]]]] = defaultdict(
lambda: defaultdict(lambda: defaultdict(set))
)
self._ignore_custom_device_definition_models: Final[tuple[str, ...]] = (
central.config.ignore_custom_device_definition_models
)
Expand All @@ -294,7 +294,7 @@ def __init__(
] = defaultdict(lambda: defaultdict(lambda: defaultdict(set)))

# model, channel_no
self._relevant_master_paramsets_by_device: Final[defaultdict[str, set[int | None]]] = defaultdict(set)
self._relevant_master_paramsets_by_device: Final[dict[str, set[int | None]]] = defaultdict(set)
self._init()

def _init(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions hahomematic/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,8 @@ async def _add_new_devices(self, interface_id: str, device_descriptions: tuple[D
await client.fetch_paramset_descriptions(device_description=dev_desc)
save_paramset_descriptions = True
except Exception as ex: # pragma: no cover
save_device_descriptions = False
save_paramset_descriptions = False
_LOGGER.error(
"ADD_NEW_DEVICES failed: %s [%s]",
type(ex).__name__,
Expand Down
4 changes: 2 additions & 2 deletions hahomematic/central/xml_rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import logging
import threading
from typing import Any, Final, cast
from typing import Any, Final
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer

from hahomematic import central as hmcu
Expand Down Expand Up @@ -54,7 +54,7 @@ def error(self, interface_id: str, error_code: str, msg: str) -> None:
def listDevices(self, interface_id: str) -> list[dict[str, Any]]:
"""Return already existing devices to CCU / Homegear."""
if central := self.get_central(interface_id):
return cast(list[dict[str, Any]], central.list_devices(interface_id=interface_id))
return [dict(device_description) for device_description in central.list_devices(interface_id=interface_id)]
return []

def newDevices(self, interface_id: str, device_descriptions: list[dict[str, Any]]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
from typing import Any, Final, NamedTuple, Required, TypedDict

VERSION: Final = "2025.1.9"
VERSION: Final = "2025.1.10"

# default
DEFAULT_CUSTOM_ID: Final = "custom_id"
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def check_password(password: str | None) -> bool:
return True


def defaultdict_from_dict(origin: dict) -> defaultdict[Any, Any]:
def regular_to_default_dict_hook(origin: dict) -> defaultdict[Any, Any]:
"""Use defaultdict in json.loads object_hook."""
new_dict: Callable = lambda: defaultdict(new_dict)
new_instance = new_dict()
Expand Down

0 comments on commit 81fdbf5

Please sign in to comment.