Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Oct 21, 2024
1 parent b774e8d commit 5f12c49
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/HABApp/config/models/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
Expand Down
34 changes: 34 additions & 0 deletions tests/test_config/test_config.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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',
' - #',
]
)

0 comments on commit 5f12c49

Please sign in to comment.