This commit is contained in:
Alexandre
2021-11-17 10:38:33 +01:00
parent cbb1aa0d1f
commit d12bb8ec80
26 changed files with 99 additions and 542 deletions

View File

@@ -1,4 +1,4 @@
ARG BUILD_UPSTREAM="0.6.0"
ARG BUILD_UPSTREAM="0.7.1-dev"
FROM m4dm4rtig4n/enedisgateway2mqtt:$BUILD_UPSTREAM
# Base system
@@ -7,6 +7,7 @@ ARG BASHIO_VERSION=0.13.1
# Copy root filesystem
COPY rootfs /
VOLUME [ "/config" ]
ENV TZ=Europe/Paris
RUN \
################

View File

@@ -2,53 +2,16 @@
"arch": ["aarch64", "amd64", "armv7", "armhf"],
"description": "use Enedis Gateway API to send data in your MQTT Broker",
"map": ["config:rw"],
"name": "Enedisgateway2mqtt",
"name": "Enedisgateway2mqtt_test",
"options": {
"ACCESS_TOKEN": "xxx",
"PDL": "xxx",
"MQTT_HOST": "xxx.xxx.xxx.xxx",
"DEBUG": true
"CONFIG_LOCATION": "/config/enedisgateway2mqtt/config.yaml"
},
"schema": {
"ACCESS_TOKEN": "str?",
"PDL": "str?",
"MQTT_HOST": "str?",
"custom_var": "str?",
"test": "bool?",
"MQTT_PORT": "int?",
"MQTT_PREFIX": "str?",
"MQTT_CLIENT_ID": "str?",
"MQTT_USERNAME": "str?",
"MQTT_PASSWORD": "str?",
"RETAIN": "bool?",
"QOS": "int?",
"GET_CONSUMPTION": "bool?",
"GET_CONSUMPTION_DETAIL": "bool?",
"GET_PRODUCTION": "bool?",
"GET_PRODUCTION_DETAIL": "bool?",
"HA_AUTODISCOVERY": "bool?",
"HA_AUTODISCOVERY_PREFIX": "str?",
"OFFPEAK_HOURS": "str?",
"CONSUMPTION_PRICE_BASE": "str?",
"CONSUMPTION_PRICE_HC": "str?",
"CONSUMPTION_PRICE_HP": "str?",
"CYCLE": "int?",
"ADDRESSES": "bool?",
"REFRESH_CONTRACT": "bool?",
"REFRESH_ADDRESSES": "bool?",
"WIPE_CACHE": "bool?",
"DEBUG": "bool?",
"CARD_MYENEDIS": "bool?",
"CURRENT_PLAN": "str?",
"INFLUXDB_ENABLE": "bool?",
"INFLUXDB_HOST": "str?",
"INFLUXDB_PORT": "int?",
"INFLUXDB_TOKEN": "str?",
"INFLUXDB_ORG": "str?",
"INFLUXDB_BUCKET": "str?"
"CONFIG_LOCATION": "str",
"TZ": "str?"
},
"slug": "enedisgateway2mqtt",
"upstream": "0.6.0",
"upstream": "0.7.1",
"url": "https://github.com/alexbelgium/hassio-addons",
"version": "0.6.0-12"
"version": "0.7.1"
}

View File

@@ -0,0 +1,15 @@
#!/bin/bash
###################################
# Export all addon options as env #
###################################
# For all keys in options.json
JSONSOURCE="/data/options.json"
# Export keys as env variables
mapfile -t arr < <(jq -r 'keys[]' ${JSONSOURCE})
for KEYS in ${arr[@]}; do
# export key
export $(echo "${KEYS}=$(jq .$KEYS ${JSONSOURCE})")
done

View File

