diff --git a/src/device-types/HASensor.cpp b/src/device-types/HASensor.cpp index ed23e17..7372e0f 100644 --- a/src/device-types/HASensor.cpp +++ b/src/device-types/HASensor.cpp @@ -7,6 +7,7 @@ HASensor::HASensor(const char* uniqueId) : HABaseDeviceType(AHATOFSTR(HAComponentSensor), uniqueId), _deviceClass(nullptr), + _stateClass(nullptr), _forceUpdate(false), _icon(nullptr), _unitOfMeasurement(nullptr) @@ -25,10 +26,11 @@ void HASensor::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(HAUniqueIdProperty), _uniqueId); _serializer->set(AHATOFSTR(HADeviceClassProperty), _deviceClass); + _serializer->set(AHATOFSTR(HAStateClassProperty), _stateClass); _serializer->set(AHATOFSTR(HAIconProperty), _icon); _serializer->set(AHATOFSTR(HAUnitOfMeasurementProperty), _unitOfMeasurement); diff --git a/src/device-types/HASensor.h b/src/device-types/HASensor.h index a48e2c1..113987a 100644 --- a/src/device-types/HASensor.h +++ b/src/device-types/HASensor.h @@ -41,6 +41,15 @@ public: inline void setDeviceClass(const char* deviceClass) { _deviceClass = deviceClass; } + /** + * Sets class of the state for the long term stats. + * See: https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics + * + * @param stateClass The class name. + */ + inline void setStateClass(const char* stateClass) + { _stateClass = stateClass; } + /** * Forces HA panel to process each incoming value (MQTT message). * It's useful if you want to have meaningful value graphs in history. @@ -74,6 +83,9 @@ protected: private: /// The device class. It can be nullptr. const char* _deviceClass; + + /// 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; /// The force update flag for the HA panel. bool _forceUpdate; diff --git a/src/utils/HADictionary.cpp b/src/utils/HADictionary.cpp index a0328f0..24c9ec1 100644 --- a/src/utils/HADictionary.cpp +++ b/src/utils/HADictionary.cpp @@ -40,6 +40,7 @@ const char HANameProperty[] PROGMEM = {"name"}; const char HAUniqueIdProperty[] PROGMEM = {"uniq_id"}; const char HADeviceProperty[] PROGMEM = {"dev"}; const char HADeviceClassProperty[] PROGMEM = {"dev_cla"}; +const char HAStateClassProperty[] PROGMEM = {"state_class"}; const char HAIconProperty[] PROGMEM = {"ic"}; const char HARetainProperty[] PROGMEM = {"ret"}; const char HASourceTypeProperty[] PROGMEM = {"src_type"}; diff --git a/src/utils/HADictionary.h b/src/utils/HADictionary.h index a5fc912..587a8eb 100644 --- a/src/utils/HADictionary.h +++ b/src/utils/HADictionary.h @@ -40,6 +40,7 @@ extern const char HANameProperty[]; extern const char HAUniqueIdProperty[]; extern const char HADeviceProperty[]; extern const char HADeviceClassProperty[]; +extern const char HAStateClassProperty[]; extern const char HAIconProperty[]; extern const char HARetainProperty[]; extern const char HASourceTypeProperty[]; diff --git a/tests/SensorTest/SensorTest.ino b/tests/SensorTest/SensorTest.ino index fe606e1..e5e5801 100644 --- a/tests/SensorTest/SensorTest.ino +++ b/tests/SensorTest/SensorTest.ino @@ -92,6 +92,26 @@ AHA_TEST(SensorTest, device_class_setter) { ) } +AHA_TEST(SensorTest, state_class_setter) { + initMqttTest(testDeviceId) + + HASensor sensor(testUniqueId); + sensor.setStateClass("measurement"); + + assertEntityConfig( + mock, + sensor, + ( + "{" + "\"uniq_id\":\"uniqueSensor\"," + "\"state_class\":\"measurement\"," + "\"dev\":{\"ids\":\"testDevice\"}," + "\"stat_t\":\"testData/testDevice/uniqueSensor/stat_t\"" + "}" + ) + ) +} + AHA_TEST(SensorTest, force_update_setter) { initMqttTest(testDeviceId)