HADevice class

class HADevice

This class represents your device that’s going to be registered in the Home Assistant devices registry. Each entity (HABinarySensor, HASensor, etc.) that you use will be owned by this device.

Public Functions

HADevice()

Constructs HADevice without the unique ID.

Note

You will need to set the ID using HADevice::setUniqueId method. Otherwise none of the entities will work.

HADevice(const char *uniqueId)

Constructs HADevice with the given unique ID (string). Keep the unique ID short to save the memory.

Parameters

uniqueId – String with the null terminator.

HADevice(const byte *uniqueId, const uint16_t length)

Constructs HADevice using the given byte array as the unique ID. It works in the same way as HADevice::setUniqueId method.

Parameters
  • uniqueId – Bytes array that’s going to be converted into the string.

  • length – Number of bytes in the array.

~HADevice()

Deletes HASerializer and the availability topic if the shared availability was enabled.

inline const char *getUniqueId() const

Returns pointer to the unique ID. It can be nullptr if the device has no ID assigned.

inline const HASerializer *getSerializer() const

Returns the instance of the HASerializer used by the device. This method is used by all entities to serialize device’s representation.

inline bool isSharedAvailabilityEnabled() const

Returns true if the shared availability is enabled for the device.

inline bool isExtendedUniqueIdsEnabled() const

Returns true if the extended unique IDs feature is enabled for the device.

inline const char *getAvailabilityTopic() const

Returns availability topic generated by the HADevice::enableSharedAvailability method. It can be nullptr if the shared availability is not enabled.

inline bool isAvailable() const

Returns online/offline state of the device.

inline void enableExtendedUniqueIds()

Enables the use of extended unique IDs for all registered device types. The unique ID of each device type will be prefixed with the device’s ID once enabled.

bool setUniqueId(const byte *uniqueId, const uint16_t length)

Sets unique ID of the device based on the given byte array. Each byte is converted into a hex string representation, so the final length of the unique ID will be twice as given.

Note

The unique ID can be set only once (via constructor or using this method).

Parameters
  • uniqueId – Bytes array that’s going to be converted into the string.

  • length – Number of bytes in the array.

void setManufacturer(const char *manufacturer)

Sets the “manufacturer” property that’s going to be displayed in the Home Assistant.

Parameters

manufacturer – Any string. Keep it short to save the memory.

void setModel(const char *model)

Sets the “model” property that’s going to be displayed in the Home Assistant.

Parameters

model – Any string. Keep it short to save the memory.

void setName(const char *name)

Sets the “name” property that’s going to be displayed in the Home Assistant.

Parameters

name – Any string. Keep it short to save the memory.

void setSoftwareVersion(const char *softwareVersion)

Sets the “software version” property that’s going to be displayed in the Home Assistant.

Parameters

softwareVersion – Any string. Keep it short to save the memory.

void setConfigurationUrl(const char *url)

Sets the “configuration URL” property that’s going to be used by the Home Assistant.

Parameters

url – Configuration URL to publish.

void setAvailability(bool online)

Sets device’s availability and publishes MQTT message on the availability topic. If the device is not connected to an MQTT broker or the shared availability is not enabled then nothing happens.

Parameters

online – Set to true if the device should be displayed as available in the HA panel.

bool enableSharedAvailability()

Enables the shared availability feature.

void enableLastWill()

Enables MQTT LWT feature. Please note that the shared availability needs to be enabled first.

void publishAvailability() const

Publishes current availability of the device on the availability topic. If the device is not connected to an MQTT broker or the shared availability is not enabled then nothing happens. This method is called by the HAMqtt when the connection to an MQTT broker is acquired.

Private Members

const char *_uniqueId

The unique ID of the device. It can be a memory allocated by HADevice::setUniqueId method.

bool _ownsUniqueId

Specifies whether HADevice class owns the _uniqueId pointer.

HASerializer *_serializer

JSON serializer of the HADevice class. It’s allocated in the constructor.

char *_availabilityTopic

The availability topic allocated by HADevice::enableSharedAvailability method.

bool _sharedAvailability

Specifies whether the shared availability is enabled.

bool _available

Specifies whether the device is available (online / offline).

bool _extendedUniqueIds

Specifies whether extended unique IDs feature is enabled.