Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions custom_components/pyscript/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from .eval import AstEval, EvalFunc, EvalFuncVar
from .function import Function
from .logbook import RUNNING_EVENT_FUNC_ARGS
from .state import State

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -269,8 +270,9 @@ async def _call(self, data: DispatchData) -> None:
ev_name = self.name.replace(".", "_")
ev_entity_id = f"pyscript.{ev_name}"

# leave out the webhook request, which isn't JSON serializable
event_func_args = {key: val for key, val in data.func_args.items() if key != "request"}
event_func_args = {
key: data.func_args[key] for key in RUNNING_EVENT_FUNC_ARGS if key in data.func_args
}
event_data = {"name": ev_name, "entity_id": ev_entity_id, "func_args": event_func_args}
self.hass.bus.async_fire("pyscript_running", event_data, context=data.hass_context)
# Store HASS Context for this Task
Expand Down
4 changes: 4 additions & 0 deletions custom_components/pyscript/logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

_LOGGER = logging.getLogger(__name__)

# func_args kept in the pyscript_running event, which are what the logbook describes it with;
# others can be large or not JSON serializable (eg, a webhook request or service call data)
RUNNING_EVENT_FUNC_ARGS = ("trigger_type", "event_type", "var_name", "value", "trigger_time")


@callback
def async_describe_events(hass, async_describe_event): # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions custom_components/pyscript/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .eval import AstEval, EvalFunc, EvalFuncVar
from .event import Event
from .function import Function
from .logbook import RUNNING_EVENT_FUNC_ARGS
from .mqtt import Mqtt
from .state import STATE_VIRTUAL_ATTRS, State
from .webhook import Webhook
Expand Down Expand Up @@ -1366,8 +1367,7 @@ def call_action(self, notify_type, func_args, run_task=True):
ev_name = self.name.replace(".", "_")
ev_entity_id = f"pyscript.{ev_name}"

# leave out the webhook request, which isn't JSON serializable
event_func_args = {key: val for key, val in func_args.items() if key != "request"}
event_func_args = {key: func_args[key] for key in RUNNING_EVENT_FUNC_ARGS if key in func_args}
event_data = {"name": ev_name, "entity_id": ev_entity_id, "func_args": event_func_args}
Function.hass.bus.async_fire("pyscript_running", event_data, context=hass_context)

Expand Down
8 changes: 5 additions & 3 deletions tests/decorators/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ def webhook_no_request(payload):
await webhook.async_handle_webhook(hass, "test_no_req_hook", request)
await pyscript.wait_done({"a": 1})

# the request isn't JSON serializable, so it's left out of the pyscript_running event
# only the func_args the logbook uses are kept in the pyscript_running event
await hass.async_block_till_done()
assert len(running) == 2
for event in running:
assert "request" not in event.data["func_args"]
assert event.data["func_args"] == {"trigger_type": "webhook"}
json_bytes(event.data)


Expand All @@ -77,8 +78,9 @@ def webhook_handler_test(payload, request):
response = await webhook.async_handle_webhook(hass, "handler_hook", request)
assert json.loads(response.body) == {"method": "POST", "payload": {"b": 2}}

await hass.async_block_till_done()
assert len(running) == 1
assert "request" not in running[0].data["func_args"]
assert running[0].data["func_args"] == {"trigger_type": "webhook"}
json_bytes(running[0].data)


Expand Down
9 changes: 6 additions & 3 deletions tests/test_decorator_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ async def test_function_decorator_manager_success_calls_result_handlers(hass):
call_ast_ctx = DummyCallAstCtx(result="ok")
manager.add(result_handler)
hass_context = Context(id="call-parent")
func_args = {"trigger_type": "state", "var_name": "sensor.x", "value": "on", "arg1": object()}
fired_events = []

def event_listener(event):
Expand All @@ -662,17 +663,19 @@ def event_listener(event):

with patch.object(Function, "store_hass_context") as store_hass_context:
await call_function_manager(
manager, make_dispatch_data({"arg1": 1}, call_ast_ctx=call_ast_ctx, hass_context=hass_context)
manager,
make_dispatch_data(func_args, call_ast_ctx=call_ast_ctx, hass_context=hass_context),
)
await hass.async_block_till_done()

assert call_ast_ctx.calls == [(manager.eval_func, None, {"arg1": 1})]
assert call_ast_ctx.calls == [(manager.eval_func, None, func_args)]
assert result_handler.results == ["ok"]
assert len(fired_events) == 1
assert fired_events[0].data == {
"name": "file_hello_func",
"entity_id": "pyscript.file_hello_func",
"func_args": {"arg1": 1},
# only the func_args the logbook uses are kept
"func_args": {"trigger_type": "state", "var_name": "sensor.x", "value": "on"},
}
store_hass_context.assert_called_once_with(hass_context)

Expand Down
Loading