HADeviceTrigger class

class HADeviceTrigger : public HABaseDeviceType

HADeviceTrigger allows to a custom trigger that can be used in the Home Assistant automation. For example, it can be a wall switch that produces press and long_press actions.

Note

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

Public Types

enum TriggerType

Built-in types of the trigger.

Values:

enumerator ButtonShortPressType
enumerator ButtonShortReleaseType
enumerator ButtonLongPressType
enumerator ButtonLongReleaseType
enumerator ButtonDoublePressType
enumerator ButtonTriplePressType
enumerator ButtonQuadruplePressType
enumerator ButtonQuintuplePressType
enum TriggerSubtype

Built-in subtypes of the trigger.

Values:

enumerator TurnOnSubtype
enumerator TurnOffSubtype
enumerator Button1Subtype
enumerator Button2Subtype
enumerator Button3Subtype
enumerator Button4Subtype
enumerator Button5Subtype
enumerator Button6Subtype

Public Functions

HADeviceTrigger(const char *type, const char *subtype)

Creates the device trigger with a custom type and subtype. For example, it can be click as the type and btn0 as the subtype. Please note that combination of the type and subtype needs to be unique in a scope of your device.

Parameters
  • type – String representation of the type.

  • subtype – String representation of the subtype.

HADeviceTrigger(TriggerType type, const char *subtype)

Creates the device trigger with a built-in type and a custom subtype. For example, it can be HADeviceTrigger::ButtonShortPressType as the type and btn0 as the subtype. Please note that combination of the type and subtype needs to be unique in a scope of your device.

Parameters
  • type – Built-in type of the trigger.

  • subtype – String representation of the subtype.

HADeviceTrigger(const char *type, TriggerSubtype subtype)

Creates the device trigger with a custom type and a built-in subtype. For example, it can be click as the type and HADeviceTrigger::Button1Subtype as the subtype. Please note that combination of the type and subtype needs to be unique in a scope of your device.

Parameters
  • type – String representation of the subtype.

  • subtype – Built-in subtype of the trigger.

HADeviceTrigger(TriggerType type, TriggerSubtype subtype)

Creates the device trigger with a built-in type and built-in subtype. For example, it can be HADeviceTrigger::ButtonShortPressType as the type and HADeviceTrigger::Button1Subtype as the subtype. Please note that combination of the type and subtype needs to be unique in a scope of your device.

Parameters
  • type – Built-in type of the trigger.

  • subtype – Built-in subtype of the trigger.

~HADeviceTrigger()

Frees memory allocated by the class.

bool trigger()

Publishes MQTT message with the trigger event. The published message is not retained.

Returns

Returns true if MQTT message has been published successfully.

inline const char *getType() const

Returns the type of the trigger. If the built-in type is used the returned value points to the flash memory. Use HADeviceTrigger::isProgmemType to verify if the returned value is the progmem pointer.

Returns

Pointer to the type.

inline bool isProgmemType() const

Returns true if the built-in type was assigned to the trigger.

inline const char *getSubtype() const

Returns the subtype of the trigger. If the built-in subtype is used the returned value points to the flash memory. Use HADeviceTrigger::isProgmemSubtype to verify if the returned value is the progmem pointer.

Returns

Pointer to the subtype.

inline bool isProgmemSubtype() const

Returns true if the built-in subtype was assigned to the trigger.

Protected Functions

virtual void buildSerializer() override
virtual void onMqttConnected() override

Private Functions

uint16_t calculateIdSize() const

Calculates desired size of the unique ID based on the type and subtype that were passed to the constructor.

void buildUniqueId()

Builds the unique ID of the device’s type based on the type and subtype that were passed to the constructor.

const char *determineProgmemType(TriggerType type) const

Returns pointer to the flash memory that represents the given type.

Parameters

subtype – Built-in type enum’s value.

Returns

Pointer to the flash memory if the given type is supported. For the unsupported type the nullptr is returned.

const char *determineProgmemSubtype(TriggerSubtype subtype) const

Returns pointer to the flash memory that represents the given subtype.

Parameters

subtype – Built-in subtype enum’s value.

Returns

Pointer to the flash memory if the given subtype is supported. For the unsupported subtype the nullptr is returned.

Private Members

const char *_type

Pointer to the trigger’s type. It can be pointer to the flash memory.

const char *_subtype

Pointer to the trigger’s subtype. It can be pointer to the flash memory.

bool _isProgmemType

Specifies whether the type points to the flash memory.

bool _isProgmemSubtype

Specifies whether the subtype points to the flash memory.