From 50b5eb9863e5d543a64ae4fac1218f0f31734fcf Mon Sep 17 00:00:00 2001 From: Linus Dietz <45101649+Dielee@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:06:59 +0200 Subject: [PATCH] Add mqtt availability --- src/config.yaml | 2 +- src/const.py | 4 +++- src/mqtt.py | 8 ++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/config.yaml b/src/config.yaml index 9cefd6a..db4cf8e 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -1,6 +1,6 @@ name: "Volvo2Mqtt" description: "Volvo AAOS MQTT bridge" -version: "1.4.2" +version: "1.4.3" slug: "volvo2mqtt" init: false url: "https://github.com/Dielee/volvo2mqtt" diff --git a/src/const.py b/src/const.py index 806fd27..f32fdbf 100644 --- a/src/const.py +++ b/src/const.py @@ -1,6 +1,6 @@ from config import settings -VERSION = "v1.4.2" +VERSION = "v1.4.3" OAUTH_URL = "https://volvoid.eu.volvocars.com/as/token.oauth2" VEHICLES_URL = "https://api.volvocars.com/connected-vehicle/v1/vehicles" @@ -19,6 +19,8 @@ ENGINE_STATE_URL = "https://api.volvocars.com/connected-vehicle/v2/vehicles/{0}/ BATTERY_CHARGE_STATE_URL = "https://api.volvocars.com/connected-vehicle/v2/vehicles/{0}/battery-charge-level" FUEL_STATE_URL = "https://api.volvocars.com/connected-vehicle/v2/vehicles/{0}/fuel" +availability_topic = "volvoAAOS2mqtt/availability" + charging_system_states = {"CHARGING_SYSTEM_CHARGING": "Charging", "CHARGING_SYSTEM_IDLE": "Idle", "CHARGING_SYSTEM_FAULT": "Fault", "CHARGING_SYSTEM_UNSPECIFIED": "Unspecified"} diff --git a/src/mqtt.py b/src/mqtt.py index 25f7dcc..1795787 100644 --- a/src/mqtt.py +++ b/src/mqtt.py @@ -8,7 +8,7 @@ from datetime import datetime from babel.dates import format_datetime from config import settings from const import CLIMATE_START_URL, CLIMATE_STOP_URL, CAR_LOCK_URL, \ - CAR_UNLOCK_URL + CAR_UNLOCK_URL, availability_topic mqtt_client: mqtt.Client @@ -20,6 +20,7 @@ climate_timer = {} def connect(): client = mqtt.Client("volvoAAOS2mqtt") + client.will_set(availability_topic, "offline", 0, False) if settings["mqtt"]["username"] and settings["mqtt"]["password"]: client.username_pw_set(settings["mqtt"]["username"], settings["mqtt"]["password"]) port = 1883 @@ -39,6 +40,7 @@ def connect(): def on_connect(client, userdata, flags, rc): + mqtt_client.publish(availability_topic, "online") if len(subscribed_topics) > 0: for topic in subscribed_topics: mqtt_client.subscribe(topic) @@ -142,7 +144,8 @@ def create_ha_devices(): "icon": f"mdi:{entity['icon']}", "state_topic": f"homeassistant/{entity['domain']}/{vin}_{entity['id']}/state", "device": device, - "unique_id": f"volvoAAOS2mqtt_{vin}_{entity['id']}" + "unique_id": f"volvoAAOS2mqtt_{vin}_{entity['id']}", + "availability_topic": availability_topic } if entity.get('unit'): config["unit_of_measurement"] = entity["unit"] @@ -162,3 +165,4 @@ def create_ha_devices(): retain=True ) time.sleep(2) + mqtt_client.publish(availability_topic, "online")