Skip to content

Commit

Permalink
Merge pull request #671 from Morg42/develop
Browse files Browse the repository at this point in the history
core: add metadata checks for sdp version
  • Loading branch information
Morg42 authored Aug 17, 2024
2 parents 084883c + c1ffd0d commit 3e81dff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
24 changes: 24 additions & 0 deletions lib/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from lib.utils import Version
import lib.shyaml as shyaml
from lib.constants import (YAML_FILE, FOO, META_DATA_TYPES, META_DATA_DEFAULTS)
from lib.model.sdp.globals import SDP_VERSION

META_MODULE_PARAMETER_SECTION = 'parameters'
META_PLUGIN_SECTION = 'plugin'
Expand Down Expand Up @@ -526,6 +527,29 @@ def test_pythoncompatibility(self):
return True


def test_sdpcompatibility(self):
"""
Test if the plugin is based on SDP and check if the version requirements match
:return: True if not SDP-based or the SDP version is in the supported range
:rtype: bool
"""
sdp_version = SDP_VERSION
min_sdpversion = Version.format(str(self.get_string('sdp_minversion')))
max_sdpversion = Version.format(str(self.get_string('sdp_maxversion')))
mod_version = Version.format(self.get_string('version'))

if min_sdpversion != '':
if Version.compare(min_sdpversion, sdp_version, '>'):
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.")
return False
if max_sdpversion != '':
if Version.compare(max_sdpversion, sdp_version, '<'):
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.")
return False
return True


def get_version(self):
"""
Returns the version of the addon
Expand Down
3 changes: 3 additions & 0 deletions lib/model/sdp/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#
#############################################################################################################################################################################################################################################

# this is the internal SDP version
SDP_VERSION = '1.0.3'

# plugin attributes, used in plugin config 'device' and instance creation (**kwargs)

# general attributes
Expand Down
28 changes: 2 additions & 26 deletions lib/model/smartdeviceplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
from lib.shtime import Shtime

from lib.model.sdp.globals import (
PLUGIN_ATTR_SEND_TIMEOUT, update, ATTR_NAMES, CMD_ATTR_CMD_SETTINGS, CMD_ATTR_ITEM_ATTRS,
SDP_VERSION, update,
PLUGIN_ATTR_SEND_TIMEOUT, ATTR_NAMES, CMD_ATTR_CMD_SETTINGS, CMD_ATTR_ITEM_ATTRS,
CMD_ATTR_ITEM_TYPE, CMD_ATTR_LOOKUP, CMD_ATTR_OPCODE, CMD_ATTR_PARAMS,
CMD_ATTR_READ, CMD_ATTR_READ_CMD, CMD_ATTR_WRITE, CMD_IATTR_ATTRIBUTES,
CMD_IATTR_CYCLE, CMD_IATTR_ENFORCE, CMD_IATTR_INITIAL,
Expand All @@ -64,7 +65,6 @@
from lib.model.sdp.connection import SDPConnection
from lib.model.sdp.protocol import SDPProtocol # noqa

MIN_SDP_VERSION = "MIN_SDP_VERSION"

class SDPResultError(OSError):
pass
Expand All @@ -87,23 +87,10 @@ class SmartDevicePlugin(SmartPlugin):
The implemented methods are described below, inherited methods are only
described if changed/overwritten.
"""

# this is the internal SDP version
SDP_VERSION = '1.0.3'

# this is the placeholder version of the derived plugin, not of SDP
PLUGIN_VERSION = '0.0.1'

def __init__(self, sh, logger=None, **kwargs):
"""
Initalizes the plugin.
"""

if not self._check_sdp_version():
self.logger.error(f'minimum sdp version {getattr(self, MIN_SDP_VERSION)} required, but only {self.SDP_VERSION} present')
self._init_complete = False
return

# adjust imported ITEM_ATTR_xxx identifiers
self._set_item_attributes()

Expand Down Expand Up @@ -1052,17 +1039,6 @@ def _process_additional_data(self, command, data, value, custom, by):
#
#

def _check_sdp_version(self):
""" check if minimum sdp version is set and fulfilled """
if hasattr(self, MIN_SDP_VERSION):
self.logger.debug(f'minimum sdp version {getattr(self, MIN_SDP_VERSION)} required, {self.SDP_VERSION} present')
v_is = self.SDP_VERSION.split('.') + ['0', '0', '0']
v_min = getattr(self, MIN_SDP_VERSION).split('.') + ['0', '0', '0']
vi_is = 1000000 * int(v_is[0]) + 1000 * int(v_is[1]) + int(v_is[2])
vi_min = 1000000 * int(v_min[0]) + 1000 * int(v_min[1]) + int(v_min[2])
return vi_is >= vi_min
return True

def _get_custom_value(self, command, data):
"""
extract custom value from data
Expand Down
5 changes: 2 additions & 3 deletions lib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ def __init__(self, smarthome, configfile):
struct_keys = list(item_structs.keys())
for struct_name in struct_keys:
self._sh.items.add_struct_definition(plugin_name, struct_name, item_structs[struct_name])

# Test if plugin is disabled
if str(_conf[plugin].get('plugin_enabled', None)).lower() == 'false':
logger.info("Section {} (plugin_name {}) is disabled - Plugin not loaded".format(plugin, _conf[plugin].get('plugin_name', None)))
elif self.meta.test_shngcompatibility() and self.meta.test_pythoncompatibility():
elif self.meta.test_shngcompatibility() and self.meta.test_pythoncompatibility() and self.meta.test_sdpcompatibility():
classname, classpath = self._get_classname_and_classpath(_conf[plugin], plugin_name)
if (classname == '') and (classpath == ''):
logger.error("Plugins, section {}: plugin_name is not defined".format(plugin))
Expand All @@ -171,7 +170,7 @@ def __init__(self, smarthome, configfile):
os.chdir((self._sh._base_dir))
try:
plugin_thread = PluginWrapper(smarthome, plugin, classname, classpath, args, instance, self.meta, self._configfile)
if plugin_thread._init_complete == True:
if plugin_thread._init_complete is True:
try:
try:
startorder = self.meta.pluginsettings.get('startorder', 'normal').lower()
Expand Down

0 comments on commit 3e81dff

Please sign in to comment.