1010from homeassistant .core import Event , HomeAssistant , callback
1111from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
1212
13- from .const import ATTR_DESCRIPTION , CONF_CALENDAR_ENTITY_ID , CONF_SUMMARY
13+ from .const import (
14+ ATTR_DESCRIPTION ,
15+ CONF_CALENDAR_ENTITY_ID ,
16+ CONF_COMPARISON_METHOD ,
17+ CONF_SUMMARY ,
18+ )
1419
1520
1621async def config_entry_update_listener (hass : HomeAssistant , entry : ConfigEntry ) -> None :
@@ -28,6 +33,9 @@ async def async_setup_entry(
2833 name : str | None = config_entry .options .get ("name" )
2934 calendar_entity : str = config_entry .options [CONF_CALENDAR_ENTITY_ID ]
3035 summary : str = config_entry .options [CONF_SUMMARY ]
36+ comparison_method : str = config_entry .options .get (
37+ CONF_COMPARISON_METHOD , "contains"
38+ )
3139 unique_id = config_entry .entry_id
3240
3341 config_entry .async_on_unload (
@@ -43,6 +51,7 @@ async def async_setup_entry(
4351 unique_id ,
4452 calendar_entity ,
4553 summary ,
54+ comparison_method ,
4655 )
4756 ]
4857 )
@@ -65,12 +74,14 @@ def __init__(
6574 unique_id : str | None ,
6675 calendar_entity_id : str ,
6776 summary : str ,
77+ comparison_method : str ,
6878 ) -> None :
6979 """Initialize the Calendar Event sensor."""
7080 self ._attr_unique_id = unique_id
7181 self ._attr_name = name
7282 self ._calendar_entity_id = calendar_entity_id
7383 self ._summary = summary
84+ self ._comparison_method = comparison_method
7485 self ._hass = hass
7586 self ._config_entry = config_entry
7687
@@ -135,6 +146,23 @@ async def _update_state(self) -> None:
135146 lambda : self ._hass .async_create_task (self ._update_state ()),
136147 )
137148
149+ def _matches_criteria (self , event_summary : str ) -> bool :
150+ """Check if event summary matches the configured criteria."""
151+ event_summary_lower = event_summary .lower ()
152+ summary_lower = self ._summary .lower ()
153+
154+ if self ._comparison_method == "contains" :
155+ return summary_lower in event_summary_lower
156+ elif self ._comparison_method == "starts_with" :
157+ return event_summary_lower .startswith (summary_lower )
158+ elif self ._comparison_method == "ends_with" :
159+ return event_summary_lower .endswith (summary_lower )
160+ elif self ._comparison_method == "exactly" :
161+ return event_summary_lower == summary_lower
162+ else :
163+ # Default to contains if unknown criteria
164+ return summary_lower in event_summary_lower
165+
138166 async def _get_event_matching_summary (self ) -> Event | None :
139167 """Check if the summary is in the calendar events."""
140168
@@ -157,7 +185,7 @@ async def _get_event_matching_summary(self) -> Event | None:
157185 for event in calendar_events :
158186 if not isinstance (event , dict ):
159187 continue
160- if self ._summary . lower () in event .get ("summary" , "" ). lower ( ):
188+ if self ._matches_criteria ( event .get ("summary" , "" )):
161189 return event
162190
163191 return None
0 commit comments