Extract Home Assistant discovery logic
This commit is contained in:
130
test/test_ha_discovery/test_ha_discovery.cpp
Normal file
130
test/test_ha_discovery/test_ha_discovery.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include <unity.h>
|
||||
#include <Arduino.h>
|
||||
#include "HADiscovery.h"
|
||||
|
||||
void setUp()
|
||||
{
|
||||
}
|
||||
|
||||
void tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
void test_publish_message_count()
|
||||
{
|
||||
int callbackInvokedCount = 0;
|
||||
|
||||
auto callback = [&](const char *topic, const char *message)
|
||||
{
|
||||
callbackInvokedCount++;
|
||||
// TEST_ASSERT_EQUAL_STRING("my_prefix/shade_key", topic);
|
||||
// TEST_ASSERT_EQUAL_STRING("{}", message);
|
||||
};
|
||||
|
||||
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
|
||||
|
||||
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, callbackInvokedCount);
|
||||
}
|
||||
|
||||
void test_publish_cover_message()
|
||||
{
|
||||
int messageAssertedCount = 0;
|
||||
|
||||
auto callback = [&](const char *topic, const char *message)
|
||||
{
|
||||
if (strstr(topic, "homeassistant/cover") != nullptr)
|
||||
{
|
||||
messageAssertedCount++;
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/cover/abcd/config", topic);
|
||||
|
||||
auto expected_message = "{\"name\":null,\"unique_id\":\"cover-abcd\",\"availability_topic\":\"my_prefix/availability\",\"state_topic\":\"my_prefix/test_shade/state\",\"command_topic\":\"my_prefix/test_shade/command\",\"position_topic\":\"my_prefix/test_shade/position\",\"set_position_topic\":\"my_prefix/test_shade/set_position\",\"position_open\":100,\"position_closed\":0,\"optimistic\":false,\"device\":{\"name\":\"Test Shade\",\"identifiers\":[\"hotdog-abcd\"],\"manufacturer\":\"Hunter Douglas\"}}";
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(expected_message, message);
|
||||
}
|
||||
};
|
||||
|
||||
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
|
||||
|
||||
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(1, messageAssertedCount);
|
||||
}
|
||||
|
||||
void test_publish_battery_message()
|
||||
{
|
||||
int messageAssertedCount = 0;
|
||||
|
||||
auto callback = [&](const char *topic, const char *message)
|
||||
{
|
||||
if (strstr(topic, "homeassistant/sensor") != nullptr)
|
||||
{
|
||||
messageAssertedCount++;
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/sensor/abcd/config", topic);
|
||||
|
||||
auto expected_message = "{\"name\":\"Battery\",\"unique_id\":\"battery-abcd\",\"availability_topic\":\"my_prefix/availability\",\"device_class\":\"battery\",\"state_topic\":\"my_prefix/test_shade/battery\",\"device\":{\"name\":\"Test Shade\",\"identifiers\":[\"hotdog-abcd\"],\"manufacturer\":\"Hunter Douglas\"}}";
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(expected_message, message);
|
||||
}
|
||||
};
|
||||
|
||||
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
|
||||
|
||||
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(1, messageAssertedCount);
|
||||
}
|
||||
|
||||
void test_publish_button_message()
|
||||
{
|
||||
int messageAssertedCount = 0;
|
||||
|
||||
auto callback = [&](const char *topic, const char *message)
|
||||
{
|
||||
if (strstr(topic, "homeassistant/button") != nullptr)
|
||||
{
|
||||
messageAssertedCount++;
|
||||
TEST_ASSERT_EQUAL_STRING("homeassistant/button/abcd/config", topic);
|
||||
|
||||
auto expected_message = "{\"name\":\"Refresh\",\"unique_id\":\"refresh-button-abcd\",\"availability_topic\":\"my_prefix/availability\",\"command_topic\":\"my_prefix/test_shade/command\",\"payload_press\":\"REFRESH\",\"device\":{\"name\":\"Test Shade\",\"identifiers\":[\"hotdog-abcd\"],\"manufacturer\":\"Hunter Douglas\"}}";
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(expected_message, message);
|
||||
}
|
||||
};
|
||||
|
||||
HADiscovery haDiscovery = HADiscovery("my_prefix/", callback);
|
||||
|
||||
haDiscovery.publish(Shade{0xABCD, "test_shade", "Test Shade", -1, -1, 0, 0, nullptr});
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(1, messageAssertedCount);
|
||||
}
|
||||
|
||||
int runUnityTests(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_publish_message_count);
|
||||
RUN_TEST(test_publish_cover_message);
|
||||
RUN_TEST(test_publish_battery_message);
|
||||
RUN_TEST(test_publish_button_message);
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
/**
|
||||
* For Arduino framework
|
||||
*/
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Wait ~2 seconds before the Unity test runner
|
||||
// establishes connection with a board Serial interface
|
||||
delay(2000);
|
||||
|
||||
runUnityTests();
|
||||
}
|
||||
void loop() {}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return runUnityTests();
|
||||
}
|
||||
Reference in New Issue
Block a user