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

HASelect improvements #221

Merged
merged 5 commits into from
Feb 5, 2024
Merged

HASelect improvements #221

merged 5 commits into from
Feb 5, 2024

Conversation

dawidchyrzynski
Copy link
Owner

@dawidchyrzynski dawidchyrzynski commented Feb 5, 2024

#include <Ethernet.h>
#include <ArduinoHA.h>

#define BROKER_ADDR IPAddress(192,168,0,17)

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

EthernetClient client;
HADevice device(mac, sizeof(mac));
HAMqtt mqtt(client, device);
HASelect mySelect("mySelect");

void onSelectCommand(int8_t index, HASelect* sender)
{
    switch (index) {
    case 0:
        // Option "Low" was selected
        break;

    case 1:
        // Option "Medium" was selected
        break;

    case 2:
        // Option "High" was selected
        break;

    default:
        // unknown option
        return;
    }

    sender->setState(index); // report the selected option back to the HA panel

    // it may return null
    if (sender->getCurrentOption()) {
        Serial.print("Current option: ");
        Serial.println(sender->getCurrentOption()); // <------------ NEW FEATURE
    }
}

void setup() {
    // you don't need to verify return status
    Ethernet.begin(mac);

    // set device's details (optional)
    device.setName("Arduino");
    device.setSoftwareVersion("1.0.0");

    // set available options
    mySelect.setOptions("Low;Medium;High"); // use semicolons as separator of options
    mySelect.onCommand(onSelectCommand);

    mySelect.setIcon("mdi:home"); // optional
    mySelect.setName("My dropdown"); // optional

    // Optionally you can set retain flag for the HA commands
    // mySelect.setRetain(true);

    // Optionally you can enable optimistic mode for the HASelect.
    // In this mode you won't need to report state back to the HA when commands are executed.
    // mySelect.setOptimistic(true);

    mqtt.begin(BROKER_ADDR);
}

void loop() {
    Ethernet.maintain();
    mqtt.loop();

    // You can also report the state to the HA panel at runtime as shown below.
    // The integer corresponds to the option's index.
    // mySelect.setState(1);

    // You can also reset the select as follows:
    // mySelect.setState(-1); // <------------ NEW FEATURE
}

@dawidchyrzynski dawidchyrzynski merged commit 6cf4f11 into develop Feb 5, 2024
@dawidchyrzynski dawidchyrzynski deleted the fix/select branch February 5, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant