New logic

This commit is contained in:
Alexandre
2021-11-13 14:28:05 +01:00
parent 46c0a417c8
commit eef6b1906f
2 changed files with 52 additions and 17 deletions

View File

@@ -26,7 +26,8 @@
"CONSUMPTION_PRICE_BASE": "str?", "CONSUMPTION_PRICE_BASE": "str?",
"CONSUMPTION_PRICE_HC": "str?", "CONSUMPTION_PRICE_HC": "str?",
"CONSUMPTION_PRICE_HP": "str?", "CONSUMPTION_PRICE_HP": "str?",
"CARD_MYENEDIS": "str?" "CARD_MYENEDIS": "str?",
"custom_var": "str?"
}, },
"slug": "enedisgateway2mqtt", "slug": "enedisgateway2mqtt",
"upstream": "0.6.0", "upstream": "0.6.0",

View File

@@ -5,28 +5,62 @@
################# #################
# Create the config file # Create the config file
mkdir -p /config/enedisgateway2mqtt CONFIGSOURCE="/config/enedisgateway2mqtt/enedisgateway2mqtt.conf" #file
touch /config/enedisgateway2mqtt/enedisgateway2mqtt.conf mkdir -p "$(dirname "${CONFIGSOURCE}")" #create dir
touch ${CONFIGSOURCE} #create file
# Read the config file ##########################
# Read all addon options #
##########################
bashio::log.info "All variables defined in the addon will be exported to the config file located in /config/enedisgateway2mqtt"
################# # Get the default keys from the original file
# Create config # JSONSOURCE="/data/options.json"
################# mapfile -t arr < <(jq -r 'keys[]' ${JSONSOURCE})
echo " " # For all keys in options.json
bashio::log.info "Setting variables" for KEYS in ${arr[@]}; do
echo " " # if the custom_var field is used
for VARIABLES in "ACCESS_TOKEN" "PDL" "MQTT_HOST" "MQTT_PORT" "MQTT_PREFIX" "MQTT_CLIENT_ID" "MQTT_USERNAME" "MQTT_PASSWORD" "RETAIN" "QOS" "GET_CONSUMPTION" "GET_PRODUCTION" "HA_AUTODISCOVERY" "HA_AUTODISCOVERY_PREFIX" "CONSUMPTION_PRICE_BASE" "CONSUMPTION_PRICE_HC" "CONSUMPTION_PRICE_HP" "CARD_MYENEDIS"; do if ${KEYS} ="custom_var"; then
if bashio::config.has_value $VARIABLES; then VALUES=$(jq .$KEYS ${JSONSOURCE})
export $VARIABLES=$(bashio::config $VARIABLES) for SUBKEYS in ${VALUES//,/ }; do
echo "$VARIABLES set to $(bashio::config $VARIABLES)" [[ ! $SUBKEYS =~ ^.+[=].+$ ]] && bashio::log.fatal "Your custom_var field does not follow the structure KEY=\"text\",KEY2=\"text2\" it will be ignored" && continue || true
# Remove if already existing
sed -i "/$KEYS/ d" ${CONFIGSOURCE} &>/dev/null || true
# Write it in the config file
echo "${KEYS}=$(jq .$KEYS ${JSONSOURCE})" >>${CONFIGSOURCE}
# Say it loud
echo "... ${KEYS}=$(jq .$KEYS ${JSONSOURCE})"
done
# If it is a normal field
else
# Remove if already existing
sed -i "/$KEYS/ d" ${CONFIGSOURCE} &>/dev/null || true
# Write it in the config file
echo "${KEYS}=$(jq .$KEYS ${JSONSOURCE})" >>${CONFIGSOURCE}
# Say it loud
echo "... ${KEYS}=$(jq .$KEYS ${JSONSOURCE})"
fi fi
done done
echo " "
bashio::log.info "Starting the app" ###########################
echo " " # Read all config options #
###########################
# Replace all "" with ''
sed -i 's|"|'\''|g' $CONFIGSOURCE
# For all keys in config file
for word in $(cat $CONFIGSOURCE); do
[[ ! $word =~ ^.+[=].+$ ]] && bashio::log.fatal "$word does not follow the structure KEY=\'text\', it will be ignored" || true
export "$word" # Export the variable
echo "$word"
done
############## ##############
# Launch App # # Launch App #
############## ##############
echo " "
bashio::log.info "Starting the app"
echo " "
python -u /app/main.py || bashio::log.fatal "The app has crashed. Are you sure you entered the correct config options?" python -u /app/main.py || bashio::log.fatal "The app has crashed. Are you sure you entered the correct config options?"