mknx / smarthome

http://mknx.github.io/smarthome/
GNU General Public License v3.0
126 stars 68 forks source link

CLI Plugin: Allow other plugins to add custom commands #182

Closed thernst-de closed 8 years ago

thernst-de commented 9 years ago

There are already severy pull requests which add additional commands to the CLI plugin. I would also like to be able to trigger some functionality in my own plugin via the CLI plugin.

This pull requests enhances the CLI plugin with a method to add custom commands. This method can be used by any other plugin. An example is already contained in the modified CLI-plugin.

In order to access the CLI plugin from other plugins, please see my previous pull request #181 which adds a method to get the plugin instance.

ohinckel commented 9 years ago

I like the idea of adding commands to the CLI plugin and I was also thinking about to extend the CLI plugin to be able to register further commands. So just a few suggestions for your pull request:

thernst-de commented 9 years ago

Thank you for your input. Indeed, removal of additional commands should be possible if adding is possible. I have implemented and pushed this functionality.

Using add_command/remove_command for the basic CLI-commands should also be not a big issue. I would volunteer to change the CLI plugin if this push request has chances to be included in the develop branch. The main thing that needs to be clarified when implementing this is the sorting of the commands for the "help" command.

ohinckel commented 8 years ago

One more idea which came into my mind: the last days I was thinking about this again and had more usecases where it is really useful to have "plugin hook" available which is called on each plugin for every plugin.

E.g. when starting the plugins, it will be checked if other plugins have a parse_plugin method, and if it exists it will be called with the plugin which is currently initialized (something like the parse_logic method for logics).

To implement the original feature requested and implemented by this pull request is done by using the following code in the plugin which want to add some commands to the CLI plugin:

def parse_plugin(plugin):
     if isinstance(plugin, plugins.cli.CLI):
         plugin.add_command('dosomething', self.cli.cmd, 'dosomething - Does something')

def cli_cmd(self):
     # do some stuff here

This way it is not only possible to do something with the CLI plugin, instead it will be possible to do something will all other plugins too - if the plugin requires this.

ohinckel commented 8 years ago

Patch is part of PR smarthomeNG/smarthome#103 in new project and we could close this issue now. Thanks for your great application!

@i-am-offline, can you close this isse?