fix: pass mqtt credentials through compose
This commit is contained in:
+17
-2
@@ -116,6 +116,14 @@ def _text(value: Any, name: str, *, allow_none: bool = False) -> str | None:
|
||||
return value.strip()
|
||||
|
||||
|
||||
def _optional_text(value: Any, name: str) -> str | None:
|
||||
"""Normalize unset/blank optional environment values to ``None``."""
|
||||
|
||||
if value is None or (isinstance(value, str) and not value.strip()):
|
||||
return None
|
||||
return _text(value, name)
|
||||
|
||||
|
||||
def _integer(value: Any, name: str, *, minimum: int = 1, maximum: int | None = None) -> int:
|
||||
if isinstance(value, bool):
|
||||
raise ConfigError(f"{name} must be an integer")
|
||||
@@ -167,6 +175,13 @@ def _env_value(env: Mapping[str, str], key: str, fallback: Any) -> Any:
|
||||
return env[key] if key in env else fallback
|
||||
|
||||
|
||||
def _optional_env_value(env: Mapping[str, str], key: str, fallback: Any) -> Any:
|
||||
value = env.get(key)
|
||||
if value is None or not value.strip():
|
||||
return fallback
|
||||
return value
|
||||
|
||||
|
||||
def load_config(
|
||||
path: str | Path | None = None,
|
||||
*,
|
||||
@@ -192,8 +207,8 @@ def load_config(
|
||||
mqtt = MqttConfig(
|
||||
host=_text(_env_value(env, "MQTT_HOST", mqtt_raw.get("host", "10.40.0.2")), "mqtt.host"),
|
||||
port=_integer(_env_value(env, "MQTT_PORT", mqtt_raw.get("port", 1883)), "mqtt.port", maximum=65535),
|
||||
username=_text(_env_value(env, "MQTT_USERNAME", mqtt_raw.get("username")), "mqtt.username", allow_none=True),
|
||||
password=_text(_env_value(env, "MQTT_PASSWORD", mqtt_raw.get("password")), "mqtt.password", allow_none=True),
|
||||
username=_optional_text(_optional_env_value(env, "MQTT_USERNAME", mqtt_raw.get("username")), "mqtt.username"),
|
||||
password=_optional_text(_optional_env_value(env, "MQTT_PASSWORD", mqtt_raw.get("password")), "mqtt.password"),
|
||||
client_id=_text(_env_value(env, "MQTT_CLIENT_ID", mqtt_raw.get("client_id", "yt-controller")), "mqtt.client_id"),
|
||||
keepalive_seconds=_integer(mqtt_raw.get("keepalive_seconds", 30), "mqtt.keepalive_seconds"),
|
||||
reconnect_min_seconds=_integer(mqtt_raw.get("reconnect_min_seconds", 1), "mqtt.reconnect_min_seconds"),
|
||||
|
||||
Reference in New Issue
Block a user