Add support for responses to call_service
WS cmd (#98610)
* Add support for responses to call_service WS cmd * Revert ServiceNotFound removal and add a parameter for return_response * fix type * fix tests * remove exception handling that was added * Revert unnecessary modifications * Use kwargs
This commit is contained in:
parent
229944c21c
commit
618b666126
3 changed files with 102 additions and 13 deletions
|
@ -18,7 +18,14 @@ from homeassistant.const import (
|
|||
MATCH_ALL,
|
||||
SIGNAL_BOOTSTRAP_INTEGRATIONS,
|
||||
)
|
||||
from homeassistant.core import Context, Event, HomeAssistant, State, callback
|
||||
from homeassistant.core import (
|
||||
Context,
|
||||
Event,
|
||||
HomeAssistant,
|
||||
ServiceResponse,
|
||||
State,
|
||||
callback,
|
||||
)
|
||||
from homeassistant.exceptions import (
|
||||
HomeAssistantError,
|
||||
ServiceNotFound,
|
||||
|
@ -213,6 +220,7 @@ def handle_unsubscribe_events(
|
|||
vol.Required("service"): str,
|
||||
vol.Optional("target"): cv.ENTITY_SERVICE_FIELDS,
|
||||
vol.Optional("service_data"): dict,
|
||||
vol.Optional("return_response", default=False): bool,
|
||||
}
|
||||
)
|
||||
@decorators.async_response
|
||||
|
@ -220,7 +228,6 @@ async def handle_call_service(
|
|||
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
|
||||
) -> None:
|
||||
"""Handle call service command."""
|
||||
blocking = True
|
||||
# We do not support templates.
|
||||
target = msg.get("target")
|
||||
if template.is_complex(target):
|
||||
|
@ -228,15 +235,19 @@ async def handle_call_service(
|
|||
|
||||
try:
|
||||
context = connection.context(msg)
|
||||
await hass.services.async_call(
|
||||
msg["domain"],
|
||||
msg["service"],
|
||||
msg.get("service_data"),
|
||||
blocking,
|
||||
context,
|
||||
response = await hass.services.async_call(
|
||||
domain=msg["domain"],
|
||||
service=msg["service"],
|
||||
service_data=msg.get("service_data"),
|
||||
blocking=True,
|
||||
context=context,
|
||||
target=target,
|
||||
return_response=msg["return_response"],
|
||||
)
|
||||
connection.send_result(msg["id"], {"context": context})
|
||||
result: dict[str, Context | ServiceResponse] = {"context": context}
|
||||
if msg["return_response"]:
|
||||
result["response"] = response
|
||||
connection.send_result(msg["id"], result)
|
||||
except ServiceNotFound as err:
|
||||
if err.domain == msg["domain"] and err.service == msg["service"]:
|
||||
connection.send_error(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue