6
6
import json
7
7
import os
8
8
import sys
9
+ from configparser import NoOptionError
9
10
10
11
import myDevices .cloud .cayennemqtt as cayennemqtt
11
12
from myDevices .utils .config import Config
@@ -24,7 +25,6 @@ def __init__(self, callback=None):
24
25
self .plugin_folder = PLUGIN_FOLDER
25
26
self .callback = callback
26
27
self .plugins = {}
27
- self .extensions = {}
28
28
29
29
def load_plugin_from_file (self , filename ):
30
30
"""Loads a plugin from a specified plugin config file and adds it to the plugin list."""
@@ -43,18 +43,17 @@ def load_plugin_from_file(self, filename):
43
43
try :
44
44
enabled = config .get (section , 'enabled' , 'true' ).lower () == 'true'
45
45
inherit = config .get (section , 'inherit' , None )
46
- extension = config .get (section , 'extension' , 'false' ).lower () == 'true'
47
46
if enabled or section in inherited_from :
48
47
plugin = {
49
48
'enabled' : enabled ,
50
49
'filename' : filename ,
51
50
'section' : section ,
52
51
'name' : config .get (section , 'name' , section ),
53
52
}
54
- if not extension :
53
+ try :
55
54
plugin ['channel' ] = config .get (section , 'channel' )
56
55
plugin ['id' ] = plugin_name + ':' + plugin ['channel' ]
57
- else :
56
+ except NoOptionError :
58
57
plugin ['id' ] = plugin_name + ':' + section
59
58
inherit_items = {}
60
59
if inherit in config .sections ():
@@ -79,6 +78,9 @@ def load_plugin_from_file(self, filename):
79
78
device_class = getattr (imported_module , plugin ['class' ])
80
79
plugin ['instance' ] = device_class (** json .loads (plugin ['init_args' ]))
81
80
self .override_plugin_value (config , section , 'read' , plugin )
81
+ if 'read_args' not in plugin :
82
+ plugin ['read_args' ] = '{}'
83
+ self .override_plugin_value (config , section , 'read_args' , plugin )
82
84
try :
83
85
self .override_plugin_value (config , section , 'write' , plugin )
84
86
except :
@@ -92,10 +94,7 @@ def load_plugin_from_file(self, filename):
92
94
self .override_plugin_value (config , section , 'unregister_callback' , plugin )
93
95
except :
94
96
pass
95
- if not extension :
96
- self .plugins [plugin ['id' ]] = plugin
97
- else :
98
- self .extensions [plugin ['id' ]] = plugin
97
+ self .plugins [plugin ['id' ]] = plugin
99
98
loaded .append (section )
100
99
except Exception as e :
101
100
error (e )
@@ -111,20 +110,19 @@ def load_plugins(self):
111
110
#Remove any disabled plugins that were only loaded because they are inherited from.
112
111
self .plugins = {key :value for key , value in self .plugins .items () if value ['enabled' ]}
113
112
info ('Enabled plugins: {}' .format (self .plugins .keys ()))
114
- info ('Enabled extensions: {}' .format (self .extensions .keys ()))
115
113
116
114
def get_plugin (self , filename , section ):
117
115
"""Return the plugin for the corresponding filename and section."""
118
116
return next (plugin for plugin in self .plugins .values () if plugin ['filename' ] == filename and plugin ['section' ] == section )
119
117
120
- def get_extension (self , id ):
121
- """Return the extension with the corresponding id."""
122
- extension = None
118
+ def get_plugin_by_id (self , id ):
119
+ """Return the plugin with the corresponding id."""
120
+ plugin = None
123
121
try :
124
- extension = self .extensions [id ]
122
+ plugin = self .plugins [id ]
125
123
except :
126
124
pass
127
- return extension
125
+ return plugin
128
126
129
127
def override_plugin_value (self , config , section , key , plugin ):
130
128
"""Override the plugin value for the specified key if it exists in the config file"""
@@ -138,10 +136,11 @@ def get_plugin_readings(self):
138
136
readings = []
139
137
for key , plugin in self .plugins .items ():
140
138
try :
141
- value = getattr (plugin ['instance' ], plugin ['read' ])()
142
- value_dict = self .convert_to_dict (value )
143
- if value_dict :
144
- cayennemqtt .DataChannel .add (readings , cayennemqtt .DEV_SENSOR , key , name = plugin ['name' ], ** value_dict )
139
+ if 'channel' in plugin :
140
+ value = getattr (plugin ['instance' ], plugin ['read' ])(** json .loads (plugin ['read_args' ]))
141
+ value_dict = self .convert_to_dict (value )
142
+ if value_dict :
143
+ cayennemqtt .DataChannel .add (readings , cayennemqtt .DEV_SENSOR , key , name = plugin ['name' ], ** value_dict )
145
144
except KeyError as e :
146
145
debug ('Missing key {} in plugin \' {}\' ' .format (e , plugin ['name' ]))
147
146
except :
0 commit comments