mirror of
https://github.com/alexhopeoconnor/arduino-home-assistant.git
synced 2026-10-21 18:22:38 +10:00
Add support for extended unique IDs (#217)
* implement unique ID serialization as flag * finished extended unique IDs feature * add tests * update docs * add funding.yml
This commit is contained in:
@@ -21,7 +21,7 @@ copyright = '2022, Dawid Chyrzynski'
|
||||
author = 'Dawid Chyrzynski'
|
||||
|
||||
# The full version, including alpha/beta/rc tags
|
||||
release = '2.0.0'
|
||||
release = '2.1.0'
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
Device configuration
|
||||
====================
|
||||
|
||||
:doc:`HADevice </documents/api/core/ha-device>` represents the physical device where the library is installed.
|
||||
Logically it's a group of types like sensors, switches, lights and so on.
|
||||
In the Home Assistant, it's listed with properties that may be configured using the library's API.
|
||||
:doc:`HADevice </documents/api/core/ha-device>` represents the physical device on which the library is used.
|
||||
Essentially, it's a group of types such as sensors, switches, lights, and more.
|
||||
Within Home Assistant, it appears with properties that can be configured using the library's API.
|
||||
|
||||
Each property except the unique ID is optional.
|
||||
Setting optional properties increases flash and RAM usage so it's not recommended to set them on lower-spec MCUs.
|
||||
Every property, except for the unique ID, is optional.
|
||||
Enabling optional properties can lead to increased flash and RAM usage, therefore it is not advisable to set them on lower-spec MCUs.
|
||||
|
||||
The supported properties are:
|
||||
|
||||
@@ -19,11 +19,12 @@ The supported properties are:
|
||||
Unique ID
|
||||
---------
|
||||
|
||||
The ID of a device needs to be unique in a scope of a Home Assistant instance.
|
||||
The safest solution is to use the MAC address of an Ethernet or Wi-Fi chip but you can also implement your own solution.
|
||||
The unique ID serves as an internal identifier for devices within Home Assistant.
|
||||
With this ID, Home Assistant can monitor the device's parameters and the entities it exposes.
|
||||
The unique ID must be distinct within the scope of the Home Assistant instance.
|
||||
The recommended approach is to use the MAC address of an Ethernet or Wi-Fi chip.
|
||||
|
||||
There are three different ways to set the ID of the device.
|
||||
You can pick one depending on your needs.
|
||||
There are three distinct methods for setting the device ID, allowing you to choose the one that best suits your requirements.
|
||||
|
||||
1) Providing string (const char*) to the :doc:`HADevice </documents/api/core/ha-device>` constructor
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@@ -88,7 +89,7 @@ Device properties
|
||||
-----------------
|
||||
|
||||
Each property has its corresponding setter method in the :doc:`HADevice </documents/api/core/ha-device>` class.
|
||||
Please note that all these methods accept const char pointer whose **content is not copied**.
|
||||
Please note that all of these methods accept a const char pointer whose **content is not copied**.
|
||||
|
||||
::
|
||||
|
||||
|
||||
@@ -2,18 +2,121 @@
|
||||
Device types
|
||||
============
|
||||
|
||||
Device type represents a single entity in the Home Assistant panel.
|
||||
It can be a sensor, lock, camera or anything that's listed in the table below.
|
||||
Device type represents a single entity within the Home Assistant panel, which could be a sensor, lock, camera, or any other item listed in the table below.
|
||||
|
||||
Your physical device (for example ESP-01 board) can have multiple device types assigned.
|
||||
They will be displayed as child entities in the HA panel.
|
||||
Your physical device, such as an ESP-01 board, can have multiple device types assigned to it.
|
||||
These types will then appear as child entities in the Home Assistant panel.
|
||||
|
||||
Identifiers
|
||||
-----------
|
||||
|
||||
Home Assistant utilizes three distinct identifiers, which might initially appear confusing.
|
||||
Grasping the purpose of each is crucial for a clear understanding of the library's API.
|
||||
|
||||
Entity ID
|
||||
^^^^^^^^^
|
||||
|
||||
Home Assistant automatically generates an entity ID for each device type registered by your device.
|
||||
This ID is primarily utilized by dashboards and automations within Home Assistant.
|
||||
|
||||
When the entity is discovered by Home Assistant for the first time, the name field is automatically employed to generate the entity ID.
|
||||
Once registered, you can modify it to any desired value using the Home Assistant User Interface.
|
||||
|
||||
Home Assistant internally relies on the unique ID, so changing the entity ID or name in the Home Assistant UI does not break the integration between your device and Home Assistant.
|
||||
|
||||
Object ID
|
||||
^^^^^^^^^
|
||||
|
||||
The object ID is an optional identifier that you can assign to the device type.
|
||||
Its sole purpose is for generating the entity ID described above.
|
||||
|
||||
By default, Home Assistant generates the entity ID based on the entity's name.
|
||||
However, when the object ID is provided, Home Assistant uses it to generate the entity ID.
|
||||
|
||||
Consequently, you can use the entity's name as a user-friendly label and the object ID as an internal identifier.
|
||||
|
||||
Unique ID
|
||||
^^^^^^^^^
|
||||
|
||||
The unique ID serves as an internal identifier for the entity within the Home Assistant instance.
|
||||
Once the entity with a specific unique ID is created, it cannot be altered, as this identifier is not accessible through the user interface.
|
||||
|
||||
Home Assistant utilizes this identifier internally to store the parameters of the entity in the database.
|
||||
Given that the unique ID must be unique across the entire Home Assistant instance.
|
||||
Multiple devices cannot expose entities with the same unique ID.
|
||||
|
||||
By default, the library uses the unique ID provided in the device type's constructor.
|
||||
However, when you reuse the same codebase on multiple devices, conflicts may arise.
|
||||
To address this issue, you can enable the extended unique ID feature in the HADevice instance.
|
||||
This feature incorporates the device's unique ID as a prefix for the device type's ID.
|
||||
|
||||
.. code-block:: cpp
|
||||
:caption: Default behavior
|
||||
|
||||
#include <Ethernet.h>
|
||||
#include <ArduinoHA.h>
|
||||
|
||||
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
|
||||
|
||||
EthernetClient client;
|
||||
HADevice device(mac, sizeof(mac)); // the unique ID of the device will be 0010fa6e384a
|
||||
HAMqtt mqtt(client, device);
|
||||
|
||||
// "myValve" is unique ID of the sensor. You should define your own ID.
|
||||
HASensor valve("myValve");
|
||||
|
||||
void setup() {
|
||||
// ...
|
||||
|
||||
valve.setIcon("mdi:home");
|
||||
valve.setName("Water valve");
|
||||
|
||||
// the unique ID of the valve in HA will be "myValve"
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// ...
|
||||
}
|
||||
|
||||
.. code-block:: cpp
|
||||
:caption: Extended unique IDs
|
||||
|
||||
#include <Ethernet.h>
|
||||
#include <ArduinoHA.h>
|
||||
|
||||
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
|
||||
|
||||
EthernetClient client;
|
||||
HADevice device(mac, sizeof(mac)); // the unique ID of the device will be 0010fa6e384a
|
||||
HAMqtt mqtt(client, device);
|
||||
|
||||
// "myValve" is unique ID of the sensor. You should define your own ID.
|
||||
HASensor valve("myValve");
|
||||
|
||||
void setup() {
|
||||
// ...
|
||||
|
||||
device.enableExtendedUniqueIds(); // <------------ enables extended unique IDs
|
||||
valve.setIcon("mdi:home");
|
||||
valve.setName("Water valve");
|
||||
|
||||
// the unique ID of the valve in HA will be "0010fa6e384a_myValve"
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// ...
|
||||
}
|
||||
|
||||
Limitations
|
||||
-----------
|
||||
|
||||
Registering a new device type requires some flash and RAM memory to be utilized.
|
||||
On less powerful units like Arduino Uno, you may quickly hit the limit of resources, so keeping the device simple is recommended.
|
||||
Hitting the resource limit will result in random reboots of the device.
|
||||
Registering a new device type involves utilizing a certain amount of flash and RAM memory.
|
||||
On less powerful units, such as the Arduino Uno, you may rapidly reach the resource limit.
|
||||
Therefore, it is advisable to keep the device simple to avoid hitting the resource limit, which could lead to random reboots of the device.
|
||||
|
||||
By default, the maximum number of device types is 6.
|
||||
You can increase the limit using the :doc:`HAMqtt </documents/api/core/ha-mqtt>` class constructor as follows:
|
||||
|
||||
@@ -41,6 +41,10 @@ table.examples-table tbody td + td {
|
||||
.searchbox .caption-text {
|
||||
display: none
|
||||
}
|
||||
.code-block-caption {
|
||||
margin-top: 30px;
|
||||
font-weight: bold;
|
||||
}
|
||||
form {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
|
||||
Reference in New Issue
Block a user