From 91c50dba057699e104e4fdbc9bc48ee3835b65a5 Mon Sep 17 00:00:00 2001 From: Linus Dietz <45101649+Dielee@users.noreply.github.com> Date: Mon, 3 Jul 2023 09:02:21 +0200 Subject: [PATCH] Add MQTT autodiscovery for HA MQTT Addon --- src/config.yaml | 14 ++++++++------ src/const.py | 2 +- src/main.py | 3 ++- src/run.sh | 7 ++++++- src/translations/en.yaml | 2 +- src/util.py | 26 ++++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/config.yaml b/src/config.yaml index f378026..64ea024 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -1,10 +1,12 @@ name: "Volvo2Mqtt" description: "Volvo AAOS MQTT bridge" -version: "1.6.5" +version: "1.7.0" slug: "volvo2mqtt" init: false url: "https://github.com/Dielee/volvo2mqtt" apparmor: true +services: + - 'mqtt:need' map: - addons:rw options: @@ -13,10 +15,10 @@ options: TZ: null debug: false mqtt: - broker: null - port: 1883 - username: "" - password: "" + broker: "auto_broker" + port: "auto_port" + username: "auto_user" + password: "auto_password" volvoData: username: null password: null @@ -32,7 +34,7 @@ schema: debug: bool mqtt: broker: str - port: int(1,) + port: str username: str? password: str? volvoData: diff --git a/src/const.py b/src/const.py index 9ef3411..0029f72 100644 --- a/src/const.py +++ b/src/const.py @@ -1,6 +1,6 @@ from config import settings -VERSION = "v1.6.5" +VERSION = "v1.7.0" OAUTH_URL = "https://volvoid.eu.volvocars.com/as/token.oauth2" VEHICLES_URL = "https://api.volvocars.com/connected-vehicle/v1/vehicles" diff --git a/src/main.py b/src/main.py index 423bfd8..9a1058f 100644 --- a/src/main.py +++ b/src/main.py @@ -2,11 +2,12 @@ import logging from volvo import authorize from mqtt import update_loop, connect from const import VERSION -from util import set_tz, setup_logging +from util import set_tz, setup_logging, set_mqtt_settings if __name__ == '__main__': set_tz() + set_mqtt_settings() setup_logging() logging.info("Starting volvo2mqtt version " + VERSION) authorize() diff --git a/src/run.sh b/src/run.sh index f8fbbff..4275529 100644 --- a/src/run.sh +++ b/src/run.sh @@ -1,4 +1,9 @@ -#!/bin/bash +#!/command/with-contenv bashio export IS_HA_ADDON="true" +export MQTTHOST=$(bashio::services mqtt "host") +export MQTTPORT=$(bashio::services mqtt "port") +export MQTTUSER=$(bashio::services mqtt "username") +export MQTTPASS=$(bashio::services mqtt "password") + python -u main.py \ No newline at end of file diff --git a/src/translations/en.yaml b/src/translations/en.yaml index 1be70de..2213515 100644 --- a/src/translations/en.yaml +++ b/src/translations/en.yaml @@ -13,7 +13,7 @@ configuration: description: Enable Volvo API debug, normaly this can stay off. If enabled, the complete log file can be found under \\\addons\volvo2mqtt\log\volvo2mqtt.log. mqtt: name: MQTT Broker settings - description: Broker is your mqtt broker IP eg "192.168.0.1". Mqtt username and password are optional. Broker port can be changed. If no value is given, port 1883 will be used. + description: Leave the settings as they are if you are using the MQTT Mosquitto Addon. If not, take a look at the readme from volvo2mqtt. volvoData: name: Volvo configuration options description: You have to enter your Volvo app credentials. The username is normally your email, and the password is the password you use for your Volvo app. The vin can stay empty. The Add-On will use any vin inside your Volvo account. VCCAPI key is required and comes from your Volvo developer account. Odometer multiplier is sometimes 10, sometimes 1. The same is applicable for average speed divider and average fuel consumption. Leave it as it is if you don't know what's right. Take a look at the GitHub repo for more information! \ No newline at end of file diff --git a/src/util.py b/src/util.py index 78d379d..abc2662 100644 --- a/src/util.py +++ b/src/util.py @@ -2,6 +2,7 @@ import logging import pytz import os import sys +import config from logging import handlers from datetime import datetime from const import units @@ -87,3 +88,28 @@ def convert_metric_values(value): else: return value + +def set_mqtt_settings(): + if os.environ.get("IS_HA_ADDON"): + if config.settings["mqtt"]["broker"] != "auto_broker" \ + or config.settings["mqtt"]["port"] != "auto_port" \ + or config.settings["mqtt"]["username"] != "auto_user" \ + or config.settings["mqtt"]["password"] != "auto_password": + # If settings were manually set, use the manually set settings + return None + + broker_host = os.getenv("MQTTHOST", None) + broker_port = os.getenv("MQTTPORT", None) + broker_user = os.getenv("MQTTUSER", None) + broker_pass = os.getenv("MQTTPASS", None) + + if not broker_host or not broker_port: + raise Exception("MQTT connection could not be established. Please check if your MQTT Add-On is running!") + + logging.debug("MQTT Credentials - Host " + broker_host + " Port: " + str(broker_port) + + " User: " + str(broker_user) + " Pass: " + str(broker_pass)) + + config.settings["mqtt"]["broker"] = broker_host + config.settings["mqtt"]["port"] = broker_port + config.settings["mqtt"]["username"] = broker_user + config.settings["mqtt"]["password"] = broker_pass