HABaseDeviceType class

class HABaseDeviceType

Subclassed by HABinarySensor, HAButton, HACamera, HACover, HADeviceTracker, HADeviceTrigger, HAFan, HAHVAC, HALight, HALock, HANumber, HAScene, HASelect, HASensor, HASwitch, HATagScanner

Public Types

enum NumberPrecision

Values:

enumerator PrecisionP0

No digits after the decimal point.

enumerator PrecisionP1

One digit after the decimal point.

enumerator PrecisionP2

Two digits after the decimal point.

enumerator PrecisionP3

Three digits after the decimal point.

Public Functions

HABaseDeviceType(const __FlashStringHelper *componentName, const char *uniqueId)

Creates a new device type instance and registers it in the HAMqtt class.

Parameters
  • componentName – The name of the Home Assistant component (e.g. binary_sensor). You can find all available component names in the Home Assistant documentation. The component name needs to be stored in the flash memory.

  • uniqueId – The unique ID of the device type. It needs to be unique in a scope of the HADevice.

inline const char *uniqueId() const

Returns unique ID of the device type.

inline const __FlashStringHelper *componentName() const

Returns component name defined by the device type. It’s used for the MQTT discovery topic.

inline bool isAvailabilityConfigured() const

Returns true if the availability was configured for this device type.

inline bool isOnline() const

Returns online state of the device type.

inline void setName(const char *name)

Sets name of the device type that will be used as a label in the HA panel. Keep the name short to save the resources.

Parameters

name – The device type name.

inline const char *getName() const

Returns name of the deviced type that was assigned via setName method. It can be nullptr if there is no name assigned.

inline void setObjectId(const char *objectId)

Sets object ID that will be used by HA to generate entity ID. Keep the ID short to save the resources.

Parameters

objectId – The object ID.

inline const char *getObjectId() const

Returns the object ID that was set by setObjectId method. It can be nullptr if there is no ID assigned.

virtual void setAvailability(bool online)

Sets availability of the device type. Setting the initial availability enables availability reporting for this device type. Please note that not all device types support this feature. Follow HA documentation of a specific device type to get more information.

Parameters

online – Specifies whether the device type is online.

Protected Functions

inline virtual void buildSerializer()

This method should build serializer that will be used for publishing the configuration. The serializer is built each time the MQTT connection is acquired. Follow implementation of the existing device types to get better understanding of the logic.

virtual void onMqttConnected() = 0

This method is called each time the MQTT connection is acquired. Each device type should publish its configuration and availability. It can be also used for subscribing to MQTT topics.

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

This method is called each time the device receives a MQTT message. It can be any MQTT message so the method should always verify the topic.

Parameters
  • topic – The topic on which the message was produced.

  • payload – The payload of the message. It can be nullptr.

  • length – The length of the payload.

void destroySerializer()

Destroys the existing serializer.

void publishConfig()

Publishes configuration of this device type on the HA discovery topic.

void publishAvailability()

Publishes current availability of the device type. The message is only produced if the availability is configured for this device type.

bool publishOnDataTopic(const __FlashStringHelper *topic, const __FlashStringHelper *payload, bool retained = false)

Publishes the given flash string on the data topic.

Parameters
  • topic – The topic to publish on (progmem string).

  • payload – The message’s payload (progmem string).

  • retained – Specifies whether the message should be retained.

bool publishOnDataTopic(const __FlashStringHelper *topic, const char *payload, bool retained = false)

Publishes the given string on the data topic.

Parameters
  • topic – The topic to publish on (progmem string).

  • payload – The message’s payload.

  • retained – Specifies whether the message should be retained.

bool publishOnDataTopic(const __FlashStringHelper *topic, const uint8_t *payload, const uint16_t length, bool retained = false, bool isProgmemData = false)

Publishes the given data on the data topic.

Parameters
  • topic – The topic to publish on (progmem string).

  • payload – The message’s payload.

  • length – The length of the payload.

  • retained – Specifies whether the message should be retained.

  • isProgmemData – Specifies whether the given data is stored in the flash memory.

Protected Attributes

const __FlashStringHelper *const _componentName

The component name that was assigned via the constructor.

const char *_uniqueId

The unique ID that was assigned via the constructor.

const char *_name

The name that was set using setName method. It can be nullptr.

const char *_objectId

The object ID that was set using setObjectId method. It can be nullptr.

HASerializer *_serializer

HASerializer that belongs to this device type. It can be nullptr.

Protected Static Functions

static HAMqtt *mqtt()

Returns instance of the HAMqtt class.

static void subscribeTopic(const char *uniqueId, const __FlashStringHelper *topic)

Subscribes to the given data topic.

Parameters
  • uniqueId – THe unique ID of the device type assigned via the constructor.

  • topic – Topic to subscribe (progmem string).

Private Types

enum Availability

Values:

enumerator AvailabilityDefault
enumerator AvailabilityOnline
enumerator AvailabilityOffline

Private Members

Availability _availability

The current availability of this device type. AvailabilityDefault means that the initial availability was never set.

Friends

friend class HAMqtt