Skip to content

Commit afb98c7

Browse files
committed
Add support for disabling plugins.
1 parent a7089f2 commit afb98c7

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

myDevices/plugins/disable.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from myDevices.utils.config import Config
3+
from myDevices.utils.logger import setInfo, info, error
4+
from myDevices.plugins.manager import PLUGIN_FOLDER
5+
6+
if __name__ == '__main__':
7+
# Run the code to disable a plugin in a script so it can be called via sudo
8+
setInfo()
9+
info(sys.argv)
10+
if len(sys.argv) != 3:
11+
error('Plugin not disabled, invalid arguments')
12+
sys.exit(1)
13+
filename = sys.argv[1]
14+
if filename.startswith(PLUGIN_FOLDER) and filename.endswith('.plugin'):
15+
section = sys.argv[2]
16+
config = Config(filename)
17+
if section in config.sections():
18+
config.set(section, 'enabled', 'false')
19+
else:
20+
error('Section \'{}\' not found in {}'.format(section, filename))
21+
sys.exit(1)
22+
else:
23+
sys.exit(1)

myDevices/plugins/manager.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
import myDevices.cloud.cayennemqtt as cayennemqtt
1111
from myDevices.utils.config import Config
1212
from myDevices.utils.logger import debug, error, exception, info
13+
from myDevices.utils.subprocess import executeCommand
14+
15+
PLUGIN_FOLDER = '/etc/myDevices/plugins'
1316

1417

1518
class PluginManager():
1619
"""Loads plugins and reads/writes plugin data"""
1720

1821
def __init__(self):
1922
"""Initializes the plugin manager and loads the plugin list"""
20-
self.plugin_folder = '/etc/myDevices/plugins'
23+
self.plugin_folder = PLUGIN_FOLDER
2124
self.plugins = {}
2225
self.load_plugins()
2326

@@ -33,6 +36,8 @@ def load_plugin_from_file(self, filename):
3336
enabled = config.get(section, 'enabled', 'true').lower() == 'true'
3437
if enabled:
3538
plugin = {
39+
'filename': filename,
40+
'section': section,
3641
'channel': config.get(section, 'channel'),
3742
'name': config.get(section, 'name', section),
3843
'module': config.get(section, 'module'),
@@ -96,10 +101,13 @@ def convert_to_dict(self, value):
96101
value_dict['unit'] = 'null'
97102
return value_dict
98103

99-
def is_plugin(self, plugin, channel):
100-
"""Returns True if the specified plugin:channel is a valid plugin"""
104+
def is_plugin(self, plugin, channel=None):
105+
"""Returns True if the specified plugin or plugin:channel are valid plugins"""
101106
try:
102-
return plugin + ':' + channel in self.plugins.keys()
107+
key = plugin
108+
if channel is not None:
109+
key = plugin + ':' + channel
110+
return key in self.plugins.keys()
103111
except:
104112
return False
105113

@@ -116,4 +124,20 @@ def write_value(self, plugin, channel, value):
116124
return False
117125
else:
118126
return False
119-
return True
127+
return True
128+
129+
def disable(self, plugin):
130+
"""Disable the specified plugin"""
131+
disabled = False
132+
try:
133+
output, result = executeCommand('sudo python3 -m myDevices.plugins.disable "{}" "{}"'.format(self.plugins[plugin]['filename'], self.plugins[plugin]['section']))
134+
if result == 0:
135+
disabled = True
136+
info('Plugin \'{}\' disabled'.format(plugin))
137+
else:
138+
info('Plugin \'{}\' not disabled'.format(plugin))
139+
del self.plugins[plugin]
140+
except Exception as e:
141+
info(e)
142+
pass
143+
return disabled

myDevices/sensors/sensors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ def RemoveSensor(self, name):
280280
"""
281281
bVal = False
282282
try:
283+
if self.pluginManager.is_plugin(name):
284+
return self.pluginManager.disable(name)
283285
sensorRemove = name
284286
with self.sensorMutex:
285287
retValue = manager.removeDevice(sensorRemove)

0 commit comments

Comments
 (0)