Code from yaml/import functions dynamically?

Hi,

I have a few shelly relays around the house. They send different events depending on how many times the physical switch attached to themhas been flipped.

The action for most events will just be light.toggle so I’ll be able to just package these actions in the apps yaml

actions:
 - light/toggle:
   entity_id: some_light

And call each action using call_service()

Some events will require unique logic though and I’d like to use code for these events.

But I am not sure how to best realize that.

Would it be ok/recommended to have something like this in the apps yaml?

function_module: some_module
function_name: some_function

And then in the apps code do:

import importlib
module_name = self.args.get("function_module")
func_name = self.args.get("function_name")
external_module = importlib.import_module(module_name)
my_function = getattr(external_module, func_name)
my_function(...)

Or is there a better way of doing this? It feels kinda hacky.