187 files
This commit is contained in:
1 parent
28fa34d78a
commit
1e1b12d52b
187 files changed
+40899
-32654
No files matched your search
@@ -31,12 +31,17 @@ from .const import (
|
||||
STORAGE_KEY,
|
||||
)
|
||||
from .log_utils import DeviceLoggerAdapter
|
||||
from .time_utils import utc_now
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
STORAGE_KEY_RECORDER = f"{STORAGE_KEY}.recorder"
|
||||
|
||||
|
||||
# Seconds between buffer saves while recording (audit PLATFORM-18).
|
||||
_SAVE_INTERVAL_S = 300.0
|
||||
|
||||
|
||||
class RecorderStore(Store[dict[str, Any]]):
|
||||
"""Store for recorder data with migration support."""
|
||||
|
||||
@@ -87,7 +92,7 @@ class CycleRecorder:
|
||||
def current_duration(self) -> float:
|
||||
"""Return current recording duration in seconds."""
|
||||
if self._start_time:
|
||||
return (dt_util.now() - self._start_time).total_seconds()
|
||||
return (utc_now() - self._start_time).total_seconds()
|
||||
return 0.0
|
||||
|
||||
async def async_load(self) -> None:
|
||||
@@ -188,7 +193,7 @@ class CycleRecorder:
|
||||
"last_run": self._last_run,
|
||||
}
|
||||
await self._store.async_save(data)
|
||||
self._last_save = dt_util.now()
|
||||
self._last_save = utc_now()
|
||||
|
||||
async def start_recording(self) -> None:
|
||||
"""Start a new recording."""
|
||||
@@ -200,7 +205,7 @@ class CycleRecorder:
|
||||
# Previous recordings are kept until explicitly cleared or overwritten
|
||||
|
||||
self._is_recording = True
|
||||
self._start_time = dt_util.now()
|
||||
self._start_time = utc_now()
|
||||
self._buffer = []
|
||||
await self._async_save()
|
||||
|
||||
@@ -209,14 +214,15 @@ class CycleRecorder:
|
||||
if not self._is_recording:
|
||||
return
|
||||
|
||||
now = dt_util.now()
|
||||
now = utc_now()
|
||||
# Append to buffer
|
||||
self._buffer.append((now.isoformat(), float(power)))
|
||||
|
||||
# Periodic save every 60s to ensure data persistence
|
||||
# Better safe than sorry: save if last save was > 1 minute ago
|
||||
if self._last_save and (now - self._last_save).total_seconds() > 60:
|
||||
self.hass.add_job(self._async_save)
|
||||
elif not self._last_save:
|
||||
# Periodic save every 5 min (audit PLATFORM-18): each save rewrites the
|
||||
# whole buffer and the previous recording, ~200 MB to the SD card over a
|
||||
# 4 h recording at 60 s. Stamped when SCHEDULED, not when the write
|
||||
# finishes, so the readings that arrive meanwhile cannot each schedule one.
|
||||
if not self._last_save or (now - self._last_save).total_seconds() > _SAVE_INTERVAL_S:
|
||||
self._last_save = now
|
||||
self.hass.add_job(self._async_save)
|
||||
|
||||
Reference in new issue
Block a user