Skip to content

Commit

Permalink
Fixed auto delete for retention_time==0
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinfrlch committed Oct 29, 2024
1 parent 50f0d78 commit ce48c2a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion custom_components/llmvision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def async_setup_entry(hass, entry):
hass.data[DOMAIN][entry_uid] = filtered_entry_data

# check if the entry is the calendar entry (has entry rentention_time)
if filtered_entry_data.get(CONF_RETENTION_TIME):
if filtered_entry_data.get(CONF_RETENTION_TIME) is not None:
# forward the calendar entity to the platform
await hass.config_entries.async_forward_entry_setups(entry, ["calendar"])

Expand All @@ -109,6 +109,7 @@ async def async_remove_entry(hass, entry):
if entry_uid in hass.data[DOMAIN]:
# Remove the entry from hass.data
_LOGGER.info(f"Removing {entry.title} from hass.data")
async_unload_entry(hass, entry)
hass.data[DOMAIN].pop(entry_uid)
else:
_LOGGER.warning(
Expand Down
8 changes: 5 additions & 3 deletions custom_components/llmvision/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid
import os
import json
from .const import CONF_RETENTION_TIME
from .const import DOMAIN, CONF_RETENTION_TIME
from homeassistant.util import dt as dt_util
from homeassistant.core import HomeAssistant
from homeassistant.components.calendar import (
Expand Down Expand Up @@ -31,7 +31,8 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
self._attr_name = config_entry.title
self._attr_unique_id = config_entry.entry_id
self._events = []
self._retention_time = config_entry.options.get(CONF_RETENTION_TIME, 7)
self._retention_time = self.hass.data.get(DOMAIN).get(
self._attr_unique_id).get(CONF_RETENTION_TIME)
self._current_event = None
self._attr_supported_features = (CalendarEntityFeature.DELETE_EVENT)
# Path to the JSON file where events are stored
Expand Down Expand Up @@ -150,6 +151,7 @@ async def _save_events(self) -> None:
# Delete events outside of retention time window
now = datetime.datetime.now()
cutoff_date = now - datetime.timedelta(days=self._retention_time)
_LOGGER.info(f"Deleting events before {cutoff_date}")

events_data = [
{
Expand All @@ -161,7 +163,7 @@ async def _save_events(self) -> None:
"location": event.location,
}
for event in self._events
if dt_util.as_local(self._ensure_datetime(event.end)) >= self._ensure_datetime(cutoff_date)
if dt_util.as_local(self._ensure_datetime(event.end)) >= self._ensure_datetime(cutoff_date) or self._retention_time == 0
]

def write_to_file():
Expand Down
3 changes: 2 additions & 1 deletion custom_components/llmvision/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ async def groq(self):
_LOGGER.error("Could not connect to Groq server.")
raise ServiceValidationError("handshake_failed")

async def semantic_index(self):
async def semantic_index(self) -> bool:
# check if semantic_index is already configured
for uid in self.hass.data[DOMAIN]:
if 'retention_time' in self.hass.data[DOMAIN][uid]:
return False
return True

def get_configured_providers(self):
providers = []
Expand Down

0 comments on commit ce48c2a

Please sign in to comment.