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
518 stars 120 forks source link

Compilation error: call of overloaded 'setState(int)' is ambiguous #250

Open mmmmm009 opened 5 months ago

mmmmm009 commented 5 months ago

Hi,

Arduino IDE 2.3.2 aha lib 2.1.0

Not sure if I've changed something in my code or updated some libraries ... Yesterday my code was ok.

I combined few examples into one code and I have a problem with numer.setState function:

#include <WiFi.h>
#include <ArduinoHA.h>

#define BROKER_ADDR IPAddress(192,168,1,200)
#define BROKER_USERNAME     "xxxx" // replace with your credentials
#define BROKER_PASSWORD     "xxxx"

#define WIFI_SSID         "xxxx"
#define WIFI_PASSWORD     "xxxx"

byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};

unsigned long lastAvailabilityToggleAt = millis();

WiFiClient client;
HADevice device;

HAMqtt mqtt(client, device);
HANumber number("myNumber");
HALock lock("myLock");
HACover cover("myCover", HACover::PositionFeature);

unsigned long previousMillis = 0;  // will store last time LED was updated
const long interval = 1000;  // interval at which to blink (milliseconds)

int i=0;

void setup() {
      byte mac[6];
     WiFi.macAddress(mac);
     device.setUniqueId(mac, sizeof(mac));

    Serial.begin(115200);
    while (!Serial) {
    ;  
    }
    Serial.println("Serial Started...");

    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");

    device.setName("Arduino");
    device.setSoftwareVersion("1.0.0");

    number.onCommand(onNumberCommand);
    number.setIcon("mdi:window-shutter");
    number.setName("My number");

    lock.onCommand(onLockCommand);
    lock.setName("Window Lock"); // optional
    lock.setIcon("mdi:lock"); // optional

    lastAvailabilityToggleAt = millis();
    device.enableSharedAvailability();
    device.enableLastWill();

    mqtt.begin(BROKER_ADDR, BROKER_USERNAME, BROKER_PASSWORD);
}

void loop() {
    mqtt.loop();

  unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
          previousMillis = currentMillis;

          Serial.print("Position: ");
          Serial.println(i);
          number.setState(i);
          cover.setPosition(i);

          if (i==101){
            cover.setState(HACover::StateOpen);
          }

          if (i==0){
            cover.setState(HACover::StateClosed);
          }

          if ((i>0) && (i<100)){
            cover.setState(HACover::StateOpening);
          }

          i++;

          if (i==102){
          i=0;
          }
    }

   //if ((millis() - lastAvailabilityToggleAt) > 5000) {
   //     device.setAvailability(!device.isAvailable());
   //     lastAvailabilityToggleAt = millis();
   // }

}

void onCoverCommand(HACover::CoverCommand cmd, HACover* sender) {
    if (cmd == HACover::CommandOpen) {
        Serial.println("Command: Open");
        sender->setState(HACover::StateOpening); // report state back to the HA
    } else if (cmd == HACover::CommandClose) {
        Serial.println("Command: Close");
        sender->setState(HACover::StateClosing); // report state back to the HA
    } else if (cmd == HACover::CommandStop) {
        Serial.println("Command: Stop");
        sender->setState(HACover::StateStopped); // report state back to the HA
    }

    // Available states:
    // HACover::StateClosed
    // HACover::StateClosing
    // HACover::StateOpen
    // HACover::StateOpening
    // HACover::StateStopped

    // You can also report position using setPosition() method
}

void onLockCommand(HALock::LockCommand cmd, HALock* sender) {
    if (cmd == HALock::CommandLock) {
        Serial.println("Command: Lock");
        sender->setState(HALock::StateLocked); // report state back to the HA
    } else if (cmd == HALock::CommandUnlock) {
        Serial.println("Command: Unlock");
        sender->setState(HALock::StateUnlocked); // report state back to the HA
    } else if (cmd == HALock::CommandOpen) {
        if (sender->getCurrentState() != HALock::StateUnlocked) {
            return; // optionally you can verify if the lock is unlocked before opening
        }

        Serial.println("Command: Open");
    }
}

void onNumberCommand(HANumeric number, HANumber* sender)
{
    if (!number.isSet()) {
        // the reset command was send by Home Assistant
    } else {
        // you can do whatever you want with the number as follows:
        int8_t numberInt8 = number.toInt8();
        int16_t numberInt16 = number.toInt16();
        int32_t numberInt32 = number.toInt32();
        uint8_t numberUInt8 = number.toUInt8();
        uint16_t numberUInt16 = number.toUInt16();
        uint32_t numberUInt32 = number.toUInt32();
        float numberFloat = number.toFloat();
    }

    sender->setState(number); // report the selected option back to the HA panel
    //Serial.print("Slider: ");
    //Serial.println(number.toInt16());
}

Compilation error: call of overloaded 'setState(int)' is ambiguous

I would really appreaciate any help. THX!