wmbusmeters/ha-addon/run.sh

71 wiersze
2.7 KiB
Bash
Czysty Zwykły widok Historia

#!/usr/bin/with-contenv bashio
CONFIG_PATH=/data/options.json
CONFIG_DATA_PATH=$(bashio::config 'data_path')
2022-12-21 22:17:37 +00:00
CONFIG_CONF=$(bashio::jq "${CONFIG_PATH}" '.conf')
CONFIG_METERS=$(bashio::jq "${CONFIG_PATH}" '.meters')
2023-02-03 13:58:20 +00:00
bashio::log.info "CONFIG_CONF ..."
bashio::log.info "${CONFIG_CONF}"
bashio::log.info "CONFIG_METERS ..."
bashio::log.info "${CONFIG_METERS}"
2022-12-21 22:06:56 +00:00
bashio::log.info "Syncing wmbusmeters configuration ..."
2022-12-21 22:29:19 +00:00
if ! bashio::fs.directory_exists "${CONFIG_DATA_PATH}/logs/meter_readings"; then
mkdir -p "${CONFIG_DATA_PATH}/logs/meter_readings"
fi
if ! bashio::fs.directory_exists "${CONFIG_DATA_PATH}/etc/wmbusmeters.d"; then
mkdir -p "${CONFIG_DATA_PATH}/etc/wmbusmeters.d"
fi
2023-02-03 13:58:20 +00:00
echo -e "$CONFIG_CONF" | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' > $CONFIG_DATA_PATH/etc/wmbusmeters.conf
2022-12-21 22:06:56 +00:00
bashio::log.info "Registering meters ..."
rm -f $CONFIG_DATA_PATH/etc/wmbusmeters.d/*
meter_no=0
IFS=$'\n'
for meter in $(jq -c -M '.meters[]' $CONFIG_PATH)
do
meter_no=$(( meter_no+1 ))
METER_NAME=$(printf 'meter-%04d' "$(( meter_no ))")
2022-12-21 22:06:56 +00:00
bashio::log.info "Adding $METER_NAME ..."
METER_DATA=$(printf '%s\n' $meter | jq --raw-output -c -M '.')
2023-02-03 13:58:20 +00:00
echo -e "$METER_DATA" | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' > $CONFIG_DATA_PATH/etc/wmbusmeters.d/$METER_NAME
done
2022-12-21 22:06:56 +00:00
bashio::log.info "Generating MQTT configuration ... "
if bashio::config.exists "mqtt.host"
then
MQTT_HOST=$(bashio::config "mqtt.host")
if bashio::config.exists "mqtt.port"; then MQTT_PORT=$(bashio::config "mqtt.port"); fi
if bashio::config.exists "mqtt.user"; then MQTT_USER=$(bashio::config "mqtt.user"); fi
if bashio::config.exists "mqtt.password"; then MQTT_PASSWORD=$(bashio::config "mqtt.password"); fi
else
MQTT_HOST=$(bashio::services mqtt "host")
MQTT_PORT=$(bashio::services mqtt "port")
MQTT_USER=$(bashio::services mqtt "username")
MQTT_PASSWORD=$(bashio::services mqtt "password")
fi
2022-12-21 22:06:56 +00:00
bashio::log.info "Broker $MQTT_HOST will be used."
pub_args=('-h' $MQTT_HOST )
pub_args_quoted=('-h' \'$MQTT_HOST\' )
[[ ! -z ${MQTT_PORT+x} ]] && pub_args+=( '-p' $MQTT_PORT ) && pub_args_quoted+=( '-p' \'$MQTT_PORT\' )
[[ ! -z ${MQTT_USER+x} ]] && pub_args+=( '-u' $MQTT_USER ) && pub_args_quoted+=( '-u' \'$MQTT_USER\' )
[[ ! -z ${MQTT_PASSWORD+x} ]] && pub_args+=( '-P' $MQTT_PASSWORD ) && pub_args_quoted+=( '-P' \'$MQTT_PASSWORD\' )
cat > /wmbusmeters/mosquitto_pub.sh << EOL
#!/usr/bin/with-contenv bashio
TOPIC=\$1
MESSAGE=\$2
/usr/bin/mosquitto_pub ${pub_args_quoted[@]} -r -t "\$TOPIC" -m "\$MESSAGE"
EOL
chmod a+x /wmbusmeters/mosquitto_pub.sh
2022-07-26 21:29:45 +00:00
# Running MQTT discovery
2022-07-26 21:59:01 +00:00
/mqtt_discovery.sh ${pub_args[@]} -c $CONFIG_PATH -w $CONFIG_DATA_PATH || true
2022-07-26 21:29:45 +00:00
2022-12-21 22:06:56 +00:00
bashio::log.info "Running wmbusmeters ..."
/wmbusmeters/wmbusmeters --useconfig=$CONFIG_DATA_PATH