HACover class

class HACover : public HABaseDeviceType

HACover allows to control a cover (such as blinds, a roller shutter or a garage door).

Note

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

Public Types

enum CoverState

Values:

enumerator StateUnknown
enumerator StateClosed
enumerator StateClosing
enumerator StateOpen
enumerator StateOpening
enumerator StateStopped
enum CoverCommand

Values:

enumerator CommandOpen
enumerator CommandClose
enumerator CommandStop
enum Features

Values:

enumerator DefaultFeatures
enumerator PositionFeature

Public Functions

HACover(const char *uniqueId, const Features features = DefaultFeatures)
Parameters
  • uniqueId – The unique ID of the cover. It needs to be unique in a scope of your device.

  • features – Features that should be enabled for the fan.

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

Changes state of the cover 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 cover.

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

Returns

Returns true if MQTT message has been published successfully.

bool setPosition(const int16_t position, const bool force = false)

Changes the position of the cover 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
  • position – The new position of the cover (0-100).

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

Returns

Returns true if MQTT message has been published successfully.

inline void setCurrentState(const CoverState state)

Sets the current state of the cover without publishing it to Home Assistant. 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 CoverState getCurrentState() const

Returns last known state of the cover. By default the state is set to CoverState::StateUnknown

inline void setCurrentPosition(const int16_t position)

Sets the current position of the cover without pushing the value to Home Assistant. This method may be useful if you want to change the position before the connection with the MQTT broker is acquired.

Parameters

position – The new position of the cover (0-100).

inline int16_t getCurrentPosition() const

Returns the last known position of the cover. By default position is set to HACover::DefaultPosition

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/cover/

Parameters

deviceClass – The class name.

inline void setIcon(const char *icon)

Sets icon of the cover. 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 cover’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 cover state. In this mode the cover 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)(CoverCommand cmd, HACover *sender))

Registers callback that will be called each time the command from HA is received. Please note that it’s not possible to register multiple callbacks for the same cover.

Parameters

callback

Public Static Attributes

static const int16_t DefaultPosition = -32768

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 CoverState 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.

bool publishPosition(const int16_t position)

Publishes the MQTT message with the given position.

Parameters

position – The position 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 cover’s callback with proper enum’s property.

Parameters
  • cmd – The data of the command.

  • length – Length of the command.

Private Members

const uint8_t _features

Features enabled for the cover.

CoverState _currentState

The current state of the cover. By default it’s HACover::StateUnknown.

int16_t _currentPosition

The current position of the cover. By default it’s HACover::DefaultPosition.

const char *_class

The device class. It can be nullptr.

const char *_icon

The icon of the button. It can be nullptr.

bool _retain

The retain flag for the HA commands.

bool _optimistic

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

void (*_commandCallback)(CoverCommand cmd, HACover *sender)

The command callback that will be called when clicking the cover’s button in the HA panel.