Skip to content

Commit 3e81dff

Browse files
authored
Merge pull request #671 from Morg42/develop
core: add metadata checks for sdp version
2 parents 084883c + c1ffd0d commit 3e81dff

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

lib/metadata.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from lib.utils import Version
2929
import lib.shyaml as shyaml
3030
from lib.constants import (YAML_FILE, FOO, META_DATA_TYPES, META_DATA_DEFAULTS)
31+
from lib.model.sdp.globals import SDP_VERSION
3132

3233
META_MODULE_PARAMETER_SECTION = 'parameters'
3334
META_PLUGIN_SECTION = 'plugin'
@@ -526,6 +527,29 @@ def test_pythoncompatibility(self):
526527
return True
527528

528529

530+
def test_sdpcompatibility(self):
531+
"""
532+
Test if the plugin is based on SDP and check if the version requirements match
533+
534+
:return: True if not SDP-based or the SDP version is in the supported range
535+
:rtype: bool
536+
"""
537+
sdp_version = SDP_VERSION
538+
min_sdpversion = Version.format(str(self.get_string('sdp_minversion')))
539+
max_sdpversion = Version.format(str(self.get_string('sdp_maxversion')))
540+
mod_version = Version.format(self.get_string('version'))
541+
542+
if min_sdpversion != '':
543+
if Version.compare(min_sdpversion, sdp_version, '>'):
544+
logger.error(f"{self._addon_type} '{self._addon_name}' {mod_version}: SmartDevicePlugin {sdp_version} is too old for this {self._addon_type}. It requires at least version {Version.format(min_sdpversion)}. The {self._addon_type} was not loaded.")
545+
return False
546+
if max_sdpversion != '':
547+
if Version.compare(max_sdpversion, sdp_version, '<'):
548+
logger.error(f"{self._addon_type} '{self._addon_name}' {mod_version}: SmartDevicePlugin {sdp_version} is too new for this {self._addon_type}. It requires a version up to {Version.format(max_sdpversion)}. The {self._addon_type} was not loaded.")
549+
return False
550+
return True
551+
552+
529553
def get_version(self):
530554
"""
531555
Returns the version of the addon

lib/model/sdp/globals.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#
4040
#############################################################################################################################################################################################################################################
4141

42+
# this is the internal SDP version
43+
SDP_VERSION = '1.0.3'
44+
4245
# plugin attributes, used in plugin config 'device' and instance creation (**kwargs)
4346

4447
# general attributes

lib/model/smartdeviceplugin.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
from lib.shtime import Shtime
4343

4444
from lib.model.sdp.globals import (
45-
PLUGIN_ATTR_SEND_TIMEOUT, update, ATTR_NAMES, CMD_ATTR_CMD_SETTINGS, CMD_ATTR_ITEM_ATTRS,
45+
SDP_VERSION, update,
46+
PLUGIN_ATTR_SEND_TIMEOUT, ATTR_NAMES, CMD_ATTR_CMD_SETTINGS, CMD_ATTR_ITEM_ATTRS,
4647
CMD_ATTR_ITEM_TYPE, CMD_ATTR_LOOKUP, CMD_ATTR_OPCODE, CMD_ATTR_PARAMS,
4748
CMD_ATTR_READ, CMD_ATTR_READ_CMD, CMD_ATTR_WRITE, CMD_IATTR_ATTRIBUTES,
4849
CMD_IATTR_CYCLE, CMD_IATTR_ENFORCE, CMD_IATTR_INITIAL,
@@ -64,7 +65,6 @@
6465
from lib.model.sdp.connection import SDPConnection
6566
from lib.model.sdp.protocol import SDPProtocol # noqa
6667

67-
MIN_SDP_VERSION = "MIN_SDP_VERSION"
6868

6969
class SDPResultError(OSError):
7070
pass
@@ -87,23 +87,10 @@ class SmartDevicePlugin(SmartPlugin):
8787
The implemented methods are described below, inherited methods are only
8888
described if changed/overwritten.
8989
"""
90-
91-
# this is the internal SDP version
92-
SDP_VERSION = '1.0.3'
93-
94-
# this is the placeholder version of the derived plugin, not of SDP
95-
PLUGIN_VERSION = '0.0.1'
96-
9790
def __init__(self, sh, logger=None, **kwargs):
9891
"""
9992
Initalizes the plugin.
10093
"""
101-
102-
if not self._check_sdp_version():
103-
self.logger.error(f'minimum sdp version {getattr(self, MIN_SDP_VERSION)} required, but only {self.SDP_VERSION} present')
104-
self._init_complete = False
105-
return
106-
10794
# adjust imported ITEM_ATTR_xxx identifiers
10895
self._set_item_attributes()
10996

@@ -1052,17 +1039,6 @@ def _process_additional_data(self, command, data, value, custom, by):
10521039
#
10531040
#
10541041

1055-
def _check_sdp_version(self):
1056-
""" check if minimum sdp version is set and fulfilled """
1057-
if hasattr(self, MIN_SDP_VERSION):
1058-
self.logger.debug(f'minimum sdp version {getattr(self, MIN_SDP_VERSION)} required, {self.SDP_VERSION} present')
1059-
v_is = self.SDP_VERSION.split('.') + ['0', '0', '0']
1060-
v_min = getattr(self, MIN_SDP_VERSION).split('.') + ['0', '0', '0']
1061-
vi_is = 1000000 * int(v_is[0]) + 1000 * int(v_is[1]) + int(v_is[2])
1062-
vi_min = 1000000 * int(v_min[0]) + 1000 * int(v_min[1]) + int(v_min[2])
1063-
return vi_is >= vi_min
1064-
return True
1065-
10661042
def _get_custom_value(self, command, data):
10671043
"""
10681044
extract custom value from data

lib/plugin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,10 @@ def __init__(self, smarthome, configfile):
146146
struct_keys = list(item_structs.keys())
147147
for struct_name in struct_keys:
148148
self._sh.items.add_struct_definition(plugin_name, struct_name, item_structs[struct_name])
149-
150149
# Test if plugin is disabled
151150
if str(_conf[plugin].get('plugin_enabled', None)).lower() == 'false':
152151
logger.info("Section {} (plugin_name {}) is disabled - Plugin not loaded".format(plugin, _conf[plugin].get('plugin_name', None)))
153-
elif self.meta.test_shngcompatibility() and self.meta.test_pythoncompatibility():
152+
elif self.meta.test_shngcompatibility() and self.meta.test_pythoncompatibility() and self.meta.test_sdpcompatibility():
154153
classname, classpath = self._get_classname_and_classpath(_conf[plugin], plugin_name)
155154
if (classname == '') and (classpath == ''):
156155
logger.error("Plugins, section {}: plugin_name is not defined".format(plugin))
@@ -171,7 +170,7 @@ def __init__(self, smarthome, configfile):
171170
os.chdir((self._sh._base_dir))
172171
try:
173172
plugin_thread = PluginWrapper(smarthome, plugin, classname, classpath, args, instance, self.meta, self._configfile)
174-
if plugin_thread._init_complete == True:
173+
if plugin_thread._init_complete is True:
175174
try:
176175
try:
177176
startorder = self.meta.pluginsettings.get('startorder', 'normal').lower()

0 commit comments

Comments
 (0)