Files
frigate/frigate/config/mqtt.py
T
b2f5315e09
CI / AMD64 Build (push) Canceled after 0s
CI / AMD64 Smoke Test (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
Validate MQTT TLS options before startup (#24951)
* Validate MQTT TLS verification options before client startup

* Test MQTT configuration recovery with authentication available

* Cover both MQTT TLS verification values in safe-mode recovery

* Use a documentation address in MQTT regression tests

* Skip MQTT TLS validation when MQTT is disabled

* Simplify MQTT TLS validation regression coverage

---------

Co-authored-by: Peter Kay <dougle.peter.kay@googlemail.com>
2026-11-12 10:04:09 -06:00

97 lines
3.4 KiB
Python

from typing import Self
from pydantic import Field, ValidationInfo, model_validator
from frigate.const import FREQUENCY_STATS_POINTS
from frigate.util.env import EnvString
from .base import FrigateBaseModel
__all__ = ["MqttConfig"]
class MqttConfig(FrigateBaseModel):
enabled: bool = Field(
default=True,
title="Enable MQTT",
description="Enable or disable MQTT integration for state, events, and snapshots.",
)
host: EnvString = Field(
default="",
title="MQTT host",
description="Hostname or IP address of the MQTT broker.",
)
port: int = Field(
default=1883,
title="MQTT port",
description="Port of the MQTT broker (usually 1883 for plain MQTT).",
)
topic_prefix: str = Field(
default="frigate",
title="Topic prefix",
description="MQTT topic prefix for all Frigate topics; must be unique if running multiple instances.",
)
client_id: str = Field(
default="frigate",
title="Client ID",
description="Client identifier used when connecting to the MQTT broker; should be unique per instance.",
)
stats_interval: int = Field(
default=60,
ge=FREQUENCY_STATS_POINTS,
title="Stats interval",
description="Interval in seconds for publishing system and camera stats to MQTT.",
)
user: EnvString | None = Field(
default=None,
title="MQTT username",
description="Optional MQTT username; can be provided via environment variables or secrets.",
)
password: EnvString | None = Field(
default=None,
title="MQTT password",
description="Optional MQTT password; can be provided via environment variables or secrets.",
validate_default=True,
)
tls_ca_certs: str | None = Field(
default=None,
title="TLS CA certs",
description="Path to CA certificate for TLS connections to the broker (for self-signed certs).",
)
tls_client_cert: str | None = Field(
default=None,
title="Client cert",
description="Client certificate path for TLS mutual authentication; do not set user/password when using client certs.",
)
tls_client_key: str | None = Field(
default=None,
title="Client key",
description="Private key path for the client certificate.",
)
tls_insecure: bool | None = Field(
default=None,
title="TLS insecure",
description="Allow insecure TLS connections by skipping hostname verification (not recommended).",
)
qos: int = Field(
default=0,
title="MQTT QoS",
description="Quality of Service level for MQTT publishes/subscriptions (0, 1, or 2).",
)
@model_validator(mode="after")
def user_requires_pass(self, info: ValidationInfo) -> Self:
if (self.user is None) != (self.password is None):
raise ValueError("Password must be provided with username.")
return self
@model_validator(mode="after")
def tls_insecure_requires_tls(self) -> Self:
"""Reject TLS verification settings without TLS when MQTT is enabled."""
if self.enabled and self.tls_insecure is not None and self.tls_ca_certs is None:
raise ValueError(
"tls_insecure requires tls_ca_certs. Remove tls_insecure for plaintext "
"MQTT, or set tls_ca_certs to a CA certificate path to enable TLS."
)
return self