HASelect class

class HASelect : public HABaseDeviceType

HASelect adds a dropdown with options in the Home Assistant panel.

Note

You can find more information about this entity in the Home Assistant documentation: https://www.home-assistant.io/integrations/button.mqtt/

Public Functions

HASelect(const char *uniqueId)
Parameters

uniqueId – The unique ID of the select. It needs to be unique in a scope of your device.

~HASelect()
void setOptions(const char *options)

Sets the list of available options that will be listed in the dropdown. The input string should contain options separated using semicolons. For example: `setOptions(“Option A;Option B;Option C”);

Note

The options list can be set only once.

Parameters

options – The list of options that are separated by semicolons.

bool setState(const int8_t state, const bool force = false)

Changes state of the select and publishes MQTT message. State represents the index of the option that was set using the setOptions method. Please note that if a new value is the same as previous one, the MQTT message won’t be published.

Parameters
  • state – New state of the select. You can set -1 to reset the select.

  • force – Forces to update state without comparing it to previous known state.

Returns

Returns true if MQTT message has been published successfully.

const char *getCurrentOption() const

Returns the selected option based on the most recent state of the select. You can utilize this method to get the string representation of the option (e.g. for printing). If no option is selected, null is returned.

inline void setCurrentState(const int8_t state)

Sets the current state of the select without publishing it to Home Assistant. State represents the index of the option that was set using the setOptions method. This method may be useful if you want to change the state before the connection with the MQTT broker is acquired.

Parameters

state – The new state of the cover.

inline int8_t getCurrentState() const

Returns last known state of the select. State represents the index of the option that was set using the setOptions method. By default the state is set to -1.

inline void setIcon(const char *icon)

Sets icon of the select. Any icon from MaterialDesignIcons.com (for example: mdi:home).

Parameters

icon – The icon name.

inline void setRetain(const bool retain)

Sets retain flag for the select’s command. If set to true the command produced by Home Assistant will be retained.

Parameters

retain

inline void setOptimistic(const bool optimistic)

Sets optimistic flag for the select state. In this mode the select state doesn’t need to be reported back to the HA panel when a command is received. By default the optimistic mode is disabled.

Parameters

optimistic – The optimistic mode (true - enabled, false - disabled).

inline void onCommand(void (*callback)(int8_t index, HASelect *sender))

Registers callback that will be called each time the option is changed from the HA panel. Please note that it’s not possible to register multiple callbacks for the same select.

Note

In non-optimistic mode, the selected option must be reported back to HA using the HASelect::setState method.

Parameters

callback

Protected Functions

virtual void buildSerializer() override
virtual void onMqttConnected() override
virtual void onMqttMessage(const char *topic, const uint8_t *payload, const uint16_t length) override

Private Functions

bool publishState(const int8_t state)

Publishes the MQTT message with the given state.

Parameters

state – The state to publish.

Returns

Returns true if the MQTT message has been published successfully.

uint8_t countOptionsInString(const char *options) const

Counts the amount of options in the given string.

Private Members

HASerializerArray *_options

Array of options for the serializer.

int8_t _currentState

Stores the current state (the current option’s index). By default it’s -1.

const char *_icon

The icon of the select. It can be nullptr.

bool _retain

The retain flag for the HA commands.

bool _optimistic

The optimistic mode of the select (true - enabled, false - disabled).

void (*_commandCallback)(int8_t index, HASelect *sender)

The command callback that will be called when option is changed via the HA panel.