Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New version 1.2.3 #55

Merged
merged 4 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,42 @@ docker build \
docker run --rm -v "$PWD/data":/data gree-hvac-mqtt-bridge
```

### Multiple devices

As of 1.2.0 the Hassio addon supports multiple devices by running paralell NodeJS processes in PM2. Old configurations will work, but are deprecated.
### Run single device as a service

Deprecated config example:
To run it when the PC starts, a systemd service has to be created by following the following commands.

```json
"hvac_host": "192.168.0.255",
"mqtt": {
"broker_url": "mqtt://localhost",
"topic_prefix": "/my/topic/prefix",
}
```shell
sudo cp /opt/gree-hvac-mqtt-bridge/gree-bridge.service /etc/systemd/system/gree-bridge.service
sudo chmod +x /etc/systemd/system/gree-bridge.service
sudo systemctl enable gree-bridge
sudo systemctl start gree-bridge
```

Correct config example:
### Multiple devices

As of 1.2.0 the Hassio addon supports multiple devices by running paralell NodeJS processes in PM2. Old configurations will work, but will run without PM2.

config example:

```json
"mqtt": {
"broker_url": "mqtt://localhost",
},
"devices": [
{
"hvac_host": "192.168.0.255",
"mqtt_topic_prefix": "/home/hvac01"
},
{
"hvac_host": "192.168.0.254",
"mqtt_topic_prefix": "/home/hvac02"
}
]
{
"mqtt": {
"broker_url": "mqtt://localhost",
"username": "user",
"password": "pass",
"retain": false
},
"devices": [
{
"hvac_host": "192.168.0.255",
"mqtt_topic_prefix": "/home/hvac01"
},
{
"hvac_host": "192.168.0.254",
"mqtt_topic_prefix": "/home/hvac02"
}
]
}
```

## Configuring HVAC WiFi
Expand All @@ -170,6 +176,12 @@ Note: This command may vary depending on your OS (e.g. Linux, macOS, CygWin). If

## Changelog

[1.2.3]

- Fix run script for single device with same configuration
- Run single device as a systemd service
- Add option to MQTT for retain flag

[1.2.2]

- Fix incorrect state checks
Expand Down
8 changes: 5 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Gree HVAC MQTT bridge",
"version": "1.2.2",
"version": "1.2.3",
"slug": "gree_hvac_mqtt_bridge",
"description": "Hass.io addon for controlling Gree air conditioners using the MQTT climate platform",
"arch": [ "aarch64", "amd64", "armhf", "armv7", "i386" ],
Expand All @@ -12,7 +12,8 @@
"mqtt": {
"broker_url": "mqtt://localhost",
"username": "",
"password": ""
"password": "",
"retain": ""
},
"devices": [
{
Expand All @@ -27,7 +28,8 @@
"broker_url": "str",
"topic_prefix": "str?",
"username": "str?",
"password": "str?"
"password": "str?",
"retain": "str?"
},
"devices": [
{
Expand Down
13 changes: 13 additions & 0 deletions gree-bridge.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Gree HVAC MQTT Bridge
After=network.target

[Service]
User=root
WorkingDirectory=/opt/gree-hvac-mqtt-bridge
ExecStart=/opt/gree-hvac-mqtt-bridge/run.sh
Restart=always

[Install]
WantedBy=multi-user.target

8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ const deviceState = {
* @param {string} mqttTopic Topic (without prefix) to send with new value
*/
const publishIfChanged = function (stateProp, newValue, mqttTopic) {
const pubmqttOptions = {
retain: false
}
if (argv['mqtt-retain']) {
pubmqttOptions.retain = (argv['mqtt-retain'] == "true")
}
if (newValue !== deviceState[stateProp]) {
deviceState[stateProp] = newValue
client.publish(mqttTopicPrefix + mqttTopic, newValue)
client.publish(mqttTopicPrefix + mqttTopic, newValue, pubmqttOptions)
}
}

Expand Down
43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gree-hvac-mqtt-bridge",
"version": "1.2.2",
"version": "1.2.3",
"description": "MQTT Bridge for controlling Gree smart air conditioners",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 17 additions & 7 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#!/bin/sh
set -e

CONFIG_PATH=/data/options.json
CONFIG_PATH=data/options.json

HVAC_HOST=$(jq -r ".hvac_host" $CONFIG_PATH)
MQTT_BROKER_URL=$(jq -r ".mqtt.broker_url" $CONFIG_PATH)
MQTT_TOPIC_PREFIX=$(jq -r ".mqtt.topic_prefix" $CONFIG_PATH)
MQTT_USERNAME=$(jq -r ".mqtt.username" $CONFIG_PATH)
MQTT_PASSWORD=$(jq -r ".mqtt.password" $CONFIG_PATH)
MQTT_RETAIN=$(jq -r ".mqtt.retain" $CONFIG_PATH)
if [ "$MQTT_RETAIN" = null ]; then
MQTT_RETAIN=false
fi

echo "MQTT_RETAIN: ${MQTT_RETAIN}"
npm install

INSTANCES=$(jq '.devices | length' $CONFIG_PATH)

if [ "$INSTANCES" -gt 0 ]; then
if [ "$INSTANCES" -gt 1 ]; then
for i in $(seq 0 $(($INSTANCES - 1))); do
HVAC_HOST=$(jq -r ".devices[$i].hvac_host" $CONFIG_PATH);
MQTT_TOPIC_PREFIX=$(jq -r ".devices[$i].mqtt_topic_prefix" $CONFIG_PATH);
Expand All @@ -25,15 +30,20 @@ if [ "$INSTANCES" -gt 0 ]; then
--mqtt-broker-url="${MQTT_BROKER_URL}" \
--mqtt-topic-prefix="${MQTT_TOPIC_PREFIX}" \
--mqtt-username="${MQTT_USERNAME}" \
--mqtt-password="${MQTT_PASSWORD}"
--mqtt-password="${MQTT_PASSWORD}" \
--mqtt-retain="${MQTT_RETAIN}"
done
npx pm2 logs /HVAC_/
else
echo "Running in single-instance mode (DEPRECATED)"
node index.js \
HVAC_HOST=$(jq -r ".devices[0].hvac_host" $CONFIG_PATH);
MQTT_TOPIC_PREFIX=$(jq -r ".devices[0].mqtt_topic_prefix" $CONFIG_PATH);
echo "Running single instance for $HVAC_HOST"
#echo "${HVAC_HOST}, ${MQTT_BROKER_URL}, ${MQTT_TOPIC_PREFIX}, ${MQTT_USERNAME}, ${MQTT_PASSWORD}"
/usr/bin/node index.js \
--hvac-host="${HVAC_HOST}" \
--mqtt-broker-url="${MQTT_BROKER_URL}" \
--mqtt-topic-prefix="${MQTT_TOPIC_PREFIX}" \
--mqtt-username="${MQTT_USERNAME}" \
--mqtt-password="${MQTT_PASSWORD}"
fi
--mqtt-password="${MQTT_PASSWORD}" \
--mqtt-retain="${MQTT_RETAIN}"
fi