diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ea2ac9e9..c6a1b9b7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.9 + rev: v0.7.0 hooks: - id: ruff name: ruff unused imports diff --git a/requirements.txt b/requirements.txt index 43217de9..f6ae32fc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ # Packages for source formatting # ----------------------------------------------------------------------------- pre-commit == 4.0.1 -ruff == 0.6.9 +ruff == 0.7.0 autotyping == 24.9.0 # ----------------------------------------------------------------------------- # Packages for other developement tasks diff --git a/src/HABApp/config/models/mqtt.py b/src/HABApp/config/models/mqtt.py index bb4d71cb..2cdbbbbd 100644 --- a/src/HABApp/config/models/mqtt.py +++ b/src/HABApp/config/models/mqtt.py @@ -1,6 +1,7 @@ import logging import random import string +from collections.abc import Generator from pathlib import Path from typing import Literal @@ -42,18 +43,14 @@ def _migrate_client_id(cls, data): class Subscribe(BaseModel): qos: QOS = Field(default=0, description='Default QoS for subscribing') - topics: tuple[tuple[str, QOS | None], ...] = Field(default=('#', )) - - @pydantic.field_validator('topics', mode='before') - def parse_topics(cls, v): - if not isinstance(v, (list, tuple, set)): - raise ValueError('must be a list') - ret = [] - for e in v: - if isinstance(e, str): - e = (e, None) - ret.append(tuple(e)) - return tuple(ret) + topics: tuple[str | tuple[str, QOS], ...] = Field(default=('#', 'topic/with/default/qos', ('topic/with/qos', 1))) + + def get_topic_qos(self) -> Generator[tuple[str, QOS], None, None]: + for obj in self.topics: + if isinstance(obj, str): + yield obj, self.qos + else: + yield obj class Publish(BaseModel): diff --git a/src/HABApp/mqtt/connection/subscribe.py b/src/HABApp/mqtt/connection/subscribe.py index e07db70e..5457411b 100644 --- a/src/HABApp/mqtt/connection/subscribe.py +++ b/src/HABApp/mqtt/connection/subscribe.py @@ -105,8 +105,8 @@ async def apply_subscriptions(self) -> None: # Unsubscribing has the corresponding handling, so we call that every time if not self.plugin_connection.has_errors: # subscription from config - for topic, qos in CONFIG.mqtt.subscribe.topics: - target[topic] = qos if qos is not None else default_qos + for topic, qos in CONFIG.mqtt.subscribe.get_topic_qos(): + target[topic] = qos # runtime subscriptions overwrite the subscriptions from the config file for topic, qos in self.runtime_subs.items(): target[topic] = qos diff --git a/tests/test_config/test_config.py b/tests/test_config/test_config.py index a525425e..6a3cfefe 100644 --- a/tests/test_config/test_config.py +++ b/tests/test_config/test_config.py @@ -32,8 +32,10 @@ def test_default_file() -> None: subscribe: qos: 0 # Default QoS for subscribing topics: - - - '#' - - + - '#' + - topic/with/default/qos + - - topic/with/qos + - 1 publish: qos: 0 # Default QoS when publishing values retain: false # Default retain flag when publishing values