@@ -1,144 +1,37 @@
#!/usr/bin/env bashio
############################
# Check if config is there #
############################
if bashio::config.true "test"; then
# Where is the config
CONFIGSOURCE="/config/enedisgateway2mqtt/config.yaml"
# Check if config file is there, or create one from template
if [ -f $CONFIGSOURCE ]; then
echo "Using config file found in $CONFIGSOURCE"
else
echo "No config file, creating one from template"
# Create folder
mkdir -p "$(dirname "${CONFIGSOURCE}")"
# Downloading template
TEMPLATESOURCE="https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/enedisgateway2mqtt/rootfs/templates/config.yaml"
curl -L -f -s $TEMPLATESOURCE --output $CONFIGSOURCE
# Placing template in config
#cp config.yaml "$(dirname "${CONFIGSOURCE}")"
# Need to restart
bashio::log.fatal "Config file not found, creating a new one. Please customize the file in $CONFIGSOURCE before restarting."
bashio::exit.nok
fi
# Check if yaml is valid
yamllint -d relaxed --no-warnings $CONFIGSOURCE &> ERROR
if [ $? = 0 ]; then
echo "Config file is a valid yaml"
else
bashio::log.fatal "Config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list :"
cat ERROR
fi
# Create symlink
[ -f /data/config.yaml ] && rm /data/config.yaml
ln -s $CONFIGSOURCE /data
echo "Symlink created"
# Export all yaml entries as env variables
# Helper function
function parse_yaml {
local prefix=$2 || local prefix=""
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
# Get variables and export
bashio::log.info "Starting the app with the variables in /config/enedisgateway2mqtt"
for word in $(parse_yaml $CONFIGSOURCE conf); do
# Data validation
if [[ $word =~ ^.+[=].+$ ]]; then
export $word # Export the variable
bashio::log.blue "$word"
else
bashio::log.fatal "$word does not follow the structure KEY=text, it will be ignored and removed from the config"
sed -i "/$word/ d" ${CONFIGSOURCE}
fi
done
# Where is the config
CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION")
# Check if config file is there, or create one from template
if [ -f $CONFIGSOURCE ]; then
echo "Using config file found in $CONFIGSOURCE"
else
echo "No config file, creating one from template"
# Create folder
mkdir -p "$(dirname "${CONFIGSOURCE}")"
# Placing template in config
cp /data/config.yaml "$(dirname "${CONFIGSOURCE}")" &>/dev/null \
|| cp /templates/config.yaml "$(dirname "${CONFIGSOURCE}")"
# Need to restart
bashio::log.fatal "Config file not found, creating a new one. Please customize the file in $CONFIGSOURCE before restarting."
bashio::exit.nok
fi
#################
# Create config #
#################
# Check if yaml is valid
yamllint -d relaxed --no-warnings $CONFIGSOURCE &> ERROR || EXIT_CODE=$?
if [ $EXIT_CODE = 0 ]; then
echo "Config file is a valid yaml"
else
cat ERROR
bashio::log.fatal "Config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above."
bashio::exit.nok
fi
# Create the config file
CONFIGSOURCE="/config/enedisgateway2mqtt/enedisgateway2mqtt.conf" #file
mkdir -p "$(dirname "${CONFIGSOURCE}")" #create dir
touch ${CONFIGSOURCE} #create 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
JSONSOURCE="/data/options.json"
mapfile -t arr < <(jq -r 'keys[]' ${JSONSOURCE})
# For all keys in options.json
for KEYS in ${arr[@]}; do
# if the custom_var field is used
if [ "${KEYS}" = "custom_var" ]; then
VALUES=$(jq .$KEYS ${JSONSOURCE}) # Get list of custom elements
VALUES=${VALUES:1:-1} # Remove first and last ""
for SUBKEYS in ${VALUES//,/ }; do
[[ ! $SUBKEYS =~ ^.+[=].+$ ]] && bashio::log.warning "Your custom_var field $SUBKEYS does not follow the structure KEY=\"text\",KEY2=\"text2\" it will be ignored" && continue || true
# Remove the key if already existing
sed -i "/$(echo "${SUBKEYS%%=*}")/ d" ${CONFIGSOURCE}
# Remove apostrophes
SUBKEYS=${SUBKEYS//[\"\']/}
# Write it in the config file
echo ${SUBKEYS} >>${CONFIGSOURCE}
# Say it loud
# echo "... ${SUBKEYS}"
done
# If it is a normal field
else
# Remove if already existing
sed -i "/$KEYS/ d" ${CONFIGSOURCE}
# Store key
KEYS=$(echo "${KEYS}=$(jq .$KEYS ${JSONSOURCE})")
# Remove apostrophes
KEYS=${KEYS//[\"\']/}
# Write it in the config file
echo $KEYS >>${CONFIGSOURCE}
# Say it loud
# echo "... ${KEYS}=$(jq .$KEYS ${JSONSOURCE})"
fi
done
###########################
# Read all config options #
###########################
bashio::log.info "Starting the app with the variables in /config/enedisgateway2mqtt"
# For all keys in config file
for word in $(cat $CONFIGSOURCE); do
# Data validation
if [[ $word =~ ^.+[=].+$ ]]; then
export $word # Export the variable
bashio::log.blue "$word"
else
bashio::log.fatal "$word does not follow the structure KEY=text, it will be ignored and removed from the config"
sed -i "/$word/ d" ${CONFIGSOURCE}
fi
done
# Create symlink
[ -f /data/config.yaml ] && rm /data/config.yaml
ln -s $CONFIGSOURCE /data
echo "Symlink created"
##############
# Launch App #

View File

@@ -1,11 +1,63 @@
---
doe: "a deer, a female deer"
ray: "a drop of golden sun"
pi: 3.14159
xmas: true
french-hens: 3
calling-birds:
- huey
- dewey
- louie
- fred
##########
# GLOBAL #
##########
debug: false
####################
## MQTT ##
####################
mqtt:
host: MOSQUITO_SERVER # MANDATORY
port: 1883
username: ""
password: ""
prefix: enedis_gateway
client_id: enedis_gateway
retain: true
qos: 0
####################
## Home assistant ##
####################
home_assistant:
discovery: false
discovery_prefix: homeassistant
card_myenedis: false
###############
## Influx DB ##
###############
#influxdb:
# host: MY_INFLUXDB_SERVER
# port: 8086
# token: MY_TOKEN
# org: MY_ORG
# bucket: MY_BUCKET
####################
## ENEDIS GATEWAY ##
####################
enedis_gateway:
PDL_1: # MANDATORY
token: PDL_1_TOKEN # MANDATORY
plan: BASE # BASE or HP/HC
consumption: true
consumption_detail: true
consumption_price_hc: 0
consumption_price_hp: 0
consumption_price_base: 0
production: false
production_detail: false
# offpeak_hours: "" # USE ONLY IF YOU WANT OVERLOAD DEFAULT VALUE, Format : 22h36-00h10;01h00-06h00
addresses: true
# PDL_2:
# token: PDL_2_TOKEN
# plan: HP/HC
# consumption: true
# consumption_detail: true
# consumption_price_hc: 0.1781
# consumption_price_hp: 0.1337
# consumption_price_base: 0.1781
# production: false
# production_detail: false
# addresses: true