diff --git a/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py b/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py index 3cac78b588..20aad694fb 100644 --- a/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py +++ b/custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py @@ -53,22 +53,25 @@ async def extract_entities_from_automation_config( return entities # Extract entities from trigger config - if "trigger" in config: - entities.update( - await extract_entities_from_trigger_config(hass, config["trigger"]) - ) + for key in ("trigger", "triggers"): + if key in config: + entities.update( + await extract_entities_from_trigger_config(hass, config[key]) + ) # Extract entities from condition config - if "condition" in config: - entities.update( - await extract_entities_from_condition_config(hass, config["condition"]) - ) + for key in ("condition", "conditions"): + if key in config: + entities.update( + await extract_entities_from_condition_config(hass, config[key]) + ) # Extract entities from action config - if "action" in config: - entities.update( - await extract_entities_from_action_config(hass, config["action"]) - ) + for key in ("action", "actions"): + if key in config: + entities.update( + await extract_entities_from_action_config(hass, config[key]) + ) return entities @@ -343,11 +346,10 @@ async def _async_compute_unknown_references(self, entity: Any) -> set[str]: self.hass, entity.raw_config ) ) - all_entities.difference_update( - extract_event_types_from_trigger_config( - entity.raw_config.get("trigger") + for key in ("trigger", "triggers"): + all_entities.difference_update( + extract_event_types_from_trigger_config(entity.raw_config.get(key)) ) - ) # Extract entities from Template objects within the automation entity all_entities.update( diff --git a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py index aeeaf116df..5cef68d866 100644 --- a/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py +++ b/tests/ectoplasms/automation/repairs/test_unknown_entity_references.py @@ -427,6 +427,54 @@ async def test_automation_full_config(hass: HomeAssistant) -> None: } +async def test_automation_full_config_with_plural_keys(hass: HomeAssistant) -> None: + """A modern automation config yields entities from plural sections.""" + config = { + "alias": "Test", + "triggers": [{"trigger": "state", "entity_id": "binary_sensor.motion"}], + "conditions": [ + { + "condition": "state", + "entity_id": "input_boolean.snooze_uptime_alerts", + "state": "off", + } + ], + "actions": [ + {"action": "light.turn_on", "target": {"entity_id": "light.kitchen"}}, + ], + } + + assert await extract_entities_from_automation_config(hass, config) == { + "binary_sensor.motion", + "input_boolean.snooze_uptime_alerts", + "light.kitchen", + } + + +async def test_plural_condition_entity_is_reported_unknown( + hass: HomeAssistant, +) -> None: + """A missing entity in a plural ``conditions`` section is reported.""" + entity = MockAutomationEntity( + raw_config={ + "conditions": [ + { + "condition": "state", + "entity_id": "input_boolean.snooze_uptime_alerts", + "state": "off", + } + ], + }, + referenced_entities=set(), + ) + repair = SpookRepair(hass) + repair._known_entity_ids = set() + + assert await repair._async_compute_unknown_references(entity) == { + "input_boolean.snooze_uptime_alerts" + } + + async def test_automation_non_dict_returns_empty(hass: HomeAssistant) -> None: """A non-dict argument short-circuits to an empty set.""" assert await extract_entities_from_automation_config(hass, []) == set()