Skip to content

Commit

Permalink
Remove save option of fetch_paramset_description / Cleanup (#1674)
Browse files Browse the repository at this point in the history
* Remove save option of fetch_paramset_description

* Improve typing

* Align naming

* Align naming
  • Loading branch information
SukramJ authored Aug 28, 2024
1 parent 750c183 commit bd05772
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
21 changes: 16 additions & 5 deletions hahomematic/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,14 @@ def version(self) -> str | None:
self._version = max(versions) if versions else None
return self._version

async def save_caches(self) -> None:
async def save_caches(
self, save_device_descriptions: bool = False, save_paramset_descriptions: bool = False
) -> None:
"""Save persistent caches."""
await self.device_descriptions.save()
await self.paramset_descriptions.save()
if save_device_descriptions:
await self.device_descriptions.save()
if save_paramset_descriptions:
await self.paramset_descriptions.save()

async def start(self) -> None:
"""Start processing of the central unit."""
Expand Down Expand Up @@ -355,7 +359,7 @@ async def stop(self) -> None:
if not self._started:
_LOGGER.debug("STOP: Central %s not started", self._name)
return
await self.save_caches()
await self.save_caches(save_device_descriptions=True, save_paramset_descriptions=True)
self._stop_connection_checker()
await self._stop_clients()
if self.json_rpc_client.is_activated:
Expand Down Expand Up @@ -825,21 +829,28 @@ async def _add_new_devices(
)
)
client = self._clients[interface_id]
save_paramset_descriptions = False
save_device_descriptions = False
for dev_desc in device_descriptions:
try:
self.device_descriptions.add_device_description(
interface_id=interface_id, device_description=dev_desc
)
save_device_descriptions = True
if dev_desc[Description.ADDRESS] not in known_addresses:
await client.fetch_paramset_descriptions(device_description=dev_desc)
save_paramset_descriptions = True
except Exception as err: # pragma: no cover
_LOGGER.error(
"ADD_NEW_DEVICES failed: %s [%s]",
type(err).__name__,
reduce_args(args=err.args),
)

await self.save_caches()
await self.save_caches(
save_device_descriptions=save_device_descriptions,
save_paramset_descriptions=save_paramset_descriptions,
)
if new_device_addresses := self._check_for_new_device_addresses():
await self.device_details.load()
await self.data_cache.load()
Expand Down
15 changes: 6 additions & 9 deletions hahomematic/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def _convert_value(
)

async def fetch_paramset_description(
self, channel_address: str, paramset_key: ParamsetKey, save_to_file: bool = True
self, channel_address: str, paramset_key: ParamsetKey
) -> None:
"""Fetch a specific paramset and add it to the known ones."""
_LOGGER.debug("FETCH_PARAMSET_DESCRIPTION: %s for %s", paramset_key, channel_address)
Expand All @@ -658,9 +658,6 @@ async def fetch_paramset_description(
paramset_description=paramset_description,
)

if save_to_file:
await self.central.paramset_descriptions.save()

async def fetch_paramset_descriptions(self, device_description: dict[str, Any]) -> None:
"""Fetch paramsets for provided device description."""
data = await self.get_paramset_descriptions(device_description=device_description)
Expand All @@ -676,11 +673,11 @@ async def fetch_paramset_descriptions(self, device_description: dict[str, Any])

async def get_paramset_descriptions(
self, device_description: dict[str, Any]
) -> dict[str, dict[str, dict[str, ParameterData]]]:
) -> dict[str, dict[ParamsetKey, dict[str, ParameterData]]]:
"""Get paramsets for provided device description."""
if not device_description:
return {}
paramsets: dict[str, dict[str, dict[str, ParameterData]]] = {}
paramsets: dict[str, dict[ParamsetKey, dict[str, ParameterData]]] = {}
address = device_description[Description.ADDRESS]
paramsets[address] = {}
_LOGGER.debug("GET_PARAMSET_DESCRIPTIONS for %s", address)
Expand Down Expand Up @@ -716,9 +713,9 @@ async def _get_paramset_description(

async def get_all_paramset_descriptions(
self, device_descriptions: tuple[dict[str, Any], ...]
) -> dict[str, dict[str, Any]]:
) -> dict[str, dict[ParamsetKey, dict[str, ParameterData]]]:
"""Get all paramset descriptions for provided device descriptions."""
all_paramsets: dict[str, dict[str, Any]] = {}
all_paramsets: dict[str, dict[ParamsetKey, dict[str, ParameterData]]] = {}
for device_description in device_descriptions:
all_paramsets.update(
await self.get_paramset_descriptions(device_description=device_description)
Expand Down Expand Up @@ -785,7 +782,7 @@ async def update_paramset_descriptions(self, device_address: str) -> None:
interface_id=self.interface_id, device_address=device_address
)
)
await self.central.paramset_descriptions.save()
await self.central.save_caches(save_paramset_descriptions=True)

def __str__(self) -> str:
"""Provide some useful information."""
Expand Down
3 changes: 1 addition & 2 deletions hahomematic/platforms/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,8 @@ async def reload_paramset_descriptions(self) -> None:
await self.client.fetch_paramset_description(
channel_address=channel_address,
paramset_key=paramset_key,
save_to_file=False,
)
await self.central.paramset_descriptions.save()
await self.central.save_caches(save_paramset_descriptions=True)
for entity in self.generic_entities:
entity.update_parameter_data()
self.fire_device_updated_callback()
Expand Down
18 changes: 10 additions & 8 deletions hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,16 @@ def paramset_description_export_converter(
for interface, interface_data in data.items():
if interface not in target:
target[interface] = {}
for address, items in interface_data.items():
for address, channel_data in interface_data.items():
if address not in target[interface]:
target[interface][address] = {}
for p_key, paramsets in items.items():
for p_key, paramsets in channel_data.items():
if (paramset_key := str(p_key)) not in target[interface][address]:
target[interface][address][paramset_key] = {}
for parameter, paramset in paramsets.items():
target[interface][address][paramset_key][parameter] = paramset.as_dict()
for parameter, parameter_data in paramsets.items():
target[interface][address][paramset_key][parameter] = (
parameter_data.as_dict()
)
except Exception as ex:
_LOGGER.error(
"PARAMSET_DESCRIPTION_EXPORT_CONVERTER failed: %s", reduce_args(args=ex.args)
Expand All @@ -427,15 +429,15 @@ def paramset_description_import_converter(
for interface, interface_data in data.items():
if interface not in target:
target[interface] = {}
for address, items in interface_data.items():
for address, channel_data in interface_data.items():
if address not in target[interface]:
target[interface][address] = {}
for p_key, paramsets in items.items():
for p_key, paramsets in channel_data.items():
if (paramset_key := ParamsetKey(p_key)) not in target[interface][address]:
target[interface][address][paramset_key] = {}
for parameter, paramset in paramsets.items():
for parameter, parameter_data in paramsets.items():
target[interface][address][paramset_key][parameter] = ParameterData(
paramset
parameter_data
)

except Exception as ex:
Expand Down

0 comments on commit bd05772

Please sign in to comment.