Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed Mar 29, 2024
1 parent e919733 commit 8fbd14e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
8 changes: 6 additions & 2 deletions homeassistant/helpers/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ def async_match_states(
if (floor is None) and (floor_name is not None):
# Look up floor by name
floor = _find_floor(floor_name, floors)
assert floor is not None, f"No floor named {floor_name}"
if floor is None:
_LOGGER.warning("Floor not found: %s", floor_name)
return []

Check failure on line 347 in homeassistant/helpers/intent.py

View workflow job for this annotation

GitHub Actions / Check mypy

No return value expected [return-value]

if floor is not None:
filter_areas = [
Expand All @@ -352,7 +354,9 @@ def async_match_states(
if (area is None) and (area_name is not None):
# Look up area by name
area = _find_area(area_name, areas)
assert area is not None, f"No area named {area_name}"
if area is None:
_LOGGER.warning("Area not found: %s", area_name)
return []

Check failure on line 359 in homeassistant/helpers/intent.py

View workflow job for this annotation

GitHub Actions / Check mypy

No return value expected [return-value]

if area is not None:
filter_areas = [area]
Expand Down
61 changes: 60 additions & 1 deletion tests/helpers/test_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ async def test_async_match_states(
area_bedroom = area_registry.async_get_or_create("bedroom")

# Kitchen is on the first floor
floor_1 = floor_registry.async_create("first floor")
floor_1 = floor_registry.async_create("first floor", aliases={"ground floor"})
area_kitchen = area_registry.async_update(
area_kitchen.id, floor_id=floor_1.floor_id
)

# Bedroom is on the second floor
floor_2 = floor_registry.async_create("second floor")
area_bedroom = area_registry.async_update(
area_bedroom.id, floor_id=floor_2.floor_id
)

state1 = State(
"light.kitchen", "on", attributes={ATTR_FRIENDLY_NAME: "kitchen light"}
)
Expand Down Expand Up @@ -102,6 +108,13 @@ async def test_async_match_states(
)
)

# Invalid area
assert not list(
intent.async_match_states(
hass, area_name="invalid area", states=[state1, state2]
)
)

# Domain + area
assert list(
intent.async_match_states(
Expand All @@ -126,6 +139,28 @@ async def test_async_match_states(
)
) == [state1]

assert list(
intent.async_match_states(
# Check alias
hass,
floor_name="ground floor",
states=[state1, state2],
)
) == [state1]

assert list(
intent.async_match_states(
hass, floor_name="second floor", states=[state1, state2]
)
) == [state2]

# Invalid floor
assert not list(
intent.async_match_states(
hass, floor_name="invalid floor", states=[state1, state2]
)
)


async def test_match_device_area(
hass: HomeAssistant,
Expand Down Expand Up @@ -315,3 +350,27 @@ async def mock_service(call):

assert len(calls) == 1
assert calls[0].data == {"entity_id": "light.kitchen"}


async def test_invalid_area_floor_names(hass: HomeAssistant) -> None:
"""Test that we throw an intent handle error with invalid area/floor names."""
handler = intent.ServiceIntentHandler(
"TestType", "light", "turn_on", "Turned {} on"
)
intent.async_register(hass, handler)

with pytest.raises(intent.IntentHandleError):
await intent.async_handle(
hass,
"test",
"TestType",
slots={"area": {"value": "invalid area"}},
)

with pytest.raises(intent.IntentHandleError):
await intent.async_handle(
hass,
"test",
"TestType",
slots={"floor": {"value": "invalid floor"}},
)

0 comments on commit 8fbd14e

Please sign in to comment.