Skip to content

Commit 509b543

Browse files
authored
Fix execution of async @after() handlers. (#118)
The response of the handler was passed to `asyncio.ensure_future()` and that resulted in a `TypeError`. ``` Traceback (most recent call last): File "/home/developer/projects/tmh-ocpp/.env/lib/python3.8/site-packages/websockets/server.py", line 191, in handler await self.ws_handler(self, path) File "examples/v16/central_system.py", line 44, in on_connect await cp.start() File "/home/developer/projects/tmh-ocpp/.env/lib/python3.8/site-packages/ocpp/charge_point.py", line 126, in start await self.route_message(message) File "/home/developer/projects/tmh-ocpp/.env/lib/python3.8/site-packages/ocpp/charge_point.py", line 144, in route_message await self._handle_call(msg) File "/home/developer/projects/tmh-ocpp/.env/lib/python3.8/site-packages/ocpp/charge_point.py", line 222, in _handle_call asyncio.ensure_future(response) File "/home/developer/.pyenv/versions/3.8.0/lib/python3.8/asyncio/tasks.py", line 673, in ensure_future raise TypeError('An asyncio.Future, a coroutine or an awaitable is ' TypeError: An asyncio.Future, a coroutine or an awaitable is required ``` Fixes: #117
1 parent f1c78ea commit 509b543

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

ocpp/charge_point.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ async def _handle_call(self, msg):
218218
# after handler
219219
response = handler(**snake_case_payload)
220220
if inspect.isawaitable(response):
221-
response = await response
222-
asyncio.ensure_future(response)
221+
await response
223222
except KeyError:
224223
# '_on_after' hooks are not required. Therefore ignore exception
225224
# when no '_on_after' hook is installed.

tests/v16/test_v16_charge_point.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def on_boot_notification(charge_point_model, charge_point_vendor, **kwargs): #
2929
)
3030

3131
@after(Action.BootNotification)
32-
def after_boot_notification(charge_point_model, charge_point_vendor,
32+
async def after_boot_notification(charge_point_model, charge_point_vendor,
3333
**kwargs): # noqa
3434
assert charge_point_vendor == "Alfen BV"
3535
assert charge_point_model == "ICU Eve Mini"

0 commit comments

Comments
 (0)