Skip to content

Commit ef0cd69

Browse files
author
valentinfrlch
committed
Fix blocking call in calendar.py
1 parent 45d774a commit ef0cd69

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

custom_components/llmvision/calendar.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
_LOGGER = logging.getLogger(__name__)
2626

2727

28-
def _get_category(config_entry: ConfigEntry, query: str) -> list[str]:
28+
async def _get_category(hass: HomeAssistant, config_entry: ConfigEntry, query: str) -> list[str]:
2929
"""Get categories matching the query using the language regex template."""
30+
def load_lang_json(path: str) -> dict:
31+
with open(path, "r", encoding="utf-8") as f:
32+
return json.load(f)
33+
3034
# Determine language (fallback to 'en')
3135
language = config_entry.options.get(CONF_TIMELINE_LANGUAGE) or "English"
3236

@@ -59,8 +63,7 @@ def _get_category(config_entry: ConfigEntry, query: str) -> list[str]:
5963
)
6064

6165
try:
62-
with open(lang_file_path, "r", encoding="utf-8") as lang_file:
63-
lang_data = json.load(lang_file)
66+
lang_data = await hass.async_add_executor_job(load_lang_json, lang_file_path)
6467
except Exception as e:
6568
_LOGGER.error(f"Error loading language file {lang_file_path}: {e}")
6669
return []
@@ -125,7 +128,6 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
125128
# Track key_frame paths whose DB rows are not yet committed
126129
self._pending_key_frames: set[str] = set()
127130
self._cleanup_lock = asyncio.Lock()
128-
self._category: str = ""
129131
self._config_entry = config_entry
130132

131133
# Path to the JSON file where events are stored
@@ -290,7 +292,7 @@ async def _migrate(self):
290292
for row in rows:
291293
uid = row[0]
292294
summary = row[1]
293-
matches = _get_category(self._config_entry, summary)
295+
matches = await _get_category(self.hass, self._config_entry, summary)
294296
category = matches[0] if matches else ""
295297
await db.execute(
296298
"""UPDATE events SET category = ? WHERE uid = ?""",
@@ -686,7 +688,7 @@ async def get_summaries(self, start: datetime, end: datetime):
686688
return events_summaries
687689

688690
async def remember(
689-
self, start, end, label, key_frame, summary, camera_name="", today_summary=""
691+
self, start, end, label, key_frame, summary, category="", camera_name="", today_summary=""
690692
):
691693
"""Remembers the event"""
692694
_LOGGER.info(
@@ -695,12 +697,12 @@ async def remember(
695697

696698
# Resolve category
697699
try:
698-
matches = _get_category(self._config_entry, label)
699-
self._category = matches[0] if matches else ""
700-
_LOGGER.debug(f"Resolved category: {self._category} (matches={matches})")
700+
matches = await _get_category(self.hass, self._config_entry, label)
701+
category = matches[0] if matches else ""
702+
_LOGGER.debug(f"Resolved category: {category} (matches={matches})")
701703
except Exception as e:
702704
_LOGGER.warning(f"Failed to resolve category: {e}")
703-
self._category = ""
705+
category = ""
704706

705707
# Mark key_frame as pending so cleanup won't remove it before DB insert
706708
if key_frame:
@@ -711,7 +713,7 @@ async def remember(
711713
dtend=end,
712714
summary=label,
713715
description=summary,
714-
category=self._category,
716+
category=category,
715717
key_frame=key_frame,
716718
camera_name=camera_name,
717719
today_summary=today_summary,

0 commit comments

Comments
 (0)