Skip to content

Commit 4428a26

Browse files
committed
Fix service actions being identified as entities
1 parent 3438a07 commit 4428a26

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
from ....const import LOGGER
1313
from ....repairs import AbstractSpookRepair
1414
from ....util import (
15-
async_extract_entities_from_config, # Added
15+
async_extract_entities_from_config,
1616
async_extract_entities_from_template_string,
1717
async_filter_known_entity_ids_with_templates,
1818
async_get_all_entity_ids,
19-
is_template_string, # Keep for extract_entities_from_value
19+
is_template_string,
2020
)
2121

2222
if TYPE_CHECKING:
2323
from homeassistant.core import HomeAssistant
2424

2525

26-
async def extract_template_entities_from_automation_entity(entity: Any) -> set[str]:
26+
async def extract_template_entities_from_automation_entity(
27+
hass: HomeAssistant, entity: Any
28+
) -> set[str]:
2729
"""Extract entities from automation configuration using Template analysis.
2830
2931
This function finds template strings in automation configuration and creates
@@ -37,8 +39,7 @@ async def extract_template_entities_from_automation_entity(entity: Any) -> set[s
3739
else:
3840
return set()
3941

40-
# Use the new utility function
41-
return await async_extract_entities_from_config(config)
42+
return await async_extract_entities_from_config(hass, config)
4243

4344

4445
async def extract_entities_from_automation_config(
@@ -229,7 +230,7 @@ async def extract_entities_from_value(hass: HomeAssistant, value: Any) -> set[st
229230
# Process as template to extract entity references
230231
try:
231232
template_entities = await async_extract_entities_from_template_string(
232-
value
233+
hass, value
233234
)
234235
entities.update(template_entities)
235236
# pylint: disable-next=broad-exception-caught
@@ -302,7 +303,7 @@ async def async_inspect(self) -> None:
302303

303304
# Extract entities from Template objects within the automation entity
304305
template_entities = await extract_template_entities_from_automation_entity(
305-
entity
306+
self.hass, entity
306307
)
307308
all_entities.update(template_entities)
308309

custom_components/spook/ectoplasms/script/repairs/unknown_entity_references.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import Any
5+
from typing import TYPE_CHECKING, Any
66

77
from homeassistant.components import script
88
from homeassistant.const import EVENT_COMPONENT_LOADED
@@ -11,12 +11,14 @@
1111

1212
from ....repairs import AbstractSpookRepair
1313
from ....util import (
14-
async_extract_entities_from_config, # Added
14+
async_extract_entities_from_config,
1515
async_filter_known_entity_ids_with_templates,
1616
async_get_all_entity_ids,
17-
# is_template_string, # No longer needed directly here
1817
)
1918

19+
if TYPE_CHECKING:
20+
from homeassistant.core import HomeAssistant
21+
2022

2123
def extract_entities_from_trigger_config(config: dict[str, Any] | list) -> set[str]:
2224
"""Extract entity IDs from a trigger config."""
@@ -49,7 +51,9 @@ def extract_entities_from_trigger_config(config: dict[str, Any] | list) -> set[s
4951
return entities
5052

5153

52-
async def extract_template_entities_from_script_entity(entity: Any) -> set[str]:
54+
async def extract_template_entities_from_script_entity(
55+
hass: HomeAssistant, entity: Any
56+
) -> set[str]:
5357
"""Extract entities from script configuration using Template analysis.
5458
5559
This function finds template strings in script configuration and creates
@@ -69,8 +73,7 @@ async def extract_template_entities_from_script_entity(entity: Any) -> set[str]:
6973
if not config:
7074
return set()
7175

72-
# Use the new utility function
73-
return await async_extract_entities_from_config(config)
76+
return await async_extract_entities_from_config(hass, config)
7477

7578

7679
class SpookRepair(AbstractSpookRepair):
@@ -140,7 +143,7 @@ async def async_inspect(self) -> None:
140143

141144
# Extract entities from Template objects within the script entity
142145
template_entities = await extract_template_entities_from_script_entity(
143-
entity
146+
self.hass, entity
144147
)
145148
all_entities.update(template_entities)
146149

custom_components/spook/util.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def is_template_string(value: str) -> bool:
469469

470470

