Skip to content

Commit

Permalink
Make service responses optional
Browse files Browse the repository at this point in the history
  • Loading branch information
zhbjsh committed Nov 25, 2024
1 parent 20b3976 commit b3637f7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions custom_components/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,16 @@ def async_register_services(hass: HomeAssistant, domain: str):

def get_response(coro: Coroutine):
@wraps(coro)
async def wrapper(call: ServiceCall) -> ServiceResponse:
async def wrapper(call: ServiceCall) -> ServiceResponse | None:
entry_ids = await async_extract_config_entry_ids(hass, call)
data = await asyncio.gather(
*(coro(hass.data[domain][entry_id], call) for entry_id in entry_ids)
)
return {"results": [result for results in data for result in results]}
return (
{"results": [result for results in data for result in results]}
if call.return_response
else None
)

return wrapper

Expand Down Expand Up @@ -394,45 +398,45 @@ async def restart(entry_data: EntryData, call: ServiceCall) -> CommandOutput:
SERVICE_EXECUTE_COMMAND,
execute_command,
EXECUTE_COMMAND_SCHEMA,
SupportsResponse.ONLY,
SupportsResponse.OPTIONAL,
)

hass.services.async_register(
domain,
SERVICE_RUN_ACTION,
run_action,
RUN_ACTION_SCHEMA,
SupportsResponse.ONLY,
SupportsResponse.OPTIONAL,
)

hass.services.async_register(
domain,
SERVICE_POLL_SENSOR,
poll_sensor,
None,
SupportsResponse.ONLY,
SupportsResponse.OPTIONAL,
)

hass.services.async_register(
domain,
SERVICE_TURN_ON,
turn_on,
None,
SupportsResponse.ONLY,
SupportsResponse.OPTIONAL,
)

hass.services.async_register(
domain,
SERVICE_TURN_OFF,
turn_off,
None,
SupportsResponse.ONLY,
SupportsResponse.OPTIONAL,
)

hass.services.async_register(
domain,
SERVICE_RESTART,
restart,
None,
SupportsResponse.ONLY,
SupportsResponse.OPTIONAL,
)

0 comments on commit b3637f7

Please sign in to comment.