Skip to content

Commit a12b032

Browse files
Add comparison methods
1 parent 43a321c commit a12b032

File tree

5 files changed

+59
-82
lines changed

5 files changed

+59
-82
lines changed

custom_components/calendar_event/binary_sensor.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
from homeassistant.core import Event, HomeAssistant, callback
1111
from 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

1621
async 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

custom_components/calendar_event/config_flow.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@
1414

1515
from .const import (
1616
CONF_CALENDAR_ENTITY_ID,
17+
CONF_COMPARISON_METHOD,
1718
CONF_SUMMARY,
1819
DOMAIN,
1920
)
2021

22+
_MATCHING_CRITERIA = ["contains", "starts_with", "ends_with", "exactly"]
23+
2124
OPTIONS_SCHEMA = vol.Schema(
2225
{
2326
vol.Required(CONF_CALENDAR_ENTITY_ID): selector.EntitySelector(
2427
selector.EntitySelectorConfig(domain="calendar")
2528
),
2629
vol.Required(CONF_SUMMARY): selector.TextSelector(),
30+
vol.Required(
31+
CONF_COMPARISON_METHOD, default="contains"
32+
): selector.SelectSelector(
33+
selector.SelectSelectorConfig(
34+
options=_MATCHING_CRITERIA, translation_key=CONF_COMPARISON_METHOD
35+
),
36+
),
2737
}
2838
)
2939

custom_components/calendar_event/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424

2525
CONF_CALENDAR_ENTITY_ID = "calendar_entity_id"
2626
CONF_SUMMARY = "summary"
27+
CONF_COMPARISON_METHOD = "comparison_method"
2728

2829
ATTR_DESCRIPTION = "description"

custom_components/calendar_event/translations/en.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
"data": {
88
"name": "Name",
99
"calendar_entity_id": "Calendar",
10-
"summary": "Summary"
10+
"summary": "Summary",
11+
"comparison_method": "Comparison Method"
1112
},
1213
"data_description": {
1314
"calendar_entity_id": "The calendar entity to monitor for events.",
14-
"summary": "The calendar event summary text to match."
15+
"summary": "The calendar event summary text to match, matching will be case-insensitive.",
16+
"comparison_method": "How to match the summary text."
1517
}
1618
}
1719
}
@@ -23,13 +25,25 @@
2325
"description": "Configure the calendar event options.",
2426
"data": {
2527
"calendar_entity_id": "Calendar",
26-
"summary": "Summary"
28+
"summary": "Summary",
29+
"comparison_method": "Comparison Method"
2730
},
2831
"data_description": {
2932
"calendar_entity_id": "The calendar entity to monitor for events.",
30-
"summary": "The calendar event summary text to match."
33+
"summary": "The calendar event summary text to match, matching will be case-insensitive.",
34+
"comparison_method": "How to match the summary text."
3135
}
3236
}
3337
}
38+
},
39+
"selector": {
40+
"comparison_method": {
41+
"options": {
42+
"contains": "Contains",
43+
"starts_with": "Starts with",
44+
"ends_with": "Ends with",
45+
"exactly": "Exactly"
46+
}
47+
}
3448
}
3549
}

tests/test_repairs.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)