Skip to content

Commit

Permalink
feat: support MQTT_NAMESPACE via optional environment variable
Browse files Browse the repository at this point in the history
closes: #38
  • Loading branch information
JakobLichterfeld committed Apr 12, 2024
1 parent 306731a commit da6ab27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### New Features

- feat: support MQTT_NAMESPACE via optional environment variable (#38)

### Enhancements

- doc: remove version tag in example docker compose as it is obsolete in docker 25.05
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ This setup is recommended only if you are running TeslaMate Telegram Bot **on yo
restart: unless-stopped
environment:
# - CAR_ID=1 # optional, defaults to 1
- MQTT_BROKER_HOST=IP_Address
- MQTT_BROKER_HOST=IP_Address # defaults to 127.0.0.1
# - MQTT_BROKER_PORT=1883 #optional, defaults to 1883
# - MQTT_BROKER_USERNAME=username #optional, only needed when broker has authentication enabled
# - MQTT_BROKER_PASSWORD=password #optional, only needed when broker has authentication enabled
# - MQTT_NAMESPACE=namespace # optional, only needed when you specified MQTT_NAMESPACE on your TeslaMate installation
- TELEGRAM_BOT_API_KEY=secret_api_key
- TELEGRAM_BOT_CHAT_ID=secret_chat_id
ports:
Expand Down
11 changes: 10 additions & 1 deletion src/teslamate_telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
MQTT_BROKER_KEEPALIVE = 60
MQTT_BROKER_USERNAME_DEFAULT = ''
MQTT_BROKER_PASSWORD_DEFAULT = ''
MQTT_NAMESPACE_DEFAULT = ''

# Environment variables
TELEGRAM_BOT_API_KEY = 'TELEGRAM_BOT_API_KEY'
Expand All @@ -25,6 +26,7 @@
MQTT_BROKER_PASSWORD = 'MQTT_BROKER_PASSWORD'
MQTT_BROKER_HOST = 'MQTT_BROKER_HOST'
MQTT_BROKER_PORT = 'MQTT_BROKER_PORT'
MQTT_NAMESPACE = 'MQTT_NAMESPACE'
CAR_ID = 'CAR_ID'

##############################################################################
Expand Down Expand Up @@ -67,7 +69,14 @@ def get_env_variable(var_name, default_value=None):
)
raise EnvironmentError(error_message_car_id) from value_error_car_id

teslamate_mqtt_topic_base = f"teslamate/cars/{car_id}/"

namespace = get_env_variable(MQTT_NAMESPACE, MQTT_NAMESPACE_DEFAULT)
if namespace:
logging.info("Using MQTT namespace: %s", namespace)
teslamate_mqtt_topic_base = f"teslamate/{namespace}/cars/{car_id}/"
else:
teslamate_mqtt_topic_base = f"teslamate/cars/{car_id}/"

teslamate_mqtt_topic_update_available = teslamate_mqtt_topic_base + "update_available"
teslamate_mqtt_topic_update_version = teslamate_mqtt_topic_base + "update_version"

Expand Down

0 comments on commit da6ab27

Please sign in to comment.