Skip to content

Commit

Permalink
Load cached files as defaultdicts (#1988)
Browse files Browse the repository at this point in the history
SukramJ authored Jan 15, 2025

Verified

This commit was signed with the committer’s verified signature.
nilsingwersen Nils Ingwersen
1 parent 6b688ab commit 71aa870
Showing 4 changed files with 19 additions and 4 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.9 (2025-01-15)

- Load cached files as defaultdicts

# Version 2025.1.8 (2025-01-15)

- Improve defaultdict usage
4 changes: 3 additions & 1 deletion hahomematic/caches/persistent.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
from collections.abc import Mapping
from datetime import datetime
from functools import lru_cache
import json
import logging
import os
from typing import Any, Final
@@ -30,6 +31,7 @@
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,
@@ -111,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 = orjson.loads(file_pointer.read())
data = json.loads(file_pointer.read(), object_hook=defaultdict_from_dict)
if (converted_hash := hash_sha256(value=data)) == self.last_hash_saved:
return DataOperationResult.NO_LOAD
self._persistent_cache.clear()
2 changes: 1 addition & 1 deletion hahomematic/const.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
import re
from typing import Any, Final, NamedTuple, Required, TypedDict

VERSION: Final = "2025.1.8"
VERSION: Final = "2025.1.9"

# default
DEFAULT_CUSTOM_ID: Final = "custom_id"
13 changes: 11 additions & 2 deletions hahomematic/support.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
from __future__ import annotations

import base64
from collections.abc import Collection, Set as AbstractSet
from collections import defaultdict
from collections.abc import Callable, Collection, Set as AbstractSet
import contextlib
from dataclasses import dataclass
from datetime import datetime
@@ -16,7 +17,7 @@
import socket
import ssl
import sys
from typing import Any, Final
from typing import Any, Final, cast

from hahomematic import client as hmcl
from hahomematic.const import (
@@ -190,6 +191,14 @@ def check_password(password: str | None) -> bool:
return True


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


def get_tls_context(verify_tls: bool) -> ssl.SSLContext:
"""Return tls verified/unverified ssl/tls context."""
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)

0 comments on commit 71aa870

Please sign in to comment.