Merge pull request #14 from Meatballs1/mqtt_config

Add a configuration file for MQTT settings etc
This commit is contained in:
pmagyar
2024-03-19 19:30:04 +01:00
committed by GitHub
5 changed files with 37 additions and 10 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ __*
*.json
*.zip
*.sh
*.ini

View File

@@ -13,4 +13,4 @@ RUN apt-get update && \
COPY hc2mqtt hc-login HCDevice.py HCSocket.py HCxml2json.py ./
ENTRYPOINT ["python3"]
CMD ["hc2mqtt", "/config/config.json"]
CMD ["hc2mqtt", "--config", "/config/config.ini"]

View File

@@ -41,13 +41,14 @@ Installing `sslpsk` needs some extra steps:
![laptop in a clothes washer with a display DoorState:Closed](images/doorclose.jpg)
```bash
hc-login $USERNAME $PASSWORD > config/config.json
hc-login $USERNAME $PASSWORD > config/devices.json
```
or
```bash
docker-compose run -T app hc-login $USERNAME $PASSWORD > config/config.json
docker-compose build
docker-compose run -T app hc-login $USERNAME $PASSWORD > config/devices.json
```
The `hc-login` script perfoms the OAuth process to login to your
@@ -64,8 +65,30 @@ your mDNS or DNS server resolves the names correctly.
## Home Connect to MQTT
Use the following config/config.ini example:
```
devices_file = "/config/devices.json"
mqtt_host = "localhost"
mqtt_username = "mqtt"
mqtt_password = "password"
mqtt_port = 1883
mqtt_prefix = "homeconnect/"
mqtt_ssl = False
mqtt_cafile = None
mqtt_certfile = None
mqtt_keyfile = None
mqtt_clientname="hcpy"
```
```bash
hc2mqtt config.json
hc2mqtt --config config/config.ini
```
or
```bash
docker-compose up
```
This tool will establish websockets to the local devices and

14
hc2mqtt
View File

@@ -8,13 +8,14 @@ import ssl
from threading import Thread
import click
import click_config_file
import paho.mqtt.client as mqtt
from HCDevice import HCDevice
from HCSocket import HCSocket, now
@click.command()
@click.argument("config_file")
@click.option("-d", "--devices_file", default="config/devices.json")
@click.option("-h", "--mqtt_host", default="localhost")
@click.option("-p", "--mqtt_prefix", default="homeconnect/")
@click.option("--mqtt_port", default=1883, type=int)
@@ -25,13 +26,14 @@ from HCSocket import HCSocket, now
@click.option("--mqtt_certfile")
@click.option("--mqtt_keyfile")
@click.option("--mqtt_clientname", default="hcpy")
@click_config_file.configuration_option()
def hc2mqtt(config_file: str, mqtt_host: str, mqtt_prefix: str, mqtt_port: int, mqtt_username: str,
def hc2mqtt(devices_file: str, mqtt_host: str, mqtt_prefix: str, mqtt_port: int, mqtt_username: str,
mqtt_password: str, mqtt_ssl: bool, mqtt_cafile: str, mqtt_certfile: str, mqtt_keyfile: str, mqtt_clientname: str):
click.echo(f"Hello {config_file=} {mqtt_host=} {mqtt_prefix=} {mqtt_port=} {mqtt_username=} {mqtt_password=} "
click.echo(f"Hello {devices_file=} {mqtt_host=} {mqtt_prefix=} {mqtt_port=} {mqtt_username=} {mqtt_password=} "
f"{mqtt_ssl=} {mqtt_cafile=} {mqtt_certfile=} {mqtt_keyfile=} {mqtt_clientname=}")
with open(config_file, "r") as f:
with open(devices_file, "r") as f:
devices = json.load(f)
client = mqtt.Client(mqtt_clientname)
@@ -121,8 +123,8 @@ def client_connect(client, device, mqtt_topic):
# new_topic = topics[topic]
# if new_topic == "remaining":
# state["remainingseconds"] = value
# value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60)
# state["remainingseconds"] = value
# value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60)
new_topic = device_topics[topic]
state[new_topic] = value

View File

@@ -6,4 +6,5 @@ sslpsk
paho.mqtt==1.6.1
lxml
click
click-config-file
requests