diff --git a/src/HABApp/config/models/mqtt.py b/src/HABApp/config/models/mqtt.py index 2cdbbbbd..776f5331 100644 --- a/src/HABApp/config/models/mqtt.py +++ b/src/HABApp/config/models/mqtt.py @@ -30,6 +30,7 @@ class Connection(BaseModel): password: str = '' tls: TLSSettings = Field(default_factory=TLSSettings) + # implemented 2024.02.0 @pydantic.model_validator(mode='before') @classmethod def _migrate_client_id(cls, data): @@ -52,6 +53,30 @@ def get_topic_qos(self) -> Generator[tuple[str, QOS], None, None]: else: yield obj + # Implemented 2024.11.0 + @pydantic.model_validator(mode='before') + @classmethod + def _migrate_topics(cls, data): + if isinstance(data, dict) and (topics := data.get('topics', [])) is not None: + for i, topic_obj in enumerate(topics): + if not isinstance(topic_obj, list): + continue + topic, qos = topic_obj + if qos is not None: + continue + + log = logging.getLogger('HABApp.Config') + log.warning('Empty QoS is not longer allowed for subscribing to topics.') + log.warning('Specify QOS or remove empty entry, e.g from') + log.warning(f' - - {topic:s}') + log.warning(' - ') + log.warning('to') + log.warning(f' - {topic:s}') + + topics[i] = topic + + return data + class Publish(BaseModel): qos: QOS = Field(default=0, description='Default QoS when publishing values') diff --git a/tests/test_config/test_config.py b/tests/test_config/test_config.py index 6a3cfefe..d3e50bc2 100644 --- a/tests/test_config/test_config.py +++ b/tests/test_config/test_config.py @@ -1,6 +1,14 @@ +import logging import re +import pytest +from pydantic import ValidationError + from HABApp import CONFIG +from easyconfig.yaml import yaml_safe + +from HABApp.config.models.mqtt import Subscribe +from tests.helpers import LogCollector def test_default_file() -> None: @@ -55,3 +63,29 @@ def test_default_file() -> None: item: HABApp_Ping # Name of the Numberitem interval: 10 # Seconds between two pings ''' + + +def test_migrate(test_logs: LogCollector): + text = ''' + subscribe: + qos: 0 # Default QoS for subscribing + topics: + - - '#' + - + publish: + ''' + + obj = yaml_safe.load(text) + Subscribe.model_validate(obj['subscribe']) + + test_logs.add_expected( + 'HABApp.Config', logging.WARNING, + [ + 'Empty QoS is not longer allowed for subscribing to topics.', + 'Specify QOS or remove empty entry, e.g from', + ' - - #', + ' - ', + 'to', + ' - #', + ] + )