Skip to content

Commit

Permalink
Set correct response value in service description when `async_set_s…
Browse files Browse the repository at this point in the history
…ervice_schema` is used (home-assistant#95985)

* Mark scripts as response optional, make it always return a response if return_response is set

* Update test_init.py

* Revert "Update test_init.py"

This reverts commit 8e113e5.

* Split + add test
  • Loading branch information
bramkragten authored and joostlek committed Jul 6, 2023
1 parent c27719d commit 002baab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions homeassistant/helpers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,13 @@ def async_set_service_schema(
if "target" in schema:
description["target"] = schema["target"]

if (
response := hass.services.supports_response(domain, service)
) != SupportsResponse.NONE:
description["response"] = {
"optional": response == SupportsResponse.OPTIONAL,
}

hass.data.pop(ALL_SERVICE_DESCRIPTIONS_CACHE, None)
hass.data[SERVICE_DESCRIPTION_CACHE][(domain, service)] = description

Expand Down
17 changes: 17 additions & 0 deletions tests/helpers/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,19 @@ async def test_async_get_all_descriptions(hass: HomeAssistant) -> None:
None,
SupportsResponse.ONLY,
)
hass.services.async_register(
logger.DOMAIN,
"another_service_with_response",
lambda x: None,
None,
SupportsResponse.OPTIONAL,
)
service.async_set_service_schema(
hass,
logger.DOMAIN,
"another_service_with_response",
{"description": "response service"},
)

descriptions = await service.async_get_all_descriptions(hass)
assert "another_new_service" in descriptions[logger.DOMAIN]
Expand All @@ -600,6 +613,10 @@ async def test_async_get_all_descriptions(hass: HomeAssistant) -> None:
assert descriptions[logger.DOMAIN]["service_with_only_response"]["response"] == {
"optional": False
}
assert "another_service_with_response" in descriptions[logger.DOMAIN]
assert descriptions[logger.DOMAIN]["another_service_with_response"]["response"] == {
"optional": True
}

# Verify the cache returns the same object
assert await service.async_get_all_descriptions(hass) is descriptions
Expand Down

0 comments on commit 002baab

Please sign in to comment.