igor-podpalchenko / ha-devi-mqtt

Home Assistant Danfoss DeviReg MQTT service
4 stars 1 forks source link

Mqtt auth failure during runtime #2

Closed matthiasschardt closed 1 month ago

matthiasschardt commented 1 month ago

Hi, first of all thank you very much for creating this awesome project!

I was able to follow your documentation to create my own devi_config.json with release 1.0.5.
After creating the mqtt_config.json I expected the connection to my mqtt server to be established.

However, when I tried to run it, I got an unauthorized exceptions when trying to connect to the mqtt server.

Steps to reproduce

  1. Create the devi_config.json and mqtt_config.json as described in the README.md
  2. Use a mqtt server with anonymous access disabled
  3. Run the service

    docker run -it --rm -v $(pwd)/devi_config.json:/app/config/devi_config.json -v $(pwd)/mqtt_config.json:/app/config/mqtt_config.json -v $(pwd)/auto-discovery-templates:/app/config/auto-discovery-templates  podpalch/ha-devi-mqtt

Error log

20:56:29.865 [main] INFO  i.h.devi.mqtt.service.ConsoleRunner - Starting exporter service
Not authorized to connect (5)
        at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1053)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151)
        at java.base/java.lang.Thread.run(Unknown Source)

Possible solution

I have tracked this down to the client.connect() call in MQTTService.java.

The previously created MqttConnectOptions, where password and username are being set, are not passed in.

After adding the options as an instance variable and passing it in, it starts to work for me and I have had the setup up and running for a day now without any issues.

I'll create a PR with the fix that you can merge if you'd like