From 76fcff36142b8a294945d58529d1500863da1c1a Mon Sep 17 00:00:00 2001 From: SukramJ Date: Tue, 19 Nov 2024 20:15:03 +0100 Subject: [PATCH] Improve store tmp value (#1862) * Rename tmp/temp to temporary * Improve store tmp value --- changelog.md | 3 +- hahomematic/client/__init__.py | 8 ++--- hahomematic/model/data_point.py | 57 ++++++++++++++++++--------------- requirements.txt | 2 +- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/changelog.md b/changelog.md index fad470d6..411670a3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,10 +1,11 @@ # Version 2024.11.5 (2024-11-19) - Fix returned version of client +- Improve store tmp value # Version 2024.11.4 (2024-11-19) -- - Add sysvar/program refresh to scheduler +- Add sysvar/program refresh to scheduler - Run periodic tasks with an individual interval - Store tmp value for polling client data points diff --git a/hahomematic/client/__init__.py b/hahomematic/client/__init__.py index 4a638b7b..04ed31b8 100644 --- a/hahomematic/client/__init__.py +++ b/hahomematic/client/__init__.py @@ -597,7 +597,7 @@ async def _set_value( data_point_key_values = self._last_value_send_cache.add_set_value( channel_address=channel_address, parameter=parameter, value=checked_value ) - self._write_tmp_value(data_point_key_values=data_point_key_values) + self._write_temporary_value(data_point_key_values=data_point_key_values) if wait_for_callback is not None and ( device := self.central.get_device( @@ -640,7 +640,7 @@ def _check_set_value( operation=Operations.WRITE, ) - def _write_tmp_value(self, data_point_key_values: set[DP_KEY_VALUE]) -> None: + def _write_temporary_value(self, data_point_key_values: set[DP_KEY_VALUE]) -> None: """Write data point temp value.""" if self.supports_push_updates: return @@ -651,7 +651,7 @@ def _write_tmp_value(self, data_point_key_values: set[DP_KEY_VALUE]) -> None: if data_point := self.central.get_generic_data_point( channel_address=channel_address, parameter=parameter, paramset_key=paramset_key ): - data_point.write_temp_value(value=value) + data_point.write_temporary_value(value=value) async def set_value( self, @@ -778,7 +778,7 @@ async def put_paramset( paramset_key=ParamsetKey(paramset_key), values=checked_values, ) - self._write_tmp_value(data_point_key_values=data_point_key_values) + self._write_temporary_value(data_point_key_values=data_point_key_values) if wait_for_callback is not None and ( device := self.central.get_device( diff --git a/hahomematic/model/data_point.py b/hahomematic/model/data_point.py index ed8f7d44..86ecb159 100644 --- a/hahomematic/model/data_point.py +++ b/hahomematic/model/data_point.py @@ -125,8 +125,8 @@ def __init__(self, central: hmcu.CentralUnit, unique_id: str) -> None: self._path_data = self._get_path_data() self._modified_at: datetime = INIT_DATETIME self._refreshed_at: datetime = INIT_DATETIME - self._tmp_modified_at: datetime = INIT_DATETIME - self._tmp_refreshed_at: datetime = INIT_DATETIME + self._temporary_modified_at: datetime = INIT_DATETIME + self._temporary_refreshed_at: datetime = INIT_DATETIME self._service_methods: dict[str, Callable] = {} @state_property @@ -162,15 +162,15 @@ def full_name(self) -> str: @state_property def modified_at(self) -> datetime: """Return the last update datetime value.""" - if self._tmp_modified_at > self._modified_at: - return self._tmp_modified_at + if self._temporary_modified_at > self._modified_at: + return self._temporary_modified_at return self._modified_at @state_property def refreshed_at(self) -> datetime: """Return the last refresh datetime value.""" - if self._tmp_refreshed_at > self._refreshed_at: - return self._tmp_refreshed_at + if self._temporary_refreshed_at > self._refreshed_at: + return self._temporary_refreshed_at return self._refreshed_at @config_property @@ -297,14 +297,14 @@ def _set_refreshed_at(self, now: datetime = datetime.now()) -> None: """Set refreshed_at to current datetime.""" self._refreshed_at = now - def _set_tmp_modified_at(self, now: datetime = datetime.now()) -> None: - """Set tmp_modified_at to current datetime.""" - self._tmp_modified_at = now - self._set_tmp_refreshed_at(now=now) + def _set_temporary_modified_at(self, now: datetime = datetime.now()) -> None: + """Set temporary_modified_at to current datetime.""" + self._temporary_modified_at = now + self._set_temporary_refreshed_at(now=now) - def _set_tmp_refreshed_at(self, now: datetime = datetime.now()) -> None: - """Set tmp_refreshed_at to current datetime.""" - self._tmp_refreshed_at = now + def _set_temporary_refreshed_at(self, now: datetime = datetime.now()) -> None: + """Set temporary_refreshed_at to current datetime.""" + self._temporary_refreshed_at = now def __str__(self) -> str: """Provide some useful information.""" @@ -445,9 +445,9 @@ def __init__( custom_only=True, ) ) - self._value: ParameterT = None # type: ignore[assignment] + self.__value: ParameterT = None # type: ignore[assignment] self._old_value: ParameterT = None # type: ignore[assignment] - self._tmp_value: ParameterT = None # type: ignore[assignment] + self._temporary_value: ParameterT = None # type: ignore[assignment] self._state_uncertain: bool = True self._is_forced_sensor: bool = False @@ -587,11 +587,16 @@ def state_uncertain(self) -> bool: """Return, if the state is uncertain.""" return self._state_uncertain + @property + def _value(self) -> ParameterT: + """Return the value of the data_point.""" + if self._temporary_refreshed_at > self._refreshed_at: + return self._temporary_value + return self.__value + @state_property def value(self) -> ParameterT: """Return the value of the data_point.""" - if self._tmp_refreshed_at > self._refreshed_at: - return self._tmp_value return self._value @property @@ -717,9 +722,9 @@ async def load_data_point_value( def write_value(self, value: Any) -> tuple[ParameterT, ParameterT]: """Update value of the data_point.""" - self._reset_tmp_value() + self._reset_temporary_value() - old_value = self._value + old_value = self.__value if value == NO_CACHE_ENTRY: if self.refreshed_at != INIT_DATETIME: self._state_uncertain = True @@ -732,19 +737,19 @@ def write_value(self, value: Any) -> tuple[ParameterT, ParameterT]: else: self._set_modified_at() self._old_value = old_value - self._value = new_value + self.__value = new_value self._state_uncertain = False self.fire_data_point_updated_callback() return (old_value, new_value) - def write_temp_value(self, value: Any) -> None: + def write_temporary_value(self, value: Any) -> None: """Update the temporary value of the data_point.""" temp_value = self._convert_value(value) if self._value == temp_value: - self._set_refreshed_at() + self._set_temporary_refreshed_at() else: - self._set_tmp_modified_at() - self._tmp_value = temp_value + self._set_temporary_modified_at() + self._temporary_value = temp_value self._state_uncertain = True self.fire_data_point_updated_callback() @@ -787,9 +792,9 @@ def _convert_value(self, value: Any) -> ParameterT: ) return None # type: ignore[return-value] - def _reset_tmp_value(self) -> None: + def _reset_temporary_value(self) -> None: """Reset the temp storage.""" - self._tmp_value = None # type: ignore[assignment] + self._temporary_value = None # type: ignore[assignment] self._set_modified_at(now=INIT_DATETIME) def get_event_data(self, value: Any = None) -> dict[EventKey, Any]: diff --git a/requirements.txt b/requirements.txt index 3625ed48..5b60eab1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -aiohttp>=3.11.2 +aiohttp>=3.11.5 orjson>=3.10.11 python-slugify>=8.0.4 voluptuous>=0.15.2