diff --git a/hahomematic/central/__init__.py b/hahomematic/central/__init__.py index 460fb90a..5eec2835 100644 --- a/hahomematic/central/__init__.py +++ b/hahomematic/central/__init__.py @@ -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.""" @@ -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: @@ -825,13 +829,17 @@ 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]", @@ -839,7 +847,10 @@ async def _add_new_devices( 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() diff --git a/hahomematic/client/__init__.py b/hahomematic/client/__init__.py index d9ffe20a..bd8b196a 100644 --- a/hahomematic/client/__init__.py +++ b/hahomematic/client/__init__.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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.""" diff --git a/hahomematic/platforms/device.py b/hahomematic/platforms/device.py index bdb8ca49..b8768006 100644 --- a/hahomematic/platforms/device.py +++ b/hahomematic/platforms/device.py @@ -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() diff --git a/hahomematic/support.py b/hahomematic/support.py index 6b9e94bd..58a64462 100644 --- a/hahomematic/support.py +++ b/hahomematic/support.py @@ -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) @@ -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: