mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +00:00
Add service response values to service descriptions (#95262)
This commit is contained in:
parent
fa03324bbd
commit
d95c241a1a
@ -31,6 +31,7 @@ from homeassistant.core import (
|
|||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
ServiceCall,
|
ServiceCall,
|
||||||
ServiceResponse,
|
ServiceResponse,
|
||||||
|
SupportsResponse,
|
||||||
callback,
|
callback,
|
||||||
)
|
)
|
||||||
from homeassistant.exceptions import (
|
from homeassistant.exceptions import (
|
||||||
@ -635,6 +636,13 @@ async def async_get_all_descriptions(
|
|||||||
if "target" in yaml_description:
|
if "target" in yaml_description:
|
||||||
description["target"] = yaml_description["target"]
|
description["target"] = yaml_description["target"]
|
||||||
|
|
||||||
|
if (
|
||||||
|
response := hass.services.supports_response(domain, service)
|
||||||
|
) != SupportsResponse.NONE:
|
||||||
|
description["response"] = {
|
||||||
|
"optional": response == SupportsResponse.OPTIONAL,
|
||||||
|
}
|
||||||
|
|
||||||
descriptions_cache[cache_key] = description
|
descriptions_cache[cache_key] = description
|
||||||
|
|
||||||
descriptions[domain][service] = description
|
descriptions[domain][service] = description
|
||||||
|
@ -18,7 +18,7 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, HomeAssistant, ServiceCall
|
from homeassistant.core import Context, HomeAssistant, ServiceCall, SupportsResponse
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
device_registry as dr,
|
device_registry as dr,
|
||||||
entity_registry as er,
|
entity_registry as er,
|
||||||
@ -575,8 +575,31 @@ async def test_async_get_all_descriptions(hass: HomeAssistant) -> None:
|
|||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
logger.DOMAIN, "another_new_service", lambda x: None, None
|
logger.DOMAIN, "another_new_service", lambda x: None, None
|
||||||
)
|
)
|
||||||
|
hass.services.async_register(
|
||||||
|
logger.DOMAIN,
|
||||||
|
"service_with_optional_response",
|
||||||
|
lambda x: None,
|
||||||
|
None,
|
||||||
|
SupportsResponse.OPTIONAL,
|
||||||
|
)
|
||||||
|
hass.services.async_register(
|
||||||
|
logger.DOMAIN,
|
||||||
|
"service_with_only_response",
|
||||||
|
lambda x: None,
|
||||||
|
None,
|
||||||
|
SupportsResponse.ONLY,
|
||||||
|
)
|
||||||
|
|
||||||
descriptions = await service.async_get_all_descriptions(hass)
|
descriptions = await service.async_get_all_descriptions(hass)
|
||||||
assert "another_new_service" in descriptions[logger.DOMAIN]
|
assert "another_new_service" in descriptions[logger.DOMAIN]
|
||||||
|
assert "service_with_optional_response" in descriptions[logger.DOMAIN]
|
||||||
|
assert descriptions[logger.DOMAIN]["service_with_optional_response"][
|
||||||
|
"response"
|
||||||
|
] == {"optional": True}
|
||||||
|
assert "service_with_only_response" in descriptions[logger.DOMAIN]
|
||||||
|
assert descriptions[logger.DOMAIN]["service_with_only_response"]["response"] == {
|
||||||
|
"optional": False
|
||||||
|
}
|
||||||
|
|
||||||
# Verify the cache returns the same object
|
# Verify the cache returns the same object
|
||||||
assert await service.async_get_all_descriptions(hass) is descriptions
|
assert await service.async_get_all_descriptions(hass) is descriptions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user