dawidchyrzynski / arduino-home-assistant

ArduinoHA allows to integrate an Arduino/ESP based device with Home Assistant using MQTT.
https://dawidchyrzynski.github.io/arduino-home-assistant/
GNU Affero General Public License v3.0
490 stars 118 forks source link

Not discovered if used two connection types #259

Open proasnet opened 3 months ago

proasnet commented 3 months ago

Dear @dawidchyrzynski, very thanks for your library.

In my project with ESP32 I am using two connection types ( Wifi or Ethernet W5500 ) selectable via UI, so only one connection in time, not simultaneously

WiFiClient wificlient;
EthernetClient ethernetclient;

HADevice device;
HAMqtt wifi_mqtt( wificlient , device , 40 );
HAMqtt ethernet_mqtt( ethernetclient , device , 40 );

If I comment "//" one of them, discovery working. Else not. Is any way for use both constructors and working discovery by HA? Thank you

proasnet commented 2 months ago

I am trying another methods... Method 1:

WiFiClient wificlient;
EthernetClient ethernetclient;

HADevice device;
HAMqtt *mqtt;

void setup() {
  if ( ethernet_connected ) {
    mqtt = new HAMqtt( ethernetclient , device , 40 );
    debug.println( "eth MQTT" );
  }

  if ( wifi_connected ) {
    mqtt = new HAMqtt( wificlient , device , 40 );
    debug.println( "wifi MQTT" );
  }
}

Method 2:

Client *client;

HADevice device;
HAMqtt *mqtt;

void setup() {
  if ( ethernet_connected ) {
    client = new EthernetClient;
    debug.println( "eth MQTT" );
  }

  if ( wifi_connected ) {
    client = new WifiClient;
    debug.println( "wifi MQTT" );
  }
  mqtt = new HAMqtt( client , device , 40 );
}

Compile is work fine, but not discovered too.