From 9eb7d8912c65430be6c00fe809765ea743b5d6f0 Mon Sep 17 00:00:00 2001 From: SMKRV Date: Fri, 6 Dec 2024 12:26:56 +0300 Subject: [PATCH] fix: sensor history attribute calculation --- custom_components/ha_text_ai/coordinator.py | 33 +++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/custom_components/ha_text_ai/coordinator.py b/custom_components/ha_text_ai/coordinator.py index 2fd81ce..498ec74 100644 --- a/custom_components/ha_text_ai/coordinator.py +++ b/custom_components/ha_text_ai/coordinator.py @@ -173,7 +173,6 @@ class HATextAICoordinator(DataUpdateCoordinator): self.available = True self._state = STATE_READY - self._conversation_history = [] # Full conversation history self._start_time = dt_util.utcnow() @@ -404,17 +403,32 @@ class HATextAICoordinator(DataUpdateCoordinator): async def async_initialize_history_file(self) -> None: """ - Asynchronously initialize history file. + Asynchronously initialize history file and load existing history. """ try: - # Ensure directory exists first await self._create_history_dir() - # Initialize file - if not await self._file_exists(self._history_file): + if await self._file_exists(self._history_file): + # Load existing history + async with AsyncFileHandler(self._history_file, 'r') as f: + content = await f.read() + if content: + history = json.loads(content) + # Validate and load history + if isinstance(history, list): + self._conversation_history = history[-self.max_history_size:] + _LOGGER.debug( + f"Loaded {len(self._conversation_history)} history entries " + f"for {self.instance_name}" + ) + else: + # Create new history file async with AsyncFileHandler(self._history_file, 'w') as f: await f.write(json.dumps([])) + await self._check_history_size() + await self.async_update_ha_state() + _LOGGER.debug(f"History file initialized: {self._history_file}") except Exception as e: @@ -504,6 +518,15 @@ class HATextAICoordinator(DataUpdateCoordinator): _LOGGER.error(f"Error writing history entry: {e}") _LOGGER.debug(traceback.format_exc()) + async def _check_history_size(self) -> None: + """Verify and adjust history size if needed.""" + if len(self._conversation_history) > self.max_history_size: + _LOGGER.warning( + f"History size ({len(self._conversation_history)}) exceeds " + f"maximum ({self.max_history_size}). Trimming..." + ) + self._conversation_history = self._conversation_history[-self.max_history_size:] + async def _check_file_size(self, file_path: str) -> int: """ Check file size asynchronously.