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

Refactoring configuration and add env variables #16

Merged
merged 3 commits into from
May 13, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
id: info
uses: home-assistant/actions/helpers/info@master
with:
path: "./"
path: "daikin-mqtt"

- name: Check if add-on should be built
id: check
Expand Down Expand Up @@ -68,6 +68,6 @@ jobs:
args: |
${{ env.BUILD_ARGS }} \
--${{ matrix.arch }} \
--target /data \
--target daikin-mqtt \
--image "${{ steps.check.outputs.image }}" \
--docker-hub "ghcr.io/${{ github.repository_owner }}"
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: 🚀 Run Home Assistant Add-on Lint
uses: frenck/[email protected]
with:
path: "./"
path: "daikin-mqtt"



10 changes: 0 additions & 10 deletions Dockerfile

This file was deleted.

16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
I developed this integration to get my AC to work again with Home Assistant after Daikin upgraded the firmware to 2.8.0 and the native integration no longer works.
This is fairly basic at the moment but I'm hoping it fills the gap while a more polished solution is available

# Installation Home Assistant (simple)
[![Open your Home Assistant instance and show the add add-on repository dialog with a specific repository URL pre-filled.](https://my.home-assistant.io/badges/supervisor_add_addon_repository.svg)](https://my.home-assistant.io/redirect/supervisor_add_addon_repository/?repository_url=https%3A%2F%2Fgithub.com%2Fchrisp250%2Fdaikin_mqtt)
1. Add reposotory to Home Assistant (clink on the badge above)
2. Go to Settings -> Add-on Store -> ADD-ON Store -> Daikin MQTT -> Install
3. Change configuration in `/config/daikin-mqtt/configuration.yaml`
4. Start the add-on


# Requirements
These python packages are required for the code to work:
`paho-mqtt`
`requests`
# Installation Home Assistant (manual)
1. Go to Settings -> Add-on Store -> 3 dots -> Repositories -> Add repository -> Enter `https://github.com/chrisp250/daikin_mqtt` -> Add
2. Go to Settings -> Add-on Store -> ADD-ON Store -> Daikin MQTT -> Install
3. Change configuration in `/config/daikin-mqtt/configuration.yaml`
4. Start the add-on

# Installation
Installation steps are very simple:
Expand All @@ -26,6 +31,7 @@ Run the image:
- `docker run -d -v $(pwd)/daikin.ini:/usr/src/app/daikin.ini daikin_mqtt`

# Example Daikin MQTT config
You can find configuration in the `/config/daikin-mqtt/configuration.yaml`
```
general:
mqtt:
Expand Down
54 changes: 0 additions & 54 deletions config.json

This file was deleted.

14 changes: 14 additions & 0 deletions daikin-mqtt/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ARG BUILD_FROM
FROM $BUILD_FROM

WORKDIR /app
COPY app/requirements.txt .

RUN pip3 install --no-cache-dir --prefer-binary -r requirements.txt

COPY app/ .

COPY docker/run.sh /app/run.sh
RUN chmod a+x /app/run.sh

CMD [ "/app/run.sh" ]
85 changes: 85 additions & 0 deletions daikin-mqtt/app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import yaml
import os
import sys
import json

# Configuration file reader
# Env variables can be used to override the config file settings
# Example:
# export DAIKIN_MQTT_CONFIG_GENERAL=""
# export DAIKIN_MQTT_CONFIG_MQTT_SERVER="localhost"
# export DAIKIN_MQTT_CONFIG_MQTT_PORT=1883
# export DAIKIN_MQTT_CONFIG_MQTT_USERNAME="mqtt_username"
# export DAIKIN_MQTT_CONFIG_MQTT_PASSWORD="mqtt_password"
# export DAIKIN_MQTT_CONFIG_UNITS='[{"name": "unit_name", "address": "unit_address"}]'

class Config:
def __init__(self):
if "CONFIG_PATH" in os.environ:
self.configPath = os.environ['CONFIG_PATH']
else:
self.configPath = "daikin.yaml"

def getConfig(self):
valid_json = True
valid_yaml = True

with open(self.configPath) as f:
config = yaml.load(f, Loader=yaml.FullLoader)

if "DAIKIN_MQTT_CONFIG_GENERAL" in os.environ:
general = os.environ["DAIKIN_MQTT_CONFIG_GENERAL"]
else:
general = config['general']

if "DAIKIN_MQTT_CONFIG_MQTT_SERVER" in os.environ:
mqtt_server = os.environ["DAIKIN_MQTT_CONFIG_MQTT_SERVER"]
else:
mqtt_server = config['mqtt']['server']

if "DAIKIN_MQTT_CONFIG_MQTT_PORT" in os.environ:
mqtt_port = int(os.environ["DAIKIN_MQTT_CONFIG_MQTT_PORT"])
else:
mqtt_port = config['mqtt']['port']

if "DAIKIN_MQTT_CONFIG_MQTT_USERNAME" in os.environ:
mqtt_username = os.environ["DAIKIN_MQTT_CONFIG_MQTT_USERNAME"]
else:
mqtt_username = config['mqtt']['username']

if "DAIKIN_MQTT_CONFIG_MQTT_PASSWORD" in os.environ:
mqtt_password = os.environ["DAIKIN_MQTT_CONFIG_MQTT_PASSWORD"]
else:
mqtt_password = config['mqtt']['password']

if "DAIKIN_MQTT_CONFIG_UNITS" in os.environ:
try:
units = json.loads(os.environ["DAIKIN_MQTT_CONFIG_UNITS"])
except:
valid_json = False

try:
units = yaml.load(os.environ["DAIKIN_MQTT_CONFIG_UNITS"])
except:
valid_yaml = False

if not valid_json and not valid_yaml:
units = config['units']
else:
units = config['units']


if units[0]["name"] is None or units[0]["address"] is None:
print("Unit name or address is not set in config file")
sys.exit(1)

return {
"general": general,
"mqtt" : {
"server": mqtt_server,
"port": mqtt_port,
"username": mqtt_username,
"password": mqtt_password
},
"units": units
}
File renamed without changes.
File renamed without changes.
50 changes: 19 additions & 31 deletions main.py → daikin-mqtt/app/main.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
import time
import re

import paho.mqtt.client as mqtt
import yaml
import daikin
import config
import os
import sys

# Check CONFIG_PATH environment variable
if 'CONFIG_PATH' in os.environ:
print("CONFIG_PATH environment variable is set to:", os.environ['CONFIG_PATH'])
configPath = os.environ['CONFIG_PATH']
else:
print("CONFIG_PATH environment variable is not set. Using default config...")
configPath = "daikin.yaml"

print("Starting Daikin MQTT with config:", configPath)
print("Starting Daikin MQTT")

#Configuration file reader
with open(configPath) as f:
config = yaml.load(f, Loader=yaml.FullLoader)

mqtt_server=config['mqtt']['server']
mqtt_port=int(config['mqtt']['port'])
mqtt_username = config['mqtt']['username']
mqtt_password = config['mqtt']['password']
units = config['units']

if units[0]["name"] is None or units[0]["address"] is None:
print("Unit name or address is not set in config file")
sys.exit(1)
config = config.Config().getConfig()

#Define Daikin class
#aircon = daikin.Daikin(ac_address)
print("Loaded configuration:", config)

#Publish succeeded
def on_publish(client, userdata, mid, reason_code, properties):
# print("sent a message")
# print("sent a message")
exit

# Subscribed to a new topic
Expand All @@ -49,16 +34,15 @@ def on_subscribe(client, userdata, mid, reason_code_list, properties):
exit

#Message received
def on_message(client, userdata, message, topic):

def on_message(client, userdata, message):
#print("Topic:", message.topic," Payload:",message.payload)
if message.topic == (topic+'/modecommand'): #Is it a mode change message
if "modecommand" in message.topic:
print ("Set mode:",str(message.payload.decode("utf-8")))
match str(message.payload.decode("utf-8")):
case "cool":
aircon.switch_mode("cool")
case "dry":
aircon.switch_mode("dry")
aircon.switch_mode("dry")
case "heat":
aircon.switch_mode("heat")
case "fan_only":
Expand All @@ -67,7 +51,7 @@ def on_message(client, userdata, message, topic):
aircon.switch_mode("auto")
case "off":
aircon.switch_mode("off")
elif message.topic == (topic+'/temperaturecommand'): #Is it a temperature change message
elif "temperaturecommand" in message.topic: #Is it a temperature change message
print ("Set temperature:",str(message.payload.decode("utf-8")))
aircon.set_temp(message.payload.decode("utf-8"))
# update_request()
Expand Down Expand Up @@ -123,13 +107,17 @@ def update_request(aircon, topic):
mqttClient.on_message = on_message
mqttClient.on_subscribe = on_subscribe

if mqtt_username is not None and mqtt_password is not None:
mqttClient.username_pw_set(mqtt_username, mqtt_password)
if config["mqtt"]["username"] is not None and config["mqtt"]["password"] is not None:
mqttClient.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"])

try:
mqttClient.connect(config["mqtt"]["server"], config["mqtt"]["port"])
except TimeoutError:
print("Connection to MQTT broker timed out")

mqttClient.connect(mqtt_server, mqtt_port)

# Subscribe to topics and Daikin client
for unit in units:
for unit in config["units"]:
mqttClient.subscribe("climate" + "/" + unit["name"] + "/" + "modecommand")
mqttClient.subscribe("climate" + "/" + unit["name"] + "/" +'/temperaturecommand')

Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions daikin-mqtt/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Daikin MQTT",
"version": "0.5.0",
"slug": "daikin_mqtt",
"description": "MQTT Server for Daikin Air Conditioners",
"uart": true,
"url": "https://github.com/chrisp250/daikin_mqtt/tree/master/daikin_mqtt",
"services": [
"mqtt:need"
],
"arch": [
"aarch64",
"amd64",
"armhf",
"armv7",
"i386"
],
"init": false,
"timeout": 30,
"map": [
"homeassistant_config:rw"
],
"image": "ghcr.io/chrisp250/daikin-mqtt-{arch}"
}
25 changes: 25 additions & 0 deletions daikin-mqtt/docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bashio

export DAIKIN_MQTT_DATA="/homeassistant/daikin-mqtt"

if ! bashio::fs.file_exists "$DAIKIN_MQTT_DATA/configuration.yaml"; then
mkdir -p "$DAIKIN_MQTT_DATA" || bashio::exit.nok "Could not create $DAIKIN_MQTT_DATA"

cat <<EOF > "$DAIKIN_MQTT_DATA/configuration.yaml"
general:
mqtt:
server:
port: 1883
username:
password:
units:
- name:
address:
EOF
fi

export CONFIG_PATH="$DAIKIN_MQTT_DATA/configuration.yaml"

bashio::log.info "Starting Daikin MQTT..."

exec python3 /app/main.py
Binary file added daikin-mqtt/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added daikin-mqtt/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 0 additions & 15 deletions rootfs/etc/services.d/daikin-mqtt/finish

This file was deleted.

Loading
Loading