Skip to content

Commit

Permalink
Expose exec service call result as event
Browse files Browse the repository at this point in the history
  • Loading branch information
kvj committed Mar 3, 2023
1 parent d44e00a commit aaddd2b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion custom_components/openwrt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ async def async_exec(call):
await device.do_file_exec(
parts[0],
parts[1:],
call.data.get("environment", {})
call.data.get("environment", {}),
call.data.get("extra", {})
)

hass.services.async_register(DOMAIN, "reboot", async_reboot)
Expand Down
13 changes: 12 additions & 1 deletion custom_components/openwrt/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def do_reboot(self):
dict()
)

async def do_file_exec(self, command: str, params, env: dict):
async def do_file_exec(self, command: str, params, env: dict, extra: dict):
_LOGGER.debug(
f"Executing command: {self._id}: {command} with {params} env={env}")
result = await self._ubus.api_call(
Expand All @@ -169,6 +169,17 @@ async def do_file_exec(self, command: str, params, env: dict):
dict(command=command, params=params, env=env)
)
_LOGGER.debug(f"Execute result: {self._id}: {result}")
self._coordinator.hass.bus.async_fire(
"openwrt_exec_result",
{
"address": self._config.get("address"),
"id": self._config.get("id"),
"command": command,
"code": result.get("code", 1),
"stdout": result.get("stdout", ""),
**extra,
},
)

async def update_ap(self, configs) -> dict:
result = dict()
Expand Down
6 changes: 6 additions & 0 deletions custom_components/openwrt/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ exec:
name: Environment variables
description: Map of Environment variables names with values
required: false
selector:
object: {}
extra:
name: Extra event fields
description: Arbitrary object added to the execute result event
required: false
selector:
object: {}

0 comments on commit aaddd2b

Please sign in to comment.