mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-17 00:25:23 +03:00
Clean up mqtt
This commit is contained in:
parent
f7cc6b7ff3
commit
3a46651d44
@ -17,7 +17,8 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
def __init__(self, config: FrigateConfig) -> None:
|
def __init__(self, config: FrigateConfig) -> None:
|
||||||
self.config = config
|
self.config = config
|
||||||
self.mqtt_config = config.mqtt
|
self.mqtt_config = config.mqtt
|
||||||
self.connected: bool = False
|
self.connected = False
|
||||||
|
self.started = False
|
||||||
|
|
||||||
def subscribe(self, receiver: Callable) -> None:
|
def subscribe(self, receiver: Callable) -> None:
|
||||||
"""Wrapper for allowing dispatcher to subscribe."""
|
"""Wrapper for allowing dispatcher to subscribe."""
|
||||||
@ -26,7 +27,7 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
|
|
||||||
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
||||||
"""Wrapper for publishing when client is in valid state."""
|
"""Wrapper for publishing when client is in valid state."""
|
||||||
if not self.connected:
|
if not self.connected and self.started:
|
||||||
logger.error(f"Unable to publish to {topic}: client is not connected")
|
logger.error(f"Unable to publish to {topic}: client is not connected")
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -197,14 +198,6 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
|
|
||||||
for name in self.config.cameras.keys():
|
for name in self.config.cameras.keys():
|
||||||
for callback in callback_types:
|
for callback in callback_types:
|
||||||
# We need to pre-clear existing set topics because in previous
|
|
||||||
# versions the webUI retained on the /set topic but this is
|
|
||||||
# no longer the case.
|
|
||||||
self.client.publish(
|
|
||||||
f"{self.mqtt_config.topic_prefix}/{name}/{callback}/set",
|
|
||||||
None,
|
|
||||||
retain=True,
|
|
||||||
)
|
|
||||||
self.client.message_callback_add(
|
self.client.message_callback_add(
|
||||||
f"{self.mqtt_config.topic_prefix}/{name}/{callback}/set",
|
f"{self.mqtt_config.topic_prefix}/{name}/{callback}/set",
|
||||||
self.on_mqtt_command,
|
self.on_mqtt_command,
|
||||||
@ -253,6 +246,7 @@ class MqttClient(Communicator): # type: ignore[misc]
|
|||||||
# with connect_async, retries are handled automatically
|
# with connect_async, retries are handled automatically
|
||||||
self.client.connect_async(self.mqtt_config.host, self.mqtt_config.port, 60)
|
self.client.connect_async(self.mqtt_config.host, self.mqtt_config.port, 60)
|
||||||
self.client.loop_start()
|
self.client.loop_start()
|
||||||
|
self.started = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Unable to connect to MQTT server: {e}")
|
logger.error(f"Unable to connect to MQTT server: {e}")
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user