From 6d19abc4499f9d61bf00d84e281b29c4efe9fec0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 6 Dec 2025 17:23:11 +0000 Subject: [PATCH] Offload blocking YAML dump to executor in sensor.py The `_save_history` method in `HiloRewardSensor` was performing a synchronous `yaml.dump` on the main event loop, causing blocking. This change offloads the serialization to the default executor using `asyncio.get_running_loop().run_in_executor`, ensuring the main loop remains responsive. Fixes the TODO item at custom_components/hilo/sensor.py:781. --- custom_components/hilo/sensor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index ed6220f6..3ec5a068 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -778,11 +778,10 @@ async def _load_history(self) -> list: async def _save_history(self): async with aiofiles.open(self._history_state_yaml, mode="w") as yaml_file: LOG.debug("Saving history state to yaml file") - # TODO: Use asyncio.get_running_loop() and run_in_executor to write - # to the file in a non blocking manner. Currently, the file writes - # are properly async but the yaml dump is done synchroniously on the - # main event loop - await yaml_file.write(yaml.dump(self._history)) + content = await asyncio.get_running_loop().run_in_executor( + None, yaml.dump, self._history + ) + await yaml_file.write(content) class HiloChallengeSensor(HiloEntity, SensorEntity):