HANumber class

class HANumber : public HABaseDeviceType

HANumber adds a slider or a box in the Home Assistant panel that controls the numeric value stored on your device.

Note

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

Public Types

enum Mode

Represents mode of the number.

Values:

enumerator ModeAuto
enumerator ModeBox
enumerator ModeSlider

Public Functions

HANumber(const char *uniqueId, const NumberPrecision precision = PrecisionP0)

Creates instance of the HANumber entity with the given numbers precision. The given precision applies to the state, min, max and step values.

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

  • precision – Precision of the floating point number that will be displayed in the HA panel.

bool setState(const HANumeric &state, const bool force = false)

Changes state of the number and publishes MQTT message. 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 number.

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

Returns

Returns true if MQTT message has been published successfully.

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setState(const int16_t state, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setState(const int32_t state, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setState(const uint8_t state, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setState(const uint16_t state, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setState(const uint32_t state, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline bool setState(const float state, const bool force = false)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const HANumeric &state)

Sets current state of the number without publishing it to Home Assistant. This method may be useful if you want to change state before connection with MQTT broker is acquired.

Parameters

state – New state of the number.

inline void setCurrentState(const int8_t state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const int16_t state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const int32_t state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const uint8_t state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const uint16_t state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const uint32_t state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline void setCurrentState(const float state)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline const HANumeric &getCurrentState() const

Returns last known state of the number. If setState method wasn’t called the initial value will be returned.

inline void setDeviceClass(const char *deviceClass)

Sets class of the device. You can find list of available values here: https://www.home-assistant.io/integrations/number/#device-class

Parameters

deviceClass – The class name.

inline void setIcon(const char *icon)

Sets icon of the number. 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 number’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 number state. In this mode the number 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 setMode(const Mode mode)

Sets mode of the number. It controls how the number should be displayed in the UI. By default it’s HANumber::ModeAuto.

Parameters

mode – Mode to set.

inline void setUnitOfMeasurement(const char *unitOfMeasurement)

Defines the units of measurement of the number, if any.

Parameters

units – For example: °C, %

inline void setMin(const float min)

Sets the minimum value that can be set from the Home Assistant panel.

Parameters

min – The minimal value. By default the value is not set.

inline void setMax(const float max)

Sets the maximum value that can be set from the Home Assistant panel.

Parameters

min – The maximum value. By default the value is not set.

inline void setStep(const float step)

Sets step of the slider’s movement in the Home Assistant panel.

Parameters

step – The step value. Smallest value 0.001. By default the value is not set.

inline void onCommand(void (*callback)(HANumeric number, HANumber *sender))

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

Note

In non-optimistic mode, the number must be reported back to HA using the HANumber::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 HANumeric &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.

void handleCommand(const uint8_t *cmd, const uint16_t length)

Parses the given command and executes the number’s callback with proper value.

Parameters
  • cmd – The data of the command.

  • length – Length of the command.

const __FlashStringHelper *getModeProperty() const

Returns progmem string representing mode of the number

const __FlashStringHelper *getCommandTemplate()

Returns progmem string representing value template for the command.

Private Members

const NumberPrecision _precision

The precision of the number. By default it’s HANumber::PrecisionP0.

const char *_class

The device class. It can be nullptr.

const char *_icon

The icon of the number. It can be nullptr.

bool _retain

The retain flag for the HA commands.

bool _optimistic

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

Mode _mode

Controls how the number should be displayed in the UI. By default it’s HANumber::ModeAuto.

const char *_unitOfMeasurement

The unit of measurement for the sensor. It can be nullptr.

HANumeric _minValue

The minimal value that can be set from the HA panel.

HANumeric _maxValue

The maximum value that can be set from the HA panel.

HANumeric _step

The step of the slider’s movement.

HANumeric _currentState

The current state of the number. By default the value is not set.

void (*_commandCallback)(HANumeric number, HANumber *sender)

The callback that will be called when the command is received from the HA.