forked from peter/volvo2mqtt
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf6568d945 |
+7
-3
@@ -28,7 +28,7 @@ def connect():
|
|||||||
client_id = "volvoAAOS2mqtt" if os.environ.get("IS_HA_ADDON") \
|
client_id = "volvoAAOS2mqtt" if os.environ.get("IS_HA_ADDON") \
|
||||||
else "volvoAAOS2mqtt_" + settings.volvoData["username"].replace("+", "")
|
else "volvoAAOS2mqtt_" + settings.volvoData["username"].replace("+", "")
|
||||||
try:
|
try:
|
||||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id)
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id, clean_session=False)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# paho-mqtt < 2.0 does not have CallbackAPIVersion
|
# paho-mqtt < 2.0 does not have CallbackAPIVersion
|
||||||
client = mqtt.Client(client_id)
|
client = mqtt.Client(client_id)
|
||||||
@@ -40,6 +40,7 @@ def connect():
|
|||||||
client.on_message = safe_on_message
|
client.on_message = safe_on_message
|
||||||
client.on_disconnect = on_disconnect
|
client.on_disconnect = on_disconnect
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
|
client.reconnect_delay_set(min_delay=1, max_delay=30)
|
||||||
|
|
||||||
client.will_set(availability_topic, "offline", 0, False)
|
client.will_set(availability_topic, "offline", 0, False)
|
||||||
if settings["mqtt"]["username"] and settings["mqtt"]["password"]:
|
if settings["mqtt"]["username"] and settings["mqtt"]["password"]:
|
||||||
@@ -50,10 +51,9 @@ def connect():
|
|||||||
if isinstance(conf_port, int):
|
if isinstance(conf_port, int):
|
||||||
if conf_port > 0:
|
if conf_port > 0:
|
||||||
port = settings["mqtt"]["port"]
|
port = settings["mqtt"]["port"]
|
||||||
client.connect(settings["mqtt"]["broker"], port)
|
client.connect(settings["mqtt"]["broker"], port, 120)
|
||||||
|
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
client.subscribe("volvoAAOS2mqtt/otp_code")
|
|
||||||
|
|
||||||
global mqtt_client
|
global mqtt_client
|
||||||
mqtt_client = client
|
mqtt_client = client
|
||||||
@@ -145,6 +145,7 @@ def on_connect(client, userdata, flags, rc):
|
|||||||
logging.info("MQTT connected")
|
logging.info("MQTT connected")
|
||||||
|
|
||||||
send_heartbeat()
|
send_heartbeat()
|
||||||
|
mqtt_client.subscribe("volvoAAOS2mqtt/otp_code")
|
||||||
if len(subscribed_topics) > 0:
|
if len(subscribed_topics) > 0:
|
||||||
for topic in subscribed_topics:
|
for topic in subscribed_topics:
|
||||||
mqtt_client.subscribe(topic)
|
mqtt_client.subscribe(topic)
|
||||||
@@ -439,6 +440,7 @@ def update_ha_device(entity, vin, state):
|
|||||||
mqtt_client.publish(
|
mqtt_client.publish(
|
||||||
f"homeassistant/{entity['domain']}/volvoAAOS2mqtt/{vin}_{entity['id']}/config",
|
f"homeassistant/{entity['domain']}/volvoAAOS2mqtt/{vin}_{entity['id']}/config",
|
||||||
json.dumps(config),
|
json.dumps(config),
|
||||||
|
qos=1,
|
||||||
retain=True
|
retain=True
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -489,8 +491,10 @@ def create_ha_devices():
|
|||||||
mqtt_client.publish(
|
mqtt_client.publish(
|
||||||
f"homeassistant/{entity['domain']}/volvoAAOS2mqtt/{vin}_{entity['id']}/config",
|
f"homeassistant/{entity['domain']}/volvoAAOS2mqtt/{vin}_{entity['id']}/config",
|
||||||
json.dumps(config),
|
json.dumps(config),
|
||||||
|
qos=1,
|
||||||
retain=True
|
retain=True
|
||||||
)
|
)
|
||||||
|
time.sleep(0.05)
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
send_heartbeat()
|
send_heartbeat()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user