Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add websockets transport mechanism to MQTT #15601

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/docs/configuration/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ mqtt:
host: mqtt.server.com
# Optional: port (default: shown below)
port: 1883
# Optional: MQTT transport mechanism (default: shown below)
# NOTE: must be tcp (raw MQTT) or websockets.
transport: tcp
# Optional: topic prefix (default: shown below)
# NOTE: must be unique if you are running multiple instances
topic_prefix: frigate
Expand Down
4 changes: 4 additions & 0 deletions frigate/comms/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ def _on_disconnect(

def _start(self) -> None:
"""Start mqtt client."""
logger.info("MQTT transport mechanism: %s" % str(self.mqtt_config.transport))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be an info log.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, debug maybe or get rid of this line ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug seems fine

self.client = mqtt.Client(
callback_api_version=CallbackAPIVersion.VERSION2,
client_id=self.mqtt_config.client_id,
transport=self.mqtt_config.transport,
)
self.client.on_connect = self._on_connect
self.client.on_disconnect = self._on_disconnect
Expand All @@ -180,6 +182,8 @@ def _start(self) -> None:
qos=1,
retain=True,
)
if self.mqtt_config.transport == "websockets":
self.client.ws_set_options(path="/mqtt")

# register callbacks
callback_types = [
Expand Down
10 changes: 9 additions & 1 deletion frigate/config/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from pydantic import Field, ValidationInfo, model_validator
from pydantic import Field, ValidationInfo, model_validator, field_validator
from typing_extensions import Self

from frigate.const import FREQUENCY_STATS_POINTS
Expand All @@ -15,6 +15,7 @@ class MqttConfig(FrigateBaseModel):
enabled: bool = Field(default=True, title="Enable MQTT Communication.")
host: str = Field(default="", title="MQTT Host")
port: int = Field(default=1883, title="MQTT Port")
transport: str = Field(default="tcp", title="MQTT Transport Mechanism")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if we added an enum class to MQTT and used that vs a manual field validator.

topic_prefix: str = Field(default="frigate", title="MQTT Topic Prefix")
client_id: str = Field(default="frigate", title="MQTT Client ID")
stats_interval: int = Field(
Expand All @@ -36,3 +37,10 @@ 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

@field_validator("transport")
@classmethod
def check_transport_mechanism(cls, v: str) -> str:
if v != "tcp" and v != "websockets":
raise ValueError("MQTT transport could only be tcp or websockets.")
return v
Loading