AaronLionsheep / ShellyMQTT

IndigoDomo Plugin that interfaces with Shelly devices over MQTT.
MIT License
0 stars 0 forks source link

Move callbacks to separate file #103

Closed AaronLionsheep closed 3 years ago

AaronLionsheep commented 3 years ago

In an effort to clean up the main plugin.py file, callback methods could be moved to a callbacks.py file. However, the plugin expects these callback to be defined in plugin.py, so some python magic could be used. If callbacks are renamed with a prefix of callback_, then we could use __getattr__ to intercept the function calls and return a method from a callbacks.py file.

class MagicClass():
    def __init__(self, obj):
        self.an_obj = obj

    def __getattr__(self, method_name):
        def method(*args, **kwargs):
            print("Handling unknown method: '{}'".format(method_name))
            if kwargs:
                print("It had the following key word arguments: " + str(kwargs))
            if args:
                print("It had the following positional arguments: " + str(args))
            return getattr(self.an_obj, method_name)(*args, **kwargs)
        return method

(https://stackoverflow.com/questions/10879806/python-how-to-intercept-a-method-call-which-does-not-exists)