Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to add entity category to certain sensors #238

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/device-types/HABinarySensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ HABinarySensor::HABinarySensor(const char* uniqueId) :
HABaseDeviceType(AHATOFSTR(HAComponentBinarySensor), uniqueId),
_class(nullptr),
_icon(nullptr),
_currentState(false)
_currentState(false),
_entityCategory(nullptr)
{

}
Expand Down Expand Up @@ -42,11 +43,12 @@ void HABinarySensor::buildSerializer()
return;
}

_serializer = new HASerializer(this, 9); // 9 - max properties nb
_serializer = new HASerializer(this, 10); // 10 - max properties nb
_serializer->set(AHATOFSTR(HANameProperty), _name);
_serializer->set(AHATOFSTR(HAObjectIdProperty), _objectId);
_serializer->set(HASerializer::WithUniqueId);
_serializer->set(AHATOFSTR(HADeviceClassProperty), _class);
_serializer->set(AHATOFSTR(HAStateEntityCategory), _entityCategory);
_serializer->set(AHATOFSTR(HAIconProperty), _icon);

if (_expireAfter.isSet()) {
Expand Down
12 changes: 12 additions & 0 deletions src/device-types/HABinarySensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class HABinarySensor : public HABaseDeviceType
inline void setDeviceClass(const char* deviceClass)
{ _class = deviceClass; }

/**
* Sets the entity category for the sensor.
* See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
*
* @param entityCategory The category name.
*/
inline void setEntityCategory(const char* entityCategory)
{ _entityCategory = entityCategory; }

/**
* Sets icon of the sensor.
* Any icon from MaterialDesignIcons.com (for example: `mdi:home`).
Expand All @@ -88,6 +97,9 @@ class HABinarySensor : public HABaseDeviceType
/// The device class. It can be nullptr.
const char* _class;

/// The entity category for the sensor. It can be nullptr. See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
const char* _entityCategory;

/// The icon of the sensor. It can be nullptr.
const char* _icon;

Expand Down
4 changes: 3 additions & 1 deletion src/device-types/HAButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
HAButton::HAButton(const char* uniqueId) :
HABaseDeviceType(AHATOFSTR(HAComponentButton), uniqueId),
_class(nullptr),
_entityCategory(nullptr),
_icon(nullptr),
_retain(false),
_commandCallback(nullptr)
Expand All @@ -20,11 +21,12 @@ void HAButton::buildSerializer()
return;
}

_serializer = new HASerializer(this, 9); // 9 - max properties nb
_serializer = new HASerializer(this, 10); // 10 - max properties nb
_serializer->set(AHATOFSTR(HANameProperty), _name);
_serializer->set(AHATOFSTR(HAObjectIdProperty), _objectId);
_serializer->set(HASerializer::WithUniqueId);
_serializer->set(AHATOFSTR(HADeviceClassProperty), _class);
_serializer->set(AHATOFSTR(HAStateEntityCategory), _entityCategory);
_serializer->set(AHATOFSTR(HAIconProperty), _icon);

// optional property
Expand Down
12 changes: 12 additions & 0 deletions src/device-types/HAButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class HAButton : public HABaseDeviceType
inline void setDeviceClass(const char* deviceClass)
{ _class = deviceClass; }

/**
* Sets the entity category for the sensor.
* See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
*
* @param entityCategory The category name.
*/
inline void setEntityCategory(const char* entityCategory)
{ _entityCategory = entityCategory; }

/**
* Sets icon of the button.
* Any icon from MaterialDesignIcons.com (for example: `mdi:home`).
Expand Down Expand Up @@ -72,6 +81,9 @@ class HAButton : public HABaseDeviceType
/// The device class. It can be nullptr.
const char* _class;

/// The entity category for the sensor. It can be nullptr. See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
const char* _entityCategory;

/// The icon of the button. It can be nullptr.
const char* _icon;

Expand Down
4 changes: 3 additions & 1 deletion src/device-types/HANumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ HANumber::HANumber(const char* uniqueId, const NumberPrecision precision) :
HABaseDeviceType(AHATOFSTR(HAComponentNumber), uniqueId),
_precision(precision),
_class(nullptr),
_entityCategory(nullptr),
_icon(nullptr),
_retain(false),
_optimistic(false),
Expand Down Expand Up @@ -42,11 +43,12 @@ void HANumber::buildSerializer()
return;
}

_serializer = new HASerializer(this, 16); // 16 - max properties nb
_serializer = new HASerializer(this, 17); // 17 - max properties nb
_serializer->set(AHATOFSTR(HANameProperty), _name);
_serializer->set(AHATOFSTR(HAObjectIdProperty), _objectId);
_serializer->set(HASerializer::WithUniqueId);
_serializer->set(AHATOFSTR(HADeviceClassProperty), _class);
_serializer->set(AHATOFSTR(HAStateEntityCategory), _entityCategory);
_serializer->set(AHATOFSTR(HAIconProperty), _icon);
_serializer->set(AHATOFSTR(HAUnitOfMeasurementProperty), _unitOfMeasurement);
_serializer->set(
Expand Down
12 changes: 12 additions & 0 deletions src/device-types/HANumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ class HANumber : public HABaseDeviceType
inline void setDeviceClass(const char* deviceClass)
{ _class = deviceClass; }

/**
* Sets the entity category for the sensor.
* See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
*
* @param entityCategory The category name.
*/
inline void setEntityCategory(const char* entityCategory)
{ _entityCategory = entityCategory; }

/**
* Sets icon of the number.
* Any icon from MaterialDesignIcons.com (for example: `mdi:home`).
Expand Down Expand Up @@ -228,6 +237,9 @@ class HANumber : public HABaseDeviceType
/// The device class. It can be nullptr.
const char* _class;

/// The entity category for the sensor. It can be nullptr. See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
const char* _entityCategory;

/// The icon of the number. It can be nullptr.
const char* _icon;

Expand Down
4 changes: 3 additions & 1 deletion src/device-types/HASensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ HASensor::HASensor(const char* uniqueId, const uint16_t features) :
HABaseDeviceType(AHATOFSTR(HAComponentSensor), uniqueId),
_features(features),
_deviceClass(nullptr),
_entityCategory(nullptr),
_stateClass(nullptr),
_forceUpdate(false),
_icon(nullptr),
Expand Down Expand Up @@ -46,12 +47,13 @@ void HASensor::buildSerializer()
return;
}

_serializer = new HASerializer(this, 13); // 13 - max properties nb
_serializer = new HASerializer(this, 14); // 13 - max properties nb
_serializer->set(AHATOFSTR(HANameProperty), _name);
_serializer->set(AHATOFSTR(HAObjectIdProperty), _objectId);
_serializer->set(HASerializer::WithUniqueId);
_serializer->set(AHATOFSTR(HADeviceClassProperty), _deviceClass);
_serializer->set(AHATOFSTR(HAStateClassProperty), _stateClass);
_serializer->set(AHATOFSTR(HAStateEntityCategory), _entityCategory);
_serializer->set(AHATOFSTR(HAIconProperty), _icon);
_serializer->set(AHATOFSTR(HAUnitOfMeasurementProperty), _unitOfMeasurement);

Expand Down
12 changes: 12 additions & 0 deletions src/device-types/HASensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ class HASensor : public HABaseDeviceType
inline void setDeviceClass(const char* deviceClass)
{ _deviceClass = deviceClass; }

/**
* Sets the entity category for the sensor.
* See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
*
* @param entityCategory The category name.
*/
inline void setEntityCategory(const char* entityCategory)
{ _entityCategory = entityCategory; }

/**
* Sets class of the state for the long term stats.
* See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics
Expand Down Expand Up @@ -110,6 +119,9 @@ class HASensor : public HABaseDeviceType
/// The device class. It can be nullptr.
const char* _deviceClass;

/// The entity category for the sensor. It can be nullptr. See: https://www.home-assistant.io/integrations/sensor.mqtt/#entity_category
const char* _entityCategory;

/// The state class for the long term stats. It can be nullptr. See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics
const char* _stateClass;

Expand Down
1 change: 1 addition & 0 deletions src/utils/HADictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const char HAUniqueIdProperty[] PROGMEM = {"uniq_id"};
const char HAObjectIdProperty[] PROGMEM = {"obj_id"};
const char HADeviceProperty[] PROGMEM = {"dev"};
const char HADeviceClassProperty[] PROGMEM = {"dev_cla"};
const char HAStateEntityCategory[] PROGMEM = {"ent_cat"};
const char HAStateClassProperty[] PROGMEM = {"stat_cla"};
const char HAIconProperty[] PROGMEM = {"ic"};
const char HARetainProperty[] PROGMEM = {"ret"};
Expand Down
1 change: 1 addition & 0 deletions src/utils/HADictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern const char HAUniqueIdProperty[];
extern const char HAObjectIdProperty[];
extern const char HADeviceProperty[];
extern const char HADeviceClassProperty[];
extern const char HAStateEntityCategory[];
extern const char HAStateClassProperty[];
extern const char HAIconProperty[];
extern const char HARetainProperty[];
Expand Down