471471
async def async_extract_entities_from_template_string(
472-
template_str: str,
472+
hass: HomeAssistant, template_str: str
473473
) -> set[str]:
474474
"""Extract entity IDs from a template string using regex analysis.
475475
@@ -483,7 +483,7 @@ async def async_extract_entities_from_template_string(
483483

484484
# Use regex patterns to find entities
485485
try:
486-
regex_entities = extract_entities_from_template_regex(template_str)
486+
regex_entities = extract_entities_from_template_regex(hass, template_str)
487487
entities.update(regex_entities)
488488
# pylint: disable-next=broad-exception-caught
489489
except Exception as exc: # noqa: BLE001 - Keep broad for unexpected regex issues
@@ -496,7 +496,9 @@ async def async_extract_entities_from_template_string(
496496
return entities
497497

498498

499-
def extract_entities_from_template_regex(template_str: str) -> set[str]:
499+
def extract_entities_from_template_regex(
500+
hass: HomeAssistant, template_str: str
501+
) -> set[str]:
500502
"""Extract entity IDs from template string using regex patterns.
501503
502504
This function uses regex patterns based on Home Assistant's core validation
@@ -525,19 +527,26 @@ def extract_entities_from_template_regex(template_str: str) -> set[str]:
525527
if valid_entity_id(individual_id):
526528
entities.add(individual_id)
527529

528-
return entities
530+
# Filter out known services to avoid false positives
531+
known_services = async_get_all_services(hass)
532+
return entities - known_services
529533

530534

531535
async def _process_template_object(
532-
template: Template, known_entity_ids: set[str], unknown_entities: set[str]
536+
hass: HomeAssistant,
537+
template: Template,
538+
known_entity_ids: set[str],
539+
unknown_entities: set[str],
533540
) -> None:
534541
"""Process a Template object and add unknown entities to the set."""
535542
template_entities = set()
536543

537544
# Use regex patterns on the template string
538545
try:
539546
if hasattr(template, "template") and template.template:
540-
regex_entities = extract_entities_from_template_regex(template.template)
547+
regex_entities = extract_entities_from_template_regex(
548+
hass, template.template
549+
)
541550
template_entities.update(regex_entities)
542551
# pylint: disable-next=broad-exception-caught
543552
except Exception: # noqa: BLE001
@@ -550,12 +559,15 @@ async def _process_template_object(
550559

551560

552561
async def _process_template_string(
562+
hass: HomeAssistant,
553563
template_str: str,
554564
known_entity_ids: set[str],
555565
unknown_entities: set[str],
556566
) -> None:
557567
"""Process a template string and add unknown entities to the set."""
558-
template_entities = await async_extract_entities_from_template_string(template_str)
568+
template_entities = await async_extract_entities_from_template_string(
569+
hass, template_str
570+
)
559571
# Check if any of the template entities are unknown
560572
for template_entity in template_entities:
561573
# Handle comma-separated entity lists
@@ -587,7 +599,7 @@ async def async_filter_known_entity_ids_with_templates(
587599
# Handle Template objects
588600
if isinstance(entity_id_raw, Template):
589601
await _process_template_object(
590-
entity_id_raw, known_entity_ids, unknown_entities
602+
hass, entity_id_raw, known_entity_ids, unknown_entities
591603
)
592604
continue
593605

@@ -597,7 +609,7 @@ async def async_filter_known_entity_ids_with_templates(
597609
# Check if this looks like a template string
598610
if is_template_string(entity_id_raw):
599611
await _process_template_string(
600-
entity_id_raw, known_entity_ids, unknown_entities
612+
hass, entity_id_raw, known_entity_ids, unknown_entities
601613
)
602614
else:
603615
# Process as regular entity ID(s), handling comma-separated lists
@@ -655,8 +667,10 @@ def extract_template_strings_from_config(
655667
return strings
656668

657669

658-
async def async_extract_entities_from_config(config: Any) -> set[str]:
659-
"""Extract all entity IDs referenced in templates within a configuration structure."""
670+
async def async_extract_entities_from_config(
671+
hass: HomeAssistant, config: Any
672+
) -> set[str]:
673+
"""Extract entity IDs referenced in templates within a configuration structure."""
660674
entities = set()
661675
if not config:
662676
return entities
@@ -667,7 +681,7 @@ async def async_extract_entities_from_config(config: Any) -> set[str]:
667681
# async_extract_entities_from_template_string already handles
668682
# TemplateError and other exceptions internally, logging them.
669683
referenced_entities = await async_extract_entities_from_template_string(
670-
template_str
684+
hass, template_str
671685
)
672686
entities.update(referenced_entities)
673687
# pylint: disable-next=broad-exception-caught

0 commit comments

Comments
 (0)