{"action":"created","issue":{"url":"https://api.github.com/repos/n8henrie/fauxmo/issues/58","repository_url":"https://api.github.com/repos/n8henrie/fauxmo","labels_url":"https://api.github.com/repos/n8henrie/fauxmo/issues/58/labels{/name}","comments_url":"https://api.github.com/repos/n8henrie/fauxmo/issues/58/comments","events_url":"https://api.github.com/repos/n8henrie/fauxmo/issues/58/events","html_url":"https://github.com/n8henrie/fauxmo/issues/58","id":323250009,"number":58,"title":"Add close and lookup functionality to plugins","user":{"login":"howdypierce","id":2676683,"avatar_url":"https://avatars0.githubusercontent.com/u/2676683?v=4","gravatar_id":"","url":"https://api.github.com/users/howdypierce","html_url":"https://github.com/howdypierce","followers_url":"https://api.github.com/users/howdypierce/followers","following_url":"https://api.github.com/users/howdypierce/following{/other_user}","gists_url":"https://api.github.com/users/howdypierce/gists{/gist_id}","starred_url":"https://api.github.com/users/howdypierce/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/howdypierce/subscriptions","organizations_url":"https://api.github.com/users/howdypierce/orgs","repos_url":"https://api.github.com/users/howdypierce/repos","events_url":"https://api.github.com/users/howdypierce/events{/privacy}","received_events_url":"https://api.github.com/users/howdypierce/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-05-15T14:43:57Z","updated_at":"2018-05-15T22:53:58Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"## My Issue\r\nI'd like to propose two additions to the plugin API.  I've got both of these implemented already and I'm happy to submit a PR.\r\n\r\n1) Add a close function.  I've got a plugin which runs on a Raspberry Pi and uses RPi.GPIO to truly implement a Wemo switch ... meaning, the Pi controls a relay which switches an electrical outlet. The problem is, when you stop the fauxmo service, there is no opportunity for the plugin code to shut down the GPIO state cleanly.  More generically, I would think many plugins might need to clean up before exiting.\r\n\r\n   I'd propose that src/fauxmo/fauxmo.py keeps track of the plugin instances it creates, and then at shutdown it runs through those instances and calls a new close function on each.  For backward compatibility, I've added a do-nothing (but non abstract) FauxmoPlugin.close() function.  If plugins don’t need this functionality they can ignore the whole issue, but those plugins that need to perform cleanup can override close().  Existing plugins do not need to be modified.\r\n\r\n2) Add a lookup function.  This might be a little more of a stretch, but in some cases it might be useful for multiple plugin instances to interact with each other.  An example which I have implemented is a \"schedule\" plugin.  This schedule plugin in turn controls another plugin.  So, for instance, I have my GPIO plugin named \"Kitchen Light\".  You can use Alexa to \"turn on kitchen light\".  Then I have a second plugin, the schedule plugin, which is set to turn the kitchen light on at 5 am and turn it off at sunrise.  Because this schedule plugin is itself a fauxmo instance, you can also use Alexa to \"turn on kitchen light schedule\".  \r\n\r\n   However, in order for this whole approach to work, the schedule plugin instance needs to be able to find the GPIO plugin instance at runtime.\r\n\r\n   Extending the idea from the close function above, we're already maintaining a record of created plugin instances in fauxmo.py.  So I added a lookup function which allows plugins to find each other by name.  Therefore in the schedule plugin case, one of the parameters in config.json is the name of the \"slaved\" plugin, and when it comes time for the schedule to turn on the lights, it can do so.\r\n\r\nAs I mentioned, I've got both the above implemented.  Look forward to your thoughts.\r\n"},"comment":{"url":"https://api.github.com/repos/n8henrie/fauxmo/issues/comments/389339223","html_url":"https://github.com/n8henrie/fauxmo/issues/58#issuecomment-389339223","issue_url":"https://api.github.com/repos/n8henrie/fauxmo/issues/58","id":389339223,"user":{"login":"n8henrie","id":1234956,"avatar_url":"https://avatars1.githubusercontent.com/u/1234956?v=4","gravatar_id":"","url":"https://api.github.com/users/n8henrie","html_url":"https://github.com/n8henrie","followers_url":"https://api.github.com/users/n8henrie/followers","following_url":"https://api.github.com/users/n8henrie/following{/other_user}","gists_url":"https://api.github.com/users/n8henrie/gists{/gist_id}","starred_url":"https://api.github.com/users/n8henrie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/n8henrie/subscriptions","organizations_url":"https://api.github.com/users/n8henrie/orgs","repos_url":"https://api.github.com/users/n8henrie/repos","events_url":"https://api.github.com/users/n8henrie/events{/privacy}","received_events_url":"https://api.github.com/users/n8henrie/received_events","type":"User","site_admin":false},"created_at":"2018-05-15T22:53:58Z","updated_at":"2018-05-15T22:53:58Z","author_association":"OWNER","body":"Great thoughts.\r\n\r\n\\# 1: I've been thinking about the close issue much of the day -- totally makes sense. I agree, should be implemented as a non-abstract method to avoid a breaking API change for existing users. Instead of a `.close()` method, I really think this seems like a great place for a context manager, if that's possible with the asyncio API. Will look into it. Would definitely want to see what you've been using.\r\n\r\n\\# 2: I'm more hesitant about this one, ensuring interoperability between plugins seems easy to make the codebase significantly more complicated going forward. For most users, using the Routines built into Alexa seems like a cleaner solution for scheduling something, no? Do you have another example use case handy? Would be interested to take a look at the code you're using for this, but would want to think about it before moving forward."}}
{"actor":{"display_login":"n8henrie"}}