Repository navigation
Add unknown entity extractor tests #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Spook automation ectoplasm.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Spook automation repairs.""" |
250 changes: 250 additions & 0 deletions
250
tests/ectoplasms/automation/repairs/test_unknown_entity_references.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| """Golden-path tests for the automation entity-reference extractor. | ||
|
|
||
| These tests pin down the observable behavior of the module-level extractors used | ||
| by ``SpookRepair`` in | ||
| ``custom_components/spook/ectoplasms/automation/repairs/unknown_entity_references.py`` | ||
| so the upcoming consolidation into a single recursive walker cannot regress them | ||
| silently. | ||
| """ | ||
|
|
||
| # pylint: disable=wrong-import-order | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from custom_components.spook.ectoplasms.automation.repairs.unknown_entity_references import ( | ||
| extract_entities_from_action_config, | ||
| extract_entities_from_automation_config, | ||
| extract_entities_from_condition_config, | ||
| extract_entities_from_trigger_config, | ||
| extract_entities_from_value, | ||
| ) | ||
| import pytest | ||
|
|
||
| if TYPE_CHECKING: | ||
| from homeassistant.core import HomeAssistant | ||
|
|
||
|
|
||
| async def test_value_plain_entity_id(hass: HomeAssistant) -> None: | ||
| """A bare entity ID string is recognized as an entity reference.""" | ||
| assert await extract_entities_from_value(hass, "light.kitchen") == {"light.kitchen"} | ||
|
|
||
|
|
||
| async def test_value_unknown_domain_is_ignored(hass: HomeAssistant) -> None: | ||
| """Strings using an unknown domain are not treated as entity IDs.""" | ||
| assert await extract_entities_from_value(hass, "totally_made_up.kitchen") == set() | ||
|
|
||
|
|
||
| async def test_value_list_of_entity_ids(hass: HomeAssistant) -> None: | ||
| """A list of entity ID strings yields every valid entry.""" | ||
| result = await extract_entities_from_value( | ||
| hass, ["light.kitchen", "switch.lamp", "not_a_domain.foo"] | ||
| ) | ||
| assert result == {"light.kitchen", "switch.lamp"} | ||
|
|
||
|
|
||
| async def test_value_entity_dict_form(hass: HomeAssistant) -> None: | ||
| """``{"entity": "..."}`` dicts are unwrapped into their entity ID.""" | ||
| assert await extract_entities_from_value( | ||
| hass, {"entity": "sensor.temperature"} | ||
| ) == {"sensor.temperature"} | ||
|
|
||
|
|
||
| async def test_value_template_extracts_referenced_entity( | ||
| hass: HomeAssistant, | ||
| ) -> None: | ||
| """Template strings have their referenced entity IDs extracted.""" | ||
| template = "{{ states('light.kitchen') }}" | ||
| assert await extract_entities_from_value(hass, template) == {"light.kitchen"} | ||
|
|
||
|
|
||
| async def test_value_non_string_non_collection_returns_empty( | ||
| hass: HomeAssistant, | ||
| ) -> None: | ||
| """Numbers, booleans, and None yield no entities.""" | ||
| assert await extract_entities_from_value(hass, 42) == set() | ||
| assert await extract_entities_from_value(hass, None) == set() | ||
| value = True | ||
| assert await extract_entities_from_value(hass, value) == set() | ||
|
|
||
|
|
||
| async def test_trigger_state_entity_id(hass: HomeAssistant) -> None: | ||
| """A state trigger's ``entity_id`` is captured.""" | ||
| config = {"platform": "state", "entity_id": "binary_sensor.door"} | ||
| assert await extract_entities_from_trigger_config(hass, config) == { | ||
| "binary_sensor.door" | ||
| } | ||
|
|
||
|
|
||
| async def test_trigger_zone_field(hass: HomeAssistant) -> None: | ||
| """A zone trigger's ``zone`` field is captured.""" | ||
| config = { | ||
| "platform": "zone", | ||
| "entity_id": "person.alice", | ||
| "zone": "zone.home", | ||
| "event": "enter", | ||
| } | ||
| assert await extract_entities_from_trigger_config(hass, config) == { | ||
| "person.alice", | ||
| "zone.home", | ||
| } | ||
|
|
||
|
|
||
| async def test_trigger_list_of_triggers(hass: HomeAssistant) -> None: | ||
| """Lists of triggers are walked recursively.""" | ||
| config = [ | ||
| {"platform": "state", "entity_id": "light.kitchen"}, | ||
| {"platform": "state", "entity_id": ["switch.lamp", "switch.fan"]}, | ||
| ] | ||
| assert await extract_entities_from_trigger_config(hass, config) == { | ||
| "light.kitchen", | ||
| "switch.lamp", | ||
| "switch.fan", | ||
| } | ||
|
|
||
|
|
||
| async def test_trigger_empty_or_none_returns_empty(hass: HomeAssistant) -> None: | ||
| """Empty inputs produce no entities.""" | ||
| assert await extract_entities_from_trigger_config(hass, None) == set() | ||
| assert await extract_entities_from_trigger_config(hass, {}) == set() | ||
| assert await extract_entities_from_trigger_config(hass, []) == set() | ||
|
|
||
|
|
||
| async def test_condition_state_entity_id(hass: HomeAssistant) -> None: | ||
| """A state condition's ``entity_id`` is captured.""" | ||
| config = {"condition": "state", "entity_id": "switch.lamp", "state": "on"} | ||
| assert await extract_entities_from_condition_config(hass, config) == {"switch.lamp"} | ||
|
|
||
|
|
||
| async def test_condition_zone(hass: HomeAssistant) -> None: | ||
| """Zone conditions capture both the tracked entity and the zone.""" | ||
| config = { | ||
| "condition": "zone", | ||
| "entity_id": "person.alice", | ||
| "zone": "zone.home", | ||
| } | ||
| assert await extract_entities_from_condition_config(hass, config) == { | ||
| "person.alice", | ||
| "zone.home", | ||
| } | ||
|
|
||
|
|
||
| async def test_condition_nested_and(hass: HomeAssistant) -> None: | ||
| """An ``and`` condition recurses into its nested condition list.""" | ||
| config = { | ||
| "condition": "and", | ||
| "conditions": [ | ||
| {"condition": "state", "entity_id": "light.kitchen", "state": "on"}, | ||
| {"condition": "numeric_state", "entity_id": "sensor.temperature"}, | ||
| ], | ||
| } | ||
| assert await extract_entities_from_condition_config(hass, config) == { | ||
| "light.kitchen", | ||
| "sensor.temperature", | ||
| } | ||
|
|
||
|
|
||
| async def test_action_direct_entity_id(hass: HomeAssistant) -> None: | ||
| """A direct ``entity_id`` on an action is captured.""" | ||
| config = {"service": "light.turn_on", "entity_id": "light.kitchen"} | ||
| assert await extract_entities_from_action_config(hass, config) == {"light.kitchen"} | ||
|
|
||
|
|
||
| async def test_action_target_block(hass: HomeAssistant) -> None: | ||
| """``target.entity_id`` on an action is captured.""" | ||
| config = { | ||
| "service": "light.turn_on", | ||
| "target": {"entity_id": ["light.kitchen", "light.living_room"]}, | ||
| } | ||
| assert await extract_entities_from_action_config(hass, config) == { | ||
| "light.kitchen", | ||
| "light.living_room", | ||
| } | ||
|
|
||
|
|
||
| async def test_action_data_dict(hass: HomeAssistant) -> None: | ||
| """Entities buried inside ``data`` values are captured.""" | ||
| config = { | ||
| "service": "notify.notify", | ||
| "data": {"message": "hello", "target": "person.alice"}, | ||
| } | ||
| assert await extract_entities_from_action_config(hass, config) == {"person.alice"} | ||
|
|
||
|
|
||
| async def test_action_data_as_template_string(hass: HomeAssistant) -> None: | ||
| """A template string assigned directly to ``data`` is parsed.""" | ||
| config = { | ||
| "service": "notify.notify", | ||
| "data": "{{ states('sensor.temperature') }}", | ||
| } | ||
| assert await extract_entities_from_action_config(hass, config) == { | ||
| "sensor.temperature" | ||
| } | ||
|
|
||
|
|
||
| async def test_action_if_then_else_nested(hass: HomeAssistant) -> None: | ||
| """``if``/``then``/``else`` nested actions are walked.""" | ||
| config = { | ||
| "if": [{"condition": "state", "entity_id": "binary_sensor.door"}], | ||
| "then": [{"service": "light.turn_on", "target": {"entity_id": "light.hall"}}], | ||
| "else": [{"service": "light.turn_off", "target": {"entity_id": "light.hall"}}], | ||
| } | ||
| result = await extract_entities_from_action_config(hass, config) | ||
| assert {"binary_sensor.door", "light.hall"} <= result | ||
|
|
||
|
|
||
| async def test_action_list_of_actions(hass: HomeAssistant) -> None: | ||
| """A top-level list of actions is walked.""" | ||
| config = [ | ||
| {"service": "light.turn_on", "target": {"entity_id": "light.a"}}, | ||
| {"service": "switch.turn_on", "entity_id": "switch.b"}, | ||
| ] | ||
| assert await extract_entities_from_action_config(hass, config) == { | ||
| "light.a", | ||
| "switch.b", | ||
| } | ||
|
|
||
|
|
||
| async def test_automation_full_config(hass: HomeAssistant) -> None: | ||
| """A complete automation config yields entities from all three sections.""" | ||
| config = { | ||
| "alias": "Test", | ||
| "trigger": [{"platform": "state", "entity_id": "binary_sensor.motion"}], | ||
| "condition": [ | ||
| {"condition": "state", "entity_id": "input_boolean.guest", "state": "on"} | ||
| ], | ||
| "action": [ | ||
| {"service": "light.turn_on", "target": {"entity_id": "light.kitchen"}}, | ||
| ], | ||
| } | ||
| assert await extract_entities_from_automation_config(hass, config) == { | ||
| "binary_sensor.motion", | ||
| "input_boolean.guest", | ||
| "light.kitchen", | ||
| } | ||
|
|
||
|
|
||
| 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() | ||
| assert await extract_entities_from_automation_config(hass, "not a dict") == set() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("section", ["trigger", "condition", "action"]) | ||
| async def test_automation_missing_sections(hass: HomeAssistant, section: str) -> None: | ||
| """Automations missing one of the three sections still extract from the rest.""" | ||
| config = { | ||
| "trigger": [{"platform": "state", "entity_id": "binary_sensor.t"}], | ||
| "condition": [ | ||
| {"condition": "state", "entity_id": "binary_sensor.c", "state": "on"} | ||
| ], | ||
| "action": [{"service": "light.turn_on", "entity_id": "light.a"}], | ||
| } | ||
| config.pop(section) | ||
| result = await extract_entities_from_automation_config(hass, config) | ||
| expected = { | ||
| "trigger": {"binary_sensor.c", "light.a"}, | ||
| "condition": {"binary_sensor.t", "light.a"}, | ||
| "action": {"binary_sensor.t", "binary_sensor.c"}, | ||
| }[section] | ||
| assert result == expected | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Spook Lovelace ectoplasm.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Spook Lovelace repairs.""" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.