blakeblackshear.frigate/frigate/mqtt.py

26 lines
1.0 KiB
Python
Raw Normal View History

2020-11-01 15:06:15 +01:00
import paho.mqtt.client as mqtt
2020-11-03 15:15:58 +01:00
from frigate.config import MqttConfig
def create_mqtt_client(config: MqttConfig):
client = mqtt.Client(client_id=config.client_id)
2020-11-01 15:06:15 +01:00
def on_connect(client, userdata, flags, rc):
# TODO: use logging library
print("On connect called")
if rc != 0:
if rc == 3:
print ("MQTT Server unavailable")
elif rc == 4:
print ("MQTT Bad username or password")
elif rc == 5:
print ("MQTT Not authorized")
else:
print ("Unable to connect to MQTT: Connection refused. Error code: " + str(rc))
2020-11-03 15:15:58 +01:00
client.publish(config.topic_prefix+'/available', 'online', retain=True)
2020-11-01 15:06:15 +01:00
client.on_connect = on_connect
2020-11-03 15:15:58 +01:00
client.will_set(config.topic_prefix+'/available', payload='offline', qos=1, retain=True)
if not config.user is None:
client.username_pw_set(config.user, password=config.password)
client.connect(config.host, config.port, 60)
2020-11-01 15:06:15 +01:00
client.loop_start()
return client