Skip to content
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

Make it possible to attach items to a given plugin #53

Closed
wants to merge 7 commits into from
6 changes: 6 additions & 0 deletions bin/smarthome.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ def return_plugins(self):
for plugin in self._plugins:
yield plugin

def return_plugin(self, name):
return self._plugins.get_plugin(name)

def get_plugin_ident(self, plugin):
return self._plugins.get_plugin_ident(plugin)

#################################################################
# Logic Methods
#################################################################
Expand Down
24 changes: 24 additions & 0 deletions lib/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,31 @@ def __init__(self, smarthome, parent, path, config):
#############################################################
# Plugins
#############################################################
confPlugins = []
if 'plugin' in self.conf:
for pluginName in self.conf['plugin'] if type(self.conf['plugin']) is list else [self.conf['plugin']]:
plugin = self._sh.return_plugin(pluginName)
if plugin is None:
logger.error("Item {}: attached plugin {} not found.".format(self.id(), pluginName))
else:
confPlugins.append(plugin)

for plugin in self._sh.return_plugins():

# Check if plugin is explicitely configured in item or not
if len(confPlugins) and plugin not in confPlugins:
configurePlugin = True
for confPlugin in confPlugins:

# Skip plugin if plugin was not configured explictely, but another instance
# of the same class
if plugin != confPlugin and issubclass(type(plugin), type(confPlugin)):
configurePlugin = False
break

if configurePlugin == False:
continue

if hasattr(plugin, 'parse_item'):
update = plugin.parse_item(self)
if update:
Expand Down
12 changes: 12 additions & 0 deletions lib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ def __iter__(self):
for plugin in self._plugins:
yield plugin

def get_plugin(self, name):
for thread in self._threads:
if thread.name == name:
return thread.plugin
return None

def get_plugin_ident(self, plugin):
for thread in self._threads:
if thread.plugin == plugin:
return thread.ident
return None

def start(self):
logger.info('Start Plugins')
for plugin in self._threads:
Expand Down
1 change: 1 addition & 0 deletions plugins/sml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import logging
import time
import socket
import re
import serial
import threading
Expand Down