Skip to content

Commit b5256ec

Browse files
committed
Add support for channel variable in plugin args.
1 parent 117284a commit b5256ec

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

myDevices/plugins/analog.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
This module provides classes for interfacing with analog plugins.
33
"""
4-
import json
54
from myDevices.plugins.manager import PluginManager
65
from myDevices.utils.logger import info, debug
76

@@ -18,14 +17,14 @@ def __init__(self, adc):
1817
self.adc_name = adc
1918
self.adc = None
2019
self.read_args = {}
21-
self.pluginManager = PluginManager()
20+
self.plugin_manager = PluginManager()
2221
self.set_adc()
2322

2423
def set_adc(self):
2524
"""Sets the ADC plugin."""
2625
if not self.adc:
27-
self.adc = self.pluginManager.get_plugin_by_id(self.adc_name)
28-
self.read_args = json.loads(self.adc['read_args'])
26+
self.adc = self.plugin_manager.get_plugin_by_id(self.adc_name)
27+
self.read_args = self.plugin_manager.get_args(self.adc, 'read_args')
2928

3029
def read_value(self, channel, data_type=None):
3130
"""Read the data value on the specified channel."""

myDevices/plugins/manager.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ def load_plugin_from_file(self, filename):
8383
self.override_plugin_value(config, section, 'read_args', plugin)
8484
try:
8585
self.override_plugin_value(config, section, 'write', plugin)
86+
if 'write_args' not in plugin:
87+
plugin['write_args'] = '{}'
88+
self.override_plugin_value(config, section, 'write_args', plugin)
89+
except:
90+
pass
91+
try:
92+
self.override_plugin_value(config, section, 'set_function', plugin)
8693
except:
8794
pass
8895
try:
@@ -125,19 +132,27 @@ def get_plugin_by_id(self, id):
125132
return plugin
126133

127134
def override_plugin_value(self, config, section, key, plugin):
128-
"""Override the plugin value for the specified key if it exists in the config file"""
135+
"""Override the plugin value for the specified key if it exists in the config file."""
129136
if key not in plugin:
130137
plugin[key] = config.get(section, key)
131138
else:
132139
plugin[key] = config.get(section, key, plugin[key])
133140

141+
def get_args(self, plugin, key):
142+
"""Returns the specified args attribute as a dict."""
143+
args = plugin[key]
144+
if 'channel' in plugin:
145+
args = args.replace('$channel', plugin['channel'])
146+
return json.loads(args)
147+
134148
def get_plugin_readings(self):
135149
"""Return a list with current readings for all plugins."""
136150
readings = []
137151
for key, plugin in self.plugins.items():
138152
try:
139153
if 'channel' in plugin:
140-
value = getattr(plugin['instance'], plugin['read'])(**json.loads(plugin['read_args']))
154+
read_args = self.get_args(plugin, 'read_args')
155+
value = getattr(plugin['instance'], plugin['read'])(**read_args)
141156
value_dict = self.convert_to_dict(value)
142157
if value_dict:
143158
cayennemqtt.DataChannel.add(readings, cayennemqtt.DEV_SENSOR, key, name=plugin['name'], **value_dict)
@@ -186,7 +201,8 @@ def write_value(self, plugin, channel, value):
186201
if actuator in self.plugins.keys():
187202
try:
188203
write_function = getattr(self.plugins[actuator]['instance'], self.plugins[actuator]['write'])
189-
write_function(float(value))
204+
write_args = self.get_args(self.plugins[actuator], 'write_args')
205+
write_function(float(value), **write_args)
190206
except:
191207
return False
192208
else:

0 commit comments

Comments
 (0)