tesla-local-control / tesla-local-control-addon

Control your Tesla locally from HomeAssistant
Apache License 2.0
21 stars 2 forks source link

[add-on] Goodbye bashio #46

Closed baylanger closed 2 months ago

baylanger commented 3 months ago

Using the code below, at the top of run.sh, it allows to drop bashio+bash for the addon image and instead simply use "sh".

With the below change, how far would we be to use the add-on image for the standalone installation?

The script below reads directly from the add-on's config file and populates shell variables like we must use in the standalone version.

#!/bin/sh

# Pick config file either from HA addon or standalone Docker instance
[ -f /tmp/.bashio/addons.self.options.config.cache ] \
  && tesla_config_file=/tmp/.bashio/addons.self.options.config.cache \
  || tesla_config_file=/share/tesla_ble_mqtt/options.config

# Time for some fun!
if [ -f $tesla_config_file ]; then

  # get list of all settings found in the config file
  settings_list="$(jq '.' $tesla_config_file | awk -F\" '{print $2}'| grep -v '^$')"

  # for each setting, assign a shell variable (uppercase) with the value from the config file
  for keyword in $settings_list; do
    UPPER_keyword=$(echo $keyword | tr '[:lower:]' '[:upper:]')
    # get the value for a setting
    value="$(cat $tesla_config_file | jq ".${keyword}")"
    # declare a shell variable with the setting's value
    eval $UPPER_keyword="$value"
  done

  echo "Configuration Options are:
    VIN=$VIN
    MQTT_IP=$MQTT_IP
    MQTT_PORT=$MQTT_PORT
    MQTT_USER=$MQTT_USER
    MQTT_PWD=Not Shown
    SEND_CMD_RETRY_DELAY=$SEND_CMD_RETRY_DELAY"
else
  echo "ERROR couldn't find a configuration file"
  exit 1
fi

Example:

# cat /tmp/.bashio/addons.self.options.config.cache
{"vin":"5YJ3E1EB6J1111111","ble_presence_detection_ttl":300,"debug":false,"mqtt_ip":"mqtt.domain.com","mqtt_port":"1883","mqtt_user":"tesla","mqtt_pwd":"Hidden","send_cmd_retry_delay":5}
# sh goodbye-bashio.sh
Configuration Options are:
    VIN=5YJ3E1EB6J1111111
    MQTT_IP=mqtt.domain.com
    MQTT_PORT=1883
    MQTT_USER=tesla
    MQTT_PWD=Not Shown
    SEND_CMD_RETRY_DELAY=5
baylanger commented 3 months ago

Actually the code to declare the variables could run only for the add-on and we'd continue to use what we have now for standalone, aka regular shell env. variables.

# Are we in a HA addon Docker instance
if [ -f /tmp/.bashio/addons.self.options.config.cache ]; then
  addos_config_file=/tmp/.bashio/addons.self.options.config.cache

  # get list of all settings found in the config file
    settings_list="$(jq '.' $addos_config_file | awk -F\" '{print $2}'| grep -v '^$')"

    # for each setting, assign a shell variable (uppercase) with the value from the config file
    for keyword in $settings_list; do
      UPPER_keyword=$(echo $keyword | tr '[:lower:]' '[:upper:]')
    ...
raphmur commented 2 months ago

Sticking to bashio for addon