Skip to content

Commit

Permalink
Improve store tmp value (#1862)
Browse files Browse the repository at this point in the history
* Rename tmp/temp to temporary

* Improve store tmp value
  • Loading branch information
SukramJ authored Nov 19, 2024
1 parent 120b886 commit 76fcff3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions hahomematic/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
57 changes: 31 additions & 26 deletions hahomematic/model/data_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 76fcff3

Please sign in to comment.