mirror of
https://github.com/alexhopeoconnor/arduino-home-assistant.git
synced 2026-10-04 02:48:13 +10:00
HASelect improvements (#221)
* added getCurrentOption method to the HASelect * final fixes, tests * added support for publishing None state in the HASelect * update select.ino * update docs
This commit is contained in:
@@ -8,7 +8,7 @@ byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
|
||||
EthernetClient client;
|
||||
HADevice device(mac, sizeof(mac));
|
||||
HAMqtt mqtt(client, device);
|
||||
HASelect select("mySelect");
|
||||
HASelect mySelect("mySelect");
|
||||
|
||||
void onSelectCommand(int8_t index, HASelect* sender)
|
||||
{
|
||||
@@ -31,6 +31,12 @@ void onSelectCommand(int8_t index, HASelect* sender)
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
@@ -42,18 +48,18 @@ void setup() {
|
||||
device.setSoftwareVersion("1.0.0");
|
||||
|
||||
// set available options
|
||||
select.setOptions("Low;Medium;High"); // use semicolons as separator of options
|
||||
select.onCommand(onSelectCommand);
|
||||
mySelect.setOptions("Low;Medium;High"); // use semicolons as separator of options
|
||||
mySelect.onCommand(onSelectCommand);
|
||||
|
||||
select.setIcon("mdi:home"); // optional
|
||||
select.setName("My dropdown"); // optional
|
||||
mySelect.setIcon("mdi:home"); // optional
|
||||
mySelect.setName("My dropdown"); // optional
|
||||
|
||||
// Optionally you can set retain flag for the HA commands
|
||||
// select.setRetain(true);
|
||||
// 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.
|
||||
// select.setOptimistic(true);
|
||||
// mySelect.setOptimistic(true);
|
||||
|
||||
mqtt.begin(BROKER_ADDR);
|
||||
}
|
||||
@@ -64,5 +70,8 @@ void loop() {
|
||||
|
||||
// You can also report the state to the HA panel at runtime as shown below.
|
||||
// The integer corresponds to the option's index.
|
||||
// select.setState(1);
|
||||
// mySelect.setState(1);
|
||||
|
||||
// You can also reset the select as follows:
|
||||
// mySelect.setState(-1);
|
||||
}
|
||||
Reference in New Issue
Block a user