mirror of
https://github.com/alexhopeoconnor/arduino-home-assistant.git
synced 2026-10-13 14:51:21 +10:00
* added tests for HADeviceTracker * added support for progmem property value in the HASerializer * codebase update * add support for source type in HADeviceTracker * added support for HACamera * added support for HALock * rename HABinarySensor::getState to getCurrentState * standardize sender name in all callback headers * remove duplicate serializer field protection (reduce flash usage) * optimize HAUtils * minor improvements * optimize writing progmem payload * optimize HAUtils:numberToStr * remove relloc from HASerializer * optimize HASerializerArray * remove includeNullTerminator arg from the topics size generators * optimize HASerializer * replace dtostrf with custom implementation * optimize BaseDeviceType * removed relloc from the HAMqtt * removed support for floats * removed vtable for HADevice, HAMqtt, HASerializer and HASerializerArray * implemented HASwitch with tests * implemented basic HADeviceTrigger * added support for built-in triggers * finished HADeviceTrigger * update changelog * implemented base HASensor * implemented HASensorInteger * finished HASensorFloat * override onMqttConnected in HASensorInteger and HASensorFloat * add more tests for HASensorInteger and HASensorFloat * optimize HASensorFloat * update changelog * standardize constructor of the HABinarySensor * update readme * readme * readme * standardize HADeviceTracker API * standardize HASwitch API * update docs README * move Sphinx files to docsrc directory * type in HASwitch * test docs * add .nojekyll to the docs directory * update makefile of the docs * wip: docs * docs: fix html title * docs: update styling and sidebar * docs: add "Basics" section * docs: device configuration * docs: availability * docs: MQTT discovery * docs: MQTT security * docs: MQTT advanced features * rename BaseDeviceType to HABaseDeviceType * update gitignore * add API reference * update gitignore * HADevice doxygen * doxygen css * HADevice doxygen typos * docs: update links * docs: HAUtils doxygen * HAMqtt and HADevice doxygen * docs: device types * docs: HABaseDeviceType * reduced flash usage by 190 bytes * fix tests * move HAUtils class to the utils directory * make HADevice const in HAMqtt * HABinarySensor docs * doxygen * HADeviceTrigger doxygen * HALock doxygen * HASensor doxygen * HASensor doxygen * HASwitch doxygen * HATagScanner doxygen * HASerializer doxygen * fix SAMD compile error * add HAMqtt::publish method * update examples * add HAButton example * update changelog * added support for the optimistic property in the HACover * do not set optimistic property in the HASwitch if it's set to default value * update examples list * update HASensor example * update MQTT docs * fix device trigger example link * fix search form style on the search result page * added support for the optimistic property in the HALock * update readme * change readme icon * added HALock example * MDNS example * added HASensorFloat and HASensorInteger examples * update readme * update examples * fix crash in the HABaseDeviceType * add support for storing RAM elements in the HASerializerArray * update tests cases in the CoverTest * finished tests for HASelect * added support for HASelect * update README, CHANGELOG and examples list * DeviceTest typos * add more tests to SelectTest * optimize HASelect command's handler * improve CoverTest * optimize HACover command's handler * optimize HALock command's handler * update SerializerTest * remove HAMqtt::writePayload_P method * remove FlagInternalType from the HASerializer * added support for __FlashStringHelper in the topics related functions * migrated serializer to use __FlashStringHelper * add __FlashStringHelper to the HABaseDeviceType::publishOnDataTopic method * move component name to flash memory * update docs * fix debug logs * fix memory leaks * monitor memory leaks in all test cases * optimize tests (RAM) * fix HALock states * fix bug in the HASensor (invalid serialization of the value template property) * fix memory leak in the HASelect * fix crash in HASelect * fix memory leak in the HADeviceTrigger * fix memory leak in the HADevice * tests: add support for ESP8266 and ESP32 envs * update docs * add multi-switch example * update docs structure * update MQTT advanced example in the docs * HADevice: fix _uniqueId memory release * wip: HANumber * add HANumber to defines * refactor HASensor implementation for numbers (again) * update docs * wip: HANumber * wip: add support for number's precision in the HASerializer * add support for float numbers in the HASerializer * optimize vtables in the HASensor * wip: HANumber * fix crash in the float serialization * handle another edge case in the float serialization * replace int32_t with HAUtils::Number type * HANumber fixes * tests for HANumber * change HAUtils::Number type to int64_t * implemented HAUtils::strToNumber * finished HANumber command handler * finished HANumber * update docs * add number example * added support for HAScene * add scene example, update docs * update doxygen * remove memset from the codebase * micro optimizations * fix commands handlers * update HACover code * codebase cleanup * finished HAFan implementation * update docs, added fan example * make json readable in all tests * make position configurable in the HACover * docs * added support for HALight * implement default values as static HAFan members * update docs, added light example * added support for color temperature in the HALight * update light example * update docs * wip: HVAC * improve all tests with callbacks * update logs * add HVAC to the docs * optimize HANumber * add current temperature to the HVAC * started work on HVAC tests * added action feature to the HVAC * add temperature unit to the HVAC * add more tests to UtilsTest * fix fake MQTT messages mock - message shouldn't include null terminator * fix HAUtils::strToNumber * optimize HAFan, HACover and HALight * add min/max temp to the HVAC * added temp step property to the HVAC * rename HASensorNumber::getCurrentValueAsFloat to getCurrentValueFloat * update docs * added support for aux heating in the HVAC * added power command to the HVAC * add subscriptions to the HVAC * added fan mode to the HVAC * update examples * update command handlers in all device types * added swing mode to the HVAC * finished HVAC * update docs * fix memory leak in the HVAC * added support for the target temperature in the HVAC * add HVAC example * update docs * remove unnecessary checks from some device types * added support for RGB in the HALight * update docs * standardize HAHVAC code * rename HAButton::onPress method to onCommand * rename HAScene::onActivate method to onCommand * fix seg fault in the HAUtils * HASensorNumber: do not publish default value on connect * add return uint16_t to the HAUtils::numberToStr method * simplify logic in the HALight::publishRGBColor * optimize numbers serialization in the HASerializer * update README, added LICENSE.md * rename license file * added tag-scanner example * update publishOnDataTopic methods * added esp32-cam example * update docs * add examples to the README.md * update examples * update HAMqtt.h for latest SAMD core, update aunier * update README, fix auniter settings for esp32 * update README * added documentation link to the README * final tweaks in the HACover * fix logic of the speed feature in the HAFan * fix mdns example * fix bug in the DeviceTrigger (invalid serialization of the progmem type/subtype) * implemented HANumeric * update HASensorNumber * update HANumber * update HALight * update HAHVAC * update HAFan * update HACover * update examples * update tests * update docs * fix HANumeric constructor bug * update HANumeric for SAMD * update readme and docs * add payload_on to the scene config (HA throws an exception if this property is missing) * update sensor-analog example * update sensor-integer example * HVAC fixes * update docs Co-authored-by: Dawid Chyrzynski <dawid.chyrzynski@gmail.com>
382 lines
9.0 KiB
Arduino
382 lines
9.0 KiB
Arduino
#include <AUnit.h>
|
|
#include <ArduinoHA.h>
|
|
|
|
#define prepareTest(maxEntriesNb) \
|
|
initMqttTest(testDeviceId); \
|
|
DummyDeviceType dummyDeviceType; \
|
|
HASerializer serializer(&dummyDeviceType, maxEntriesNb);
|
|
|
|
#define flushSerializer(mock, serializer) \
|
|
mock->connectDummy(); \
|
|
mock->beginPublish(testTopic, serializer.calculateSize(), false); \
|
|
serializer.flush(); \
|
|
mock->endPublish();
|
|
|
|
#define assertSerializerMqttMessage(expectedJson) \
|
|
assertSingleMqttMessage(testTopic, expectedJson, false)
|
|
|
|
using aunit::TestRunner;
|
|
|
|
static const char* testDeviceId = "testDevice";
|
|
static const char* testTopic = "testTopic";
|
|
const char TestComponentStr[] PROGMEM = {"dummyProgmem"};
|
|
|
|
class DummyDeviceType : public HABaseDeviceType
|
|
{
|
|
public:
|
|
DummyDeviceType(): HABaseDeviceType(AHATOFSTR(TestComponentStr), "testId") { }
|
|
|
|
protected:
|
|
virtual void onMqttConnected() override { }
|
|
};
|
|
|
|
AHA_TEST(SerializerTest, empty_json) {
|
|
prepareTest(0)
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, skip_null_fields) {
|
|
prepareTest(2)
|
|
|
|
serializer.set(AHATOFSTR(HADeviceClassProperty), "Class");
|
|
serializer.set(AHATOFSTR(HAIconProperty), nullptr);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"dev_cla\":\"Class\"}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, char_field) {
|
|
prepareTest(1)
|
|
|
|
serializer.set(AHATOFSTR(HANameProperty), "XYZ");
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"name\":\"XYZ\"}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, bool_false_field) {
|
|
prepareTest(1)
|
|
|
|
bool value = false;
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::BoolPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"name\":false}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, bool_true_field) {
|
|
prepareTest(1)
|
|
|
|
bool value = true;
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::BoolPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"name\":true}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, number_zero_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(0, 0);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"name\":0}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, number_signed_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(-12346756, 0);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"name\":-12346756}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, number_unsigned_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(312346733, 0);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{\"name\":312346733}");
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, float_p1_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(250.5235f, 1);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{\"name\":250.5}");
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, float_p2_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(250.5235f, 2);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{\"name\":250.52}");
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, float_p3_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(250.5235f, 3);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{\"name\":250.523}");
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, float_p3_zero_signed_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(-0.243f, 3);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{\"name\":-0.243}");
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, float_p3_zero_unsigned_field) {
|
|
prepareTest(1)
|
|
|
|
HANumeric value(0.243f, 3);
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
&value,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer);
|
|
assertSerializerMqttMessage("{\"name\":0.243}");
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, progmem_field) {
|
|
prepareTest(1)
|
|
|
|
serializer.set(
|
|
AHATOFSTR(HANameProperty),
|
|
HAOffline,
|
|
HASerializer::ProgmemPropertyValue
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"name\":\"offline\"}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, topic_field) {
|
|
prepareTest(1)
|
|
|
|
serializer.topic(AHATOFSTR(HAStateTopic));
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage(
|
|
"{\"stat_t\":\"testData/testDevice/testId/stat_t\"}"
|
|
)
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, topics_field) {
|
|
prepareTest(2)
|
|
|
|
serializer.topic(AHATOFSTR(HAStateTopic));
|
|
serializer.topic(AHATOFSTR(HAAvailabilityTopic));
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage(
|
|
(
|
|
"{"
|
|
"\"stat_t\":\"testData/testDevice/testId/stat_t\","
|
|
"\"avty_t\":\"testData/testDevice/testId/avty_t\""
|
|
"}"
|
|
)
|
|
)
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, device_serialization) {
|
|
prepareTest(1)
|
|
|
|
serializer.set(HASerializer::WithDevice);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"dev\":{\"ids\":\"testDevice\"}}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, device_mixed_serialization) {
|
|
prepareTest(2)
|
|
|
|
serializer.set(HASerializer::WithDevice);
|
|
serializer.set(AHATOFSTR(HADeviceClassProperty), "Class1");
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage(
|
|
"{\"dev\":{\"ids\":\"testDevice\"},\"dev_cla\":\"Class1\"}"
|
|
)
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, device_type_availability) {
|
|
prepareTest(1)
|
|
|
|
dummyDeviceType.setAvailability(false);
|
|
serializer.set(HASerializer::WithAvailability);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage(
|
|
"{\"avty_t\":\"testData/testDevice/testId/avty_t\"}"
|
|
)
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, device_type_availability_mixed) {
|
|
prepareTest(2)
|
|
|
|
dummyDeviceType.setAvailability(false);
|
|
serializer.set(HASerializer::WithAvailability);
|
|
serializer.set(AHATOFSTR(HADeviceClassProperty), "Class1");
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage(
|
|
(
|
|
"{"
|
|
"\"avty_t\":\"testData/testDevice/testId/avty_t\","
|
|
"\"dev_cla\":\"Class1\""
|
|
"}"
|
|
)
|
|
)
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, shared_availability) {
|
|
prepareTest(1)
|
|
|
|
device.enableSharedAvailability();
|
|
serializer.set(HASerializer::WithAvailability);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"avty_t\":\"testData/testDevice/avty_t\"}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, empty_array) {
|
|
prepareTest(1)
|
|
|
|
HASerializerArray array(0);
|
|
serializer.set(
|
|
AHATOFSTR(HADeviceClassProperty),
|
|
&array,
|
|
HASerializer::ArrayPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"dev_cla\":[]}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, two_element_array) {
|
|
prepareTest(1)
|
|
|
|
HASerializerArray array(2);
|
|
array.add(HADeviceProperty);
|
|
array.add(HAIconProperty);
|
|
serializer.set(
|
|
AHATOFSTR(HADeviceClassProperty),
|
|
&array,
|
|
HASerializer::ArrayPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage("{\"dev_cla\":[\"dev\",\"ic\"]}")
|
|
}
|
|
|
|
AHA_TEST(SerializerTest, mixed_elements) {
|
|
prepareTest(6)
|
|
|
|
HASerializerArray array(2);
|
|
array.add(HADeviceProperty);
|
|
array.add(HAIconProperty);
|
|
|
|
dummyDeviceType.setAvailability(false);
|
|
serializer.set(
|
|
AHATOFSTR(HADeviceClassProperty),
|
|
&array,
|
|
HASerializer::ArrayPropertyType
|
|
);
|
|
serializer.set(HASerializer::WithAvailability);
|
|
serializer.set(HASerializer::WithDevice);
|
|
serializer.set(AHATOFSTR(HANameProperty), "TestName");
|
|
serializer.topic(AHATOFSTR(HAStateTopic));
|
|
|
|
HANumeric intValue(312346733, 0);
|
|
serializer.set(
|
|
AHATOFSTR(HAIconProperty),
|
|
&intValue,
|
|
HASerializer::NumberPropertyType
|
|
);
|
|
|
|
flushSerializer(mock, serializer)
|
|
assertSerializerMqttMessage(
|
|
(
|
|
"{"
|
|
"\"dev_cla\":[\"dev\",\"ic\"],"
|
|
"\"avty_t\":\"testData/testDevice/testId/avty_t\","
|
|
"\"dev\":{\"ids\":\"testDevice\"},"
|
|
"\"name\":\"TestName\","
|
|
"\"stat_t\":\"testData/testDevice/testId/stat_t\","
|
|
"\"ic\":312346733"
|
|
"}"
|
|
)
|
|
)
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
delay(1000);
|
|
Serial.begin(115200);
|
|
while (!Serial);
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
TestRunner::run();
|
|
delay(1);
|
|
}
|