mirror of
https://github.com/alexhopeoconnor/arduino-home-assistant.git
synced 2026-10-04 02:48:13 +10:00
Support for statistics from sensors in HA (#179)
* Fix statistics from sensors * Test for state class setter in HASensor
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"};
|
||||
|
||||
@@ -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[];
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user