include support for MQTT username/password

This commit is contained in:
David
2019-08-12 15:17:54 +10:00
parent b1399bab57
commit ebd3bbe2bc
5 changed files with 21 additions and 6 deletions
+4 -2
View File
@@ -51,7 +51,9 @@ cd /opt/ha-inverter-mqtt-agent
# Configure the 'device=' directive (in inverter.conf) to suit for RS232 or USB..  # Configure the 'device=' directive (in inverter.conf) to suit for RS232 or USB.. 
vi config/inverter.conf vi config/inverter.conf
# Configure your MQTT server host, port, Home Assistant topic, and name of the Inverter that you want displayed in Home Assistant. # Configure your MQTT server's IP/Host Name, Port, Credentials, HA topic, and name of the Inverter that you want displayed in Home Assistant...
# If your MQTT server does not need a username/password just leave these values empty.
vi config/mqtt.json vi config/mqtt.json
``` ```
@@ -62,7 +64,7 @@ docker-compose up -d
``` ```
_Note if you have issues standing up the image on your Linux distribution, you may need to manually build the image - This can be done by uncommenting the build flag in your docker-compose.yml file._ _**Note:** builds on docker hub are currently for x64 -- If you have issues standing up the image on your Linux distribution (i.e. Your Raspberry Pi/ARM device) you will need to manually build the image - This can be done by uncommenting the build flag in your docker-compose.yml file._
## Integrating into Home Assistant. ## Integrating into Home Assistant.
+3 -1
View File
@@ -2,5 +2,7 @@
"server": "10.16.10.4", "server": "10.16.10.4",
"port": "1883", "port": "1883",
"topic": "homeassistant", "topic": "homeassistant",
"devicename": "voltronic" "devicename": "voltronic",
"username": "",
"password": ""
} }
+7 -2
View File
@@ -6,11 +6,15 @@ MQTT_SERVER=`cat /etc/inverter/mqtt.json | jq '.server' -r`
MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r` MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r`
MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r` MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r`
MQTT_DEVICENAME=`cat /etc/inverter/mqtt.json | jq '.devicename' -r` MQTT_DEVICENAME=`cat /etc/inverter/mqtt.json | jq '.devicename' -r`
MQTT_USERNAME=`cat /etc/inverter/mqtt.json | jq '.username' -r`
MQTT_PASSWORD=`cat /etc/inverter/mqtt.json | jq '.password' -r`
registerTopic () { registerTopic () {
mosquitto_pub \ mosquitto_pub \
-h $MQTT_SERVER \ -h $MQTT_SERVER \
-p $MQTT_PORT \ -p $MQTT_PORT \
-u "$MQTT_USERNAME" \
-P "$MQTT_PASSWORD" \
-t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1/config" \ -t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1/config" \
-m "{ -m "{
\"name\": \""$MQTT_DEVICENAME"_$1\", \"name\": \""$MQTT_DEVICENAME"_$1\",
@@ -24,11 +28,12 @@ registerInverterRawCMD () {
mosquitto_pub \ mosquitto_pub \
-h $MQTT_SERVER \ -h $MQTT_SERVER \
-p $MQTT_PORT \ -p $MQTT_PORT \
-u "$MQTT_USERNAME" \
-P "$MQTT_PASSWORD" \
-t "$MQTT_TOPIC/sensor/$MQTT_DEVICENAME/config" \ -t "$MQTT_TOPIC/sensor/$MQTT_DEVICENAME/config" \
-m "{ -m "{
\"name\": \""$MQTT_DEVICENAME"\", \"name\": \""$MQTT_DEVICENAME"\",
\"state_topic\": \"$MQTT_TOPIC/sensor/$MQTT_DEVICENAME\", \"state_topic\": \"$MQTT_TOPIC/sensor/$MQTT_DEVICENAME\"
\"retain\": true
}" }"
} }
+4
View File
@@ -6,10 +6,14 @@ pushMQTTData () {
MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r` MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r`
MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r` MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r`
MQTT_DEVICENAME=`cat /etc/inverter/mqtt.json | jq '.devicename' -r` MQTT_DEVICENAME=`cat /etc/inverter/mqtt.json | jq '.devicename' -r`
MQTT_USERNAME=`cat /etc/inverter/mqtt.json | jq '.username' -r`
MQTT_PASSWORD=`cat /etc/inverter/mqtt.json | jq '.password' -r`
mosquitto_pub \ mosquitto_pub \
-h $MQTT_SERVER \ -h $MQTT_SERVER \
-p $MQTT_PORT \ -p $MQTT_PORT \
-u "$MQTT_USERNAME" \
-P "$MQTT_PASSWORD" \
-t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1" \ -t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1" \
-m "$2" -m "$2"
} }
+3 -1
View File
@@ -4,6 +4,8 @@ MQTT_SERVER=`cat /etc/inverter/mqtt.json | jq '.server' -r`
MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r` MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r`
MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r` MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r`
MQTT_DEVICENAME=`cat /etc/inverter/mqtt.json | jq '.devicename' -r` MQTT_DEVICENAME=`cat /etc/inverter/mqtt.json | jq '.devicename' -r`
MQTT_USERNAME=`cat /etc/inverter/mqtt.json | jq '.username' -r`
MQTT_PASSWORD=`cat /etc/inverter/mqtt.json | jq '.password' -r`
while read rawcmd; while read rawcmd;
do do
@@ -11,4 +13,4 @@ do
echo "Incoming request send: [$rawcmd] to inverter." echo "Incoming request send: [$rawcmd] to inverter."
/opt/inverter-cli/bin/inverter_poller -r $rawcmd; /opt/inverter-cli/bin/inverter_poller -r $rawcmd;
done < <(mosquitto_sub -h $MQTT_SERVER -p $MQTT_PORT -t "$MQTT_TOPIC/sensor/$MQTT_DEVICENAME" -q 1) done < <(mosquitto_sub -h $MQTT_SERVER -p $MQTT_PORT -u "$MQTT_USERNAME" -P "$MQTT_PASSWORD" -t "$MQTT_TOPIC/sensor/$MQTT_DEVICENAME" -q 1)