Report helpers for every tier-1 property, asset ids, AlexaCapability::isValid()

AlexaStatusMessage gets a helper for color, percentage, power level, range, mode, lock state, contact,
motion, humidity and the thermostat (target, lower and upper setpoint, mode), and addProperty(row, place,
value) for a property without one. Each is a function of its own on one shared builder; a value outside of
the range of its property is reported as the nearest of the range with an error.
AlexaResources.h holds the 103 asset ids of resources-and-assets.html (AlexaAssets, AlexaUnits); AlexaState
has the seven states that were missing; addStateMapping() takes a number of any type.
A setter that refuses its argument makes isValid() false, and the capability is left out of discovery with
an error instead of being announced without what was refused.
basicLight: static RAM 30,520 B (was 30,540), flash 333,389 B (was 334,981); 130 host tests (was 112).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
David 2026-09-28 21:00:01 +00:00
parent 3215457f91
commit 77156ffbe5
10 changed files with 995 additions and 100 deletions

View file

@ -493,6 +493,272 @@ void test_message_is_sent_once(void)
TEST_ASSERT_EQUAL_STRING("null", transport.sent[1].json.c_str()); // the bridge refuses it as empty
}
// The properties of the message as it was published, without what is around them
static std::string propertiesOf(const FakeTransport::Message &message)
{
JsonDocument doc;
TEST_ASSERT_TRUE(deserializeJson(doc, message.json) == DeserializationError::Ok);
std::string text;
serializeJson(doc["context"]["properties"], text);
return text;
}
#define PROPERTY(ns, name, value) "{\"namespace\":\"" ns "\",\"name\":\"" name "\",\"value\":" value SAMPLED
#define PROPERTY_OF(ns, name, instance, value) \
"{\"namespace\":\"" ns "\",\"name\":\"" name "\",\"instance\":\"" instance "\",\"value\":" value SAMPLED
void test_numbers_of_a_light_are_reported_as_they_are(void)
{
FakeTransport transport;
AlexaDevice lamp("Lamp", "root", "ESP-01", &transport);
lamp.response(TOKEN_OF_THE_DIRECTIVE)
.addBrightnessControllerProp(40)
.addPercentageControllerProp(75)
.addPowerLevelControllerProp(0)
.addColorTemperatureControllerProp(10000)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.BrightnessController", "brightness", "40") ","
PROPERTY("Alexa.PercentageController", "percentage", "75") ","
PROPERTY("Alexa.PowerLevelController", "powerLevel", "0") ","
PROPERTY("Alexa.ColorTemperatureController", "colorTemperatureInKelvin", "10000") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_color_is_reported_as_hue_saturation_and_brightness(void)
{
FakeTransport transport;
AlexaDevice lamp("Lamp", "root", "ESP-01", &transport);
lamp.response(TOKEN_OF_THE_DIRECTIVE).addColorControllerProp(350.5f, 0.75f, 0.5f).send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.ColorController", "color", "{\"hue\":350.5,\"saturation\":0.75,\"brightness\":0.5}") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_generic_controllers_report_with_their_instance(void)
{
FakeTransport transport;
AlexaDevice fan("Fan", "root", "ESP-01", &transport);
fan.response(TOKEN_OF_THE_DIRECTIVE)
.addRangeControllerProp("Fan.Speed", 7.5f)
.addRangeControllerProp("Fan.Angle", 100)
.addModeControllerProp("Fan.Direction", "Direction.Forward")
.addToggleControllerProp("Fan.Oscillate", PowerController::ON)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY_OF("Alexa.RangeController", "rangeValue", "Fan.Speed", "7.5") ","
PROPERTY_OF("Alexa.RangeController", "rangeValue", "Fan.Angle", "100") ","
PROPERTY_OF("Alexa.ModeController", "mode", "Fan.Direction", "\"Direction.Forward\"") ","
PROPERTY_OF("Alexa.ToggleController", "toggleState", "Fan.Oscillate", "\"ON\"") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_lock_reports_each_of_its_states(void)
{
FakeTransport transport;
AlexaDevice lock("Lock", "root", "ESP-01", &transport);
lock.response(TOKEN_OF_THE_DIRECTIVE)
.addLockControllerProp(AlexaLockState::LOCKED)
.addLockControllerProp(AlexaLockState::UNLOCKED)
.addLockControllerProp(AlexaLockState::JAMMED, 2000)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.LockController", "lockState", "\"LOCKED\"") ","
PROPERTY("Alexa.LockController", "lockState", "\"UNLOCKED\"") ","
"{\"namespace\":\"Alexa.LockController\",\"name\":\"lockState\",\"value\":\"JAMMED\","
"\"timeOfSample\":\"2026-09-28T13:05:09Z\",\"uncertaintyInMilliseconds\":2000}]",
propertiesOf(transport.sent[0]).c_str());
}
void test_contact_and_motion_report_what_they_detect(void)
{
FakeTransport transport;
AlexaDevice door("Door", "root", "ESP-01", &transport);
door.response(TOKEN_OF_THE_DIRECTIVE)
.addContactSensorProp(true)
.addContactSensorProp(false)
.addMotionSensorProp(true)
.addMotionSensorProp(false)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.ContactSensor", "detectionState", "\"DETECTED\"") ","
PROPERTY("Alexa.ContactSensor", "detectionState", "\"NOT_DETECTED\"") ","
PROPERTY("Alexa.MotionSensor", "detectionState", "\"DETECTED\"") ","
PROPERTY("Alexa.MotionSensor", "detectionState", "\"NOT_DETECTED\"") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_humidity_is_reported_inside_an_object(void)
{
FakeTransport transport;
AlexaDevice sensor("Sensor", "root", "ESP-01", &transport);
sensor.response(TOKEN_OF_THE_DIRECTIVE).addHumiditySensorProp(45.5f).send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.HumiditySensor", "relativeHumidity", "{\"value\":45.5}") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_health_reports_a_device_that_cannot_be_reached(void)
{
FakeTransport transport;
AlexaDevice sensor("Sensor", "root", "ESP-01", &transport);
sensor.response(TOKEN_OF_THE_DIRECTIVE).addHealthProp(EndpointHealth::OK).addHealthProp(EndpointHealth::UNREACHABLE).send();
TEST_ASSERT_EQUAL_STRING(
"[" HEALTH_OK "," PROPERTY("Alexa.EndpointHealth", "connectivity", "{\"value\":\"UNREACHABLE\"}") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_thermostat_with_one_setpoint_reports_the_target_and_the_mode(void)
{
FakeTransport transport;
AlexaDevice thermostat("Thermostat", "root", "ESP-01", &transport);
thermostat.response(TOKEN_OF_THE_DIRECTIVE)
.addThermostatSetpointProp(21.5f)
.addThermostatModeProp(AlexaThermostatMode::HEAT)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.ThermostatController", "targetSetpoint", "{\"value\":21.5,\"scale\":\"CELSIUS\"}") ","
PROPERTY("Alexa.ThermostatController", "thermostatMode", "\"HEAT\"") "]",
propertiesOf(transport.sent[0]).c_str());
}
void test_thermostat_with_two_setpoints_reports_the_lower_and_the_upper(void)
{
FakeTransport transport;
AlexaDevice thermostat("Thermostat", "root", "ESP-01", &transport);
thermostat.response(TOKEN_OF_THE_DIRECTIVE)
.addThermostatDualSetpointProp(68, 76, TemperatureSensorScale::FAHRENHEIT)
.addThermostatModeProp(AlexaThermostatMode::AUTO)
.send();
thermostat.response(TOKEN_OF_THE_DIRECTIVE)
.addThermostatLowerSetpointProp(68, TemperatureSensorScale::FAHRENHEIT)
.addThermostatUpperSetpointProp(76, TemperatureSensorScale::FAHRENHEIT)
.addThermostatModeProp(AlexaThermostatMode::AUTO)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.ThermostatController", "lowerSetpoint", "{\"value\":68,\"scale\":\"FAHRENHEIT\"}") ","
PROPERTY("Alexa.ThermostatController", "upperSetpoint", "{\"value\":76,\"scale\":\"FAHRENHEIT\"}") ","
PROPERTY("Alexa.ThermostatController", "thermostatMode", "\"AUTO\"") "]",
propertiesOf(transport.sent[0]).c_str());
TEST_ASSERT_EQUAL_STRING(propertiesOf(transport.sent[0]).c_str(), propertiesOf(transport.sent[1]).c_str());
}
void test_every_thermostat_mode_has_the_name_of_alexa(void)
{
FakeTransport transport;
AlexaDevice thermostat("Thermostat", "root", "ESP-01", &transport);
const AlexaThermostatMode modes[] = {AlexaThermostatMode::AUTO, AlexaThermostatMode::COOL, AlexaThermostatMode::ECO,
AlexaThermostatMode::EM_HEAT, AlexaThermostatMode::HEAT, AlexaThermostatMode::OFF};
const char *const names[] = {"AUTO", "COOL", "ECO", "EM_HEAT", "HEAT", "OFF"};
for (int i = 0; i < 6; i++)
{
thermostat.response(TOKEN_OF_THE_DIRECTIVE).addThermostatModeProp(modes[i]).send();
JsonDocument report;
TEST_ASSERT_TRUE(deserializeJson(report, transport.sent[i].json) == DeserializationError::Ok);
TEST_ASSERT_EQUAL_STRING(names[i], report["context"]["properties"][0]["value"] | "");
}
}
void test_value_outside_of_its_range_is_reported_as_the_nearest_with_an_error(void)
{
CapturedLog log;
AlexaLog::setOutput(&log);
FakeTransport transport;
AlexaDevice lamp("Lamp", "root", "ESP-01", &transport);
lamp.response(TOKEN_OF_THE_DIRECTIVE)
.addBrightnessControllerProp(120)
.addPercentageControllerProp(-5)
.addColorTemperatureControllerProp(500)
.addHumiditySensorProp(100.5f)
.addColorControllerProp(400, -0.25f, 0.5f)
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.BrightnessController", "brightness", "100") ","
PROPERTY("Alexa.PercentageController", "percentage", "0") ","
PROPERTY("Alexa.ColorTemperatureController", "colorTemperatureInKelvin", "1000") ","
PROPERTY("Alexa.HumiditySensor", "relativeHumidity", "{\"value\":100}") ","
PROPERTY("Alexa.ColorController", "color", "{\"hue\":360,\"saturation\":0,\"brightness\":0.5}") "]",
propertiesOf(transport.sent[0]).c_str());
assertLogged(log, "error: ESP-01: Alexa.BrightnessController reported as 100: "
"the value was outside of 0 to 100, the range Alexa gives for it");
assertLogged(log, "error: ESP-01: Alexa.PercentageController reported as 0: the value was outside of 0 to 100");
assertLogged(log, "error: ESP-01: Alexa.ColorTemperatureController reported as 1000: the value was outside of 1000 to 10000");
assertLogged(log, "error: ESP-01: Alexa.HumiditySensor reported as 100: the value was outside of 0 to 100");
assertLogged(log, "error: ESP-01: Alexa.ColorController reported as 360: the value was outside of 0 to 360");
assertLogged(log, "error: ESP-01: Alexa.ColorController reported as 0: the value was outside of 0 to 1");
}
void test_property_without_a_function_is_added_by_its_place_in_the_row(void)
{
CapturedLog log;
AlexaLog::setOutput(&log);
FakeTransport transport;
AlexaDevice speaker("Speaker", "root", "ESP-01", &transport);
JsonDocument values;
values["volume"] = 40;
values["muted"] = false;
values["level"]["@type"] = "Percentage";
values["level"]["value"] = 60;
speaker.response(TOKEN_OF_THE_DIRECTIVE)
.addProperty(AlexaInterfaces::Speaker, 0, values["volume"])
.addProperty(AlexaInterfaces::Speaker, 1, values["muted"], 250)
.addProperty(AlexaInterfaces::InventoryLevelSensor, 0, values["level"], 0, "Speaker.Battery")
.addProperty(AlexaInterfaces::Speaker, 2, values["volume"])
.addProperty(AlexaInterfaces::SceneController, 0, values["volume"])
.send();
TEST_ASSERT_EQUAL_STRING(
"[" PROPERTY("Alexa.Speaker", "volume", "40") ","
"{\"namespace\":\"Alexa.Speaker\",\"name\":\"muted\",\"value\":false,"
"\"timeOfSample\":\"2026-09-28T13:05:09Z\",\"uncertaintyInMilliseconds\":250},"
PROPERTY_OF("Alexa.InventoryLevelSensor", "level", "Speaker.Battery", "{\"@type\":\"Percentage\",\"value\":60}") "]",
propertiesOf(transport.sent[0]).c_str());
assertLogged(log, "error: ESP-01: property 2 of Alexa.Speaker not added: the interface has 2, the first of them is 0");
assertLogged(log, "error: ESP-01: property 0 of Alexa.SceneController not added: the interface has 0");
}
void test_change_report_takes_the_properties_of_every_function(void)
{
FakeTransport transport;
AlexaDevice door("Door", "root", "ESP-01", &transport);
TEST_ASSERT_TRUE(door.changeReport(AlexaCause::PHYSICAL_INTERACTION)
.addContactSensorProp(true)
.context()
.addLockControllerProp(AlexaLockState::UNLOCKED)
.addHealthProp(EndpointHealth::OK)
.send());
TEST_ASSERT_EQUAL_STRING(
"{\"event\":{" HEADER_OF("Alexa", "ChangeReport") "}" ENDPOINT
",\"payload\":{\"change\":{\"cause\":{\"type\":\"PHYSICAL_INTERACTION\"},\"properties\":["
PROPERTY("Alexa.ContactSensor", "detectionState", "\"DETECTED\"") "]}}},"
"\"context\":{\"properties\":[" PROPERTY("Alexa.LockController", "lockState", "\"UNLOCKED\"") "," HEALTH_OK "]}}",
published(transport.sent[0]).c_str());
}
int main(int, char **)
{
UNITY_BEGIN();
@ -515,5 +781,19 @@ int main(int, char **)
RUN_TEST(test_every_message_has_an_id_of_its_own);
RUN_TEST(test_change_report_of_a_handler_is_not_its_answer);
RUN_TEST(test_message_is_sent_once);
RUN_TEST(test_numbers_of_a_light_are_reported_as_they_are);
RUN_TEST(test_color_is_reported_as_hue_saturation_and_brightness);
RUN_TEST(test_generic_controllers_report_with_their_instance);
RUN_TEST(test_lock_reports_each_of_its_states);
RUN_TEST(test_contact_and_motion_report_what_they_detect);
RUN_TEST(test_humidity_is_reported_inside_an_object);
RUN_TEST(test_health_reports_a_device_that_cannot_be_reached);
RUN_TEST(test_thermostat_with_one_setpoint_reports_the_target_and_the_mode);
RUN_TEST(test_thermostat_with_two_setpoints_reports_the_lower_and_the_upper);
RUN_TEST(test_every_thermostat_mode_has_the_name_of_alexa);
RUN_TEST(test_value_outside_of_its_range_is_reported_as_the_nearest_with_an_error);
RUN_TEST(test_property_without_a_function_is_added_by_its_place_in_the_row);
RUN_TEST(test_change_report_takes_the_properties_of_every_function);
return UNITY_END();
}