From 51e28f2c935fbe2f6f0c41cc84b63e01bc9b59da Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Thu, 13 Jun 2024 11:08:03 -0400 Subject: [PATCH] Add state class support to HABinarySensor --- src/device-types/HABinarySensor.cpp | 8 +++++--- src/device-types/HABinarySensor.h | 17 +++++++++++++++-- src/device-types/HASensor.h | 3 ++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/device-types/HABinarySensor.cpp b/src/device-types/HABinarySensor.cpp index 20ab76f..155d166 100644 --- a/src/device-types/HABinarySensor.cpp +++ b/src/device-types/HABinarySensor.cpp @@ -6,7 +6,8 @@ HABinarySensor::HABinarySensor(const char* uniqueId) : HABaseDeviceType(AHATOFSTR(HAComponentBinarySensor), uniqueId), - _class(nullptr), + _deviceClass(nullptr), + _stateClass(nullptr), _icon(nullptr), _currentState(false) { @@ -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(HADeviceClassProperty), _deviceClass); + _serializer->set(AHATOFSTR(HAStateClassProperty), _stateClass); _serializer->set(AHATOFSTR(HAIconProperty), _icon); if (_expireAfter.isSet()) { diff --git a/src/device-types/HABinarySensor.h b/src/device-types/HABinarySensor.h index 518e546..fb5d41d 100644 --- a/src/device-types/HABinarySensor.h +++ b/src/device-types/HABinarySensor.h @@ -61,7 +61,17 @@ public: * @param deviceClass The class name. */ inline void setDeviceClass(const char* deviceClass) - { _class = deviceClass; } + { _deviceClass = deviceClass; } + + /** + * Sets class of the state for the long term stats. + * You can find list of available values here: https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes + * See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics + * + * @param stateClass The state class name. + */ + inline void setStateClass(const char* stateClass) + { _stateClass = stateClass; } /** * Sets icon of the sensor. @@ -86,7 +96,10 @@ private: bool publishState(bool state); /// The device class. It can be nullptr. - const char* _class; + const char* _deviceClass; + + /// The state class. It can be nullptr. + const char* _stateClass; /// The icon of the sensor. It can be nullptr. const char* _icon; diff --git a/src/device-types/HASensor.h b/src/device-types/HASensor.h index 6dbd281..4850267 100644 --- a/src/device-types/HASensor.h +++ b/src/device-types/HASensor.h @@ -66,9 +66,10 @@ public: /** * Sets class of the state for the long term stats. + * You can find list of available values here: https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes * See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics * - * @param stateClass The class name. + * @param stateClass The state class name. */ inline void setStateClass(const char* stateClass) { _stateClass = stateClass; }