M8-R01: add configurable MQTT client identity

This commit is contained in:
2026-08-24 00:44:28 +02:00
parent 2a47dab272
commit e59c192097
12 changed files with 184 additions and 15 deletions
+14 -1
View File
@@ -1,7 +1,8 @@
from functools import lru_cache
from pathlib import Path
import re
from pydantic import computed_field
from pydantic import computed_field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -53,6 +54,7 @@ class Settings(BaseSettings):
mqtt_username: str = ""
mqtt_password: str = ""
mqtt_tls_enabled: bool = False
mqtt_client_id: str = "home-automation"
# Home Assistant MQTT Discovery (T08 wires into CONFIG_FIELDS/UI; T11 does publishing).
ha_discovery_enabled: bool = False
@@ -81,6 +83,17 @@ class Settings(BaseSettings):
extra="ignore",
)
@field_validator("mqtt_client_id", mode="before")
@classmethod
def validate_mqtt_client_id(cls, value: object) -> str:
"""Normalize a broker-safe base client identity used by every MQTT client."""
if not isinstance(value, str):
raise ValueError("MQTT client ID must be a string")
normalized = value.strip()
if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}", normalized):
raise ValueError("MQTT client ID must be a non-empty ASCII slug")
return normalized
@computed_field
@property
def is_development(self) -> bool: