Skip to content

Feature suggestion: Global Hooks #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

a-alak
Copy link
Contributor

@a-alak a-alak commented Jul 6, 2025

Changes included in this PR

Two new decorators - before_message and after_message - that can be used to register global hooks.
Added a discover_message_hooks utility function that creates a mapping of hooks for the ChargePoint subclass.
The ChargePoint class stores hook mapping in property _message_hooks, it includes a new private method to streamline calling of hooks that handles both synchronous and asynchronous hooks.
Added hook execution before and after routing the calls.

Current behavior

If you want to run a function for every call, no matter the type, you currently need to add it to a handler for every type of call (or do some workarounds with overwriting methods).
I find my self having the same code to save calls on all @after handlers.

New behavior

To add global hooks you can do:

...
from ocpp.routing import before_message, after_message


class MyChargePoint(ChargePoint):
    # This will run before call handlers for all types of calls, can also be sync    
    @before_message
    async def some_async_func(self, raw_msg):
        await run_some_code()

    # This will run after call handlers for all types of calls, can also be async
    @after_message
    def some_sync_func(self, raw_msg, parsed_msg):
        run_some_code()

Impact

I believe there is no breaking changes with this implementation.
Global hooks is implemented with the same decorator design as the call handlers.
Question is how useful is it?
I have one relevant case, saving all calls. But is there other use cases?

Here is further questions to consider with this feature:

  • Will it be relevant to call the hooks with parsed data? unpack and camel_to_snake_case is run after the before hook.
  • Does it make sense to enable the hooks to modify call data or returning early error?
  • This could also be implemented as hooks to pre- and postprocess sending calls. Will that have any value?

Checklist

  1. Does your submission pass the existing tests?
  2. Are there new tests that cover these additions/changes?
  3. Have you linted your code locally before submission?

…to routing with the @before_message and @after_message decorators. The hooks are added to _message_hooks property of the ChargePoint class and executed either before or after routing the ocpp call.
@a-alak a-alak requested review from mdwcrft and proelke as code owners July 6, 2025 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant