HALock class

class HALock : public HABaseDeviceType

HALock allows to implement a custom lock (for example: door lock) that can be controlled from the Home Assistant panel.

Note

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

Public Types

enum LockState

Available states of the lock that can be reported to the HA panel.

Values:

enumerator StateUnknown
enumerator StateLocked
enumerator StateUnlocked
enum LockCommand

Commands that will be produced by the HA panel.

Values:

enumerator CommandLock
enumerator CommandUnlock
enumerator CommandOpen

Public Functions

HALock(const char *uniqueId)
Parameters

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

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

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

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

Returns

Returns true if MQTT message has been published successfully.

inline void setCurrentState(const LockState state)

Sets current state of the lock 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 lock.

inline LockState getCurrentState() const

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

inline void setIcon(const char *icon)

Sets icon of the lock. 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 lock’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 lock state. In this mode the lock 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)(LockCommand command, HALock *sender))

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

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 LockState 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 lock’s callback with proper enum’s property.

Parameters
  • cmd – The data of the command.

  • length – Length of the command.

Private Members

const char *_icon

The icon of the lock. It can be nullptr.

bool _retain

The retain flag for the HA commands.

bool _optimistic

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

LockState _currentState

The current state of the lock. By default it’s HALock::StateUnknown.

void (*_commandCallback)(LockCommand command, HALock *sender)

The callback that will be called when lock/unlock/open command is received from the HA.