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:
parent
3215457f91
commit
77156ffbe5
10 changed files with 995 additions and 100 deletions
|
|
@ -9,6 +9,7 @@
|
|||
#include <string>
|
||||
#include "AlexaDevice.h"
|
||||
#include "AlexaLog.h"
|
||||
#include "AlexaResources.h"
|
||||
#include "AlexaVersion.h"
|
||||
|
||||
// The endpoint id and the defaults are the same in every example; its name, category and capabilities are not
|
||||
|
|
@ -552,15 +553,17 @@ void test_semantics_on_an_interface_that_takes_none_are_refused_with_an_error(vo
|
|||
CapturedLog log;
|
||||
AlexaLog::setOutput(&log);
|
||||
AlexaDevice device("Test Lamp", "root", "ESP-01");
|
||||
device.addCapability(AlexaInterfaces::PowerController)
|
||||
->addActionMapping(ActionMapping({AlexaAction::Open}, "TurnOn"))
|
||||
.addStateMapping({AlexaState::Open}, "ON");
|
||||
AlexaCapability *power = device.addCapability(AlexaInterfaces::PowerController);
|
||||
power->addActionMapping(ActionMapping({AlexaAction::Open}, "TurnOn"));
|
||||
AlexaCapability *brightness = device.addCapability(AlexaInterfaces::BrightnessController);
|
||||
brightness->addStateMapping({AlexaState::Open}, "ON");
|
||||
|
||||
assertLogged(log, "error: Alexa.PowerController: action mapping not added: "
|
||||
"only RangeController, ModeController and ToggleController take one");
|
||||
assertLogged(log, "error: Alexa.PowerController: state mapping not added: "
|
||||
assertLogged(log, "error: Alexa.BrightnessController: state mapping not added: "
|
||||
"only RangeController, ModeController and ToggleController take one");
|
||||
assertDiscovery(endpoint("Test Lamp", "OTHER", POWER), device);
|
||||
TEST_ASSERT_FALSE(power->isValid());
|
||||
TEST_ASSERT_FALSE(brightness->isValid());
|
||||
}
|
||||
|
||||
void test_more_names_and_mappings_than_a_capability_holds_are_refused_with_an_error(void)
|
||||
|
|
@ -581,11 +584,12 @@ void test_more_names_and_mappings_than_a_capability_holds_are_refused_with_an_er
|
|||
{
|
||||
lift->addStateMapping({AlexaState::Open}, 100.0f);
|
||||
}
|
||||
const std::string full = announcedBy(device);
|
||||
TEST_ASSERT_NOT_NULL(strstr(announcedBy(device).c_str(), "Alexa.RangeController"));
|
||||
TEST_ASSERT_TRUE(lift->isValid());
|
||||
TEST_ASSERT_EQUAL_STRING("", log.text.c_str() + strlen("[Alex2ESP] ESP-01: capability Alexa.RangeController Blind.Lift added\r\n"));
|
||||
|
||||
lift->addFriendlyName("Shade", "en-US")
|
||||
.addFriendlyAsset(PSTR("Alexa.Setting.Opening"))
|
||||
.addFriendlyAsset(AlexaAssets::Setting_Opening)
|
||||
.addActionMapping(ActionMapping({AlexaAction::Close}, "SetRangeValue", "{\"rangeValue\":0}"))
|
||||
.addStateMapping({AlexaState::Closed}, 0.0f);
|
||||
|
||||
|
|
@ -596,7 +600,125 @@ void test_more_names_and_mappings_than_a_capability_holds_are_refused_with_an_er
|
|||
"it has 4 already (ALEX2ESP_MAX_ACTION_MAPPINGS)");
|
||||
assertLogged(log, "error: Alexa.RangeController Blind.Lift: state mapping not added: "
|
||||
"it has 2 already (ALEX2ESP_MAX_STATE_MAPPINGS)");
|
||||
TEST_ASSERT_EQUAL_STRING(full.c_str(), announcedBy(device).c_str());
|
||||
TEST_ASSERT_FALSE(lift->isValid());
|
||||
}
|
||||
|
||||
// One capability for every call that a setter refuses: each has to make its capability invalid
|
||||
void test_capability_is_valid_until_a_setter_refuses_what_it_is_given(void)
|
||||
{
|
||||
AlexaDevice device("Bedroom Blinds", "root", "ESP-01");
|
||||
AlexaCapability *valid = device.addCapability(AlexaInterfaces::ToggleController, "Blind.Valid");
|
||||
valid->addFriendlyName("Blind", "en-US")
|
||||
.addFriendlyAsset(AlexaAssets::DeviceName_Shade)
|
||||
.setNonControllable(true)
|
||||
.addActionMapping(ActionMapping({AlexaAction::Close}, "TurnOn"))
|
||||
.addStateMapping({AlexaState::Closed}, "ON");
|
||||
TEST_ASSERT_TRUE(valid->isValid());
|
||||
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.A")->addFriendlyName("", "en-US").isValid());
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.B")->addFriendlyName("Blind", nullptr).isValid());
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.C")->addFriendlyAsset(nullptr).isValid());
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.D")->addActionMapping(ActionMapping({}, "TurnOn")).isValid());
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.E")->addActionMapping(ActionMapping({AlexaAction::Open}, "")).isValid());
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.F")->addStateMapping({}, "ON").isValid());
|
||||
TEST_ASSERT_FALSE(device.addCapability(AlexaInterfaces::ToggleController, "Blind.G")
|
||||
->addStateMapping({AlexaState::Open}, static_cast<const char *>(nullptr))
|
||||
.isValid());
|
||||
}
|
||||
|
||||
void test_capability_that_is_not_valid_is_left_out_of_discovery_with_an_error(void)
|
||||
{
|
||||
CapturedLog log;
|
||||
AlexaLog::setOutput(&log);
|
||||
AlexaDevice device("Bedroom Blinds", "root", "ESP-01");
|
||||
device.addCapability(AlexaInterfaces::PowerController);
|
||||
// The name that was refused is followed by one that is taken: the capability could be announced, as half
|
||||
// of what the sketch asked for
|
||||
device.addCapability(AlexaInterfaces::RangeController, "Blind.Lift")
|
||||
->addFriendlyName("Lift", nullptr)
|
||||
.addFriendlyAsset(AlexaAssets::Setting_Opening);
|
||||
|
||||
assertDiscovery(endpoint("Bedroom Blinds", "OTHER", POWER), device);
|
||||
assertLogged(log, "error: ESP-01: Alexa.RangeController Blind.Lift left out of discovery: "
|
||||
"a call that set it up was refused, correct what the error of that call names");
|
||||
}
|
||||
|
||||
void test_state_mapping_takes_a_number_of_any_type(void)
|
||||
{
|
||||
AlexaDevice device("Bedroom Blinds", "root", "ESP-01");
|
||||
AlexaCapability *lift = device.addCapability(AlexaInterfaces::RangeController, "Blind.Lift");
|
||||
lift->addFriendlyAsset(AlexaAssets::Setting_Opening).addStateMapping({AlexaState::Closed}, 0).addStateMapping({AlexaState::Open}, 1, 100);
|
||||
AlexaCapability *tilt = device.addCapability(AlexaInterfaces::RangeController, "Blind.Tilt");
|
||||
tilt->addFriendlyAsset(AlexaAssets::Setting_Direction).addStateMapping({AlexaState::Closed}, 0.0).addStateMapping({AlexaState::Open}, 90u);
|
||||
|
||||
TEST_ASSERT_TRUE(lift->isValid());
|
||||
TEST_ASSERT_TRUE(tilt->isValid());
|
||||
assertDiscovery(
|
||||
endpoint("Bedroom Blinds", "OTHER",
|
||||
"{\"interface\":\"Alexa.RangeController\",\"version\":\"3\",\"type\":\"AlexaInterface\","
|
||||
"\"properties\":{\"retrievable\":true,\"proactivelyReported\":false,"
|
||||
"\"supported\":[{\"name\":\"rangeValue\"}]},"
|
||||
"\"semantics\":{\"stateMappings\":["
|
||||
"{\"@type\":\"StatesToValue\",\"states\":[\"Alexa.States.Closed\"],\"value\":0},"
|
||||
"{\"@type\":\"StatesToRange\",\"states\":[\"Alexa.States.Open\"],"
|
||||
"\"range\":{\"minimumValue\":1,\"maximumValue\":100}}]},"
|
||||
"\"instance\":\"Blind.Lift\","
|
||||
"\"capabilityResources\":{\"friendlyNames\":["
|
||||
"{\"@type\":\"asset\",\"value\":{\"assetId\":\"Alexa.Setting.Opening\"}}]}},"
|
||||
"{\"interface\":\"Alexa.RangeController\",\"version\":\"3\",\"type\":\"AlexaInterface\","
|
||||
"\"properties\":{\"retrievable\":true,\"proactivelyReported\":false,"
|
||||
"\"supported\":[{\"name\":\"rangeValue\"}]},"
|
||||
"\"semantics\":{\"stateMappings\":["
|
||||
"{\"@type\":\"StatesToValue\",\"states\":[\"Alexa.States.Closed\"],\"value\":0},"
|
||||
"{\"@type\":\"StatesToValue\",\"states\":[\"Alexa.States.Open\"],\"value\":90}]},"
|
||||
"\"instance\":\"Blind.Tilt\","
|
||||
"\"capabilityResources\":{\"friendlyNames\":["
|
||||
"{\"@type\":\"asset\",\"value\":{\"assetId\":\"Alexa.Setting.Direction\"}}]}}"),
|
||||
device);
|
||||
}
|
||||
|
||||
void test_states_of_a_hunch_and_of_an_announcement_are_announced(void)
|
||||
{
|
||||
AlexaDevice device("Washer", "root", "ESP-01");
|
||||
device.addCapability(AlexaInterfaces::ToggleController, "Washer.Eco")
|
||||
->addFriendlyName("Eco", "en-US")
|
||||
.addStateMapping({AlexaState::EcoOn}, "ON")
|
||||
.addStateMapping({AlexaState::EcoOff}, "OFF");
|
||||
device.addCapability(AlexaInterfaces::ModeController, "Washer.Cycle")
|
||||
->addFriendlyAsset(AlexaAssets::Setting_WashCycle)
|
||||
.addStateMapping({AlexaState::Stuck, AlexaState::Done}, "Cycle.End");
|
||||
device.addCapability(AlexaInterfaces::RangeController, "Washer.Detergent")
|
||||
->addFriendlyName("Detergent", "en-US")
|
||||
.addStateMapping({AlexaState::Empty, AlexaState::Low}, 0, 10)
|
||||
.addStateMapping({AlexaState::Full}, 100);
|
||||
|
||||
const std::string announced = announcedBy(device);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(strstr(announced.c_str(),
|
||||
"\"stateMappings\":[{\"@type\":\"StatesToValue\",\"states\":[\"Alexa.States.EcoOn\"],\"value\":\"ON\"},"
|
||||
"{\"@type\":\"StatesToValue\",\"states\":[\"Alexa.States.EcoOff\"],\"value\":\"OFF\"}]"));
|
||||
TEST_ASSERT_NOT_NULL(strstr(announced.c_str(),
|
||||
"\"stateMappings\":[{\"@type\":\"StatesToValue\","
|
||||
"\"states\":[\"Alexa.States.Done\",\"Alexa.States.Stuck\"],\"value\":\"Cycle.End\"}]"));
|
||||
TEST_ASSERT_NOT_NULL(strstr(announced.c_str(),
|
||||
"\"stateMappings\":[{\"@type\":\"StatesToRange\",\"states\":[\"Alexa.States.Low\",\"Alexa.States.Empty\"],"
|
||||
"\"range\":{\"minimumValue\":0,\"maximumValue\":10}},"
|
||||
"{\"@type\":\"StatesToValue\",\"states\":[\"Alexa.States.Full\"],\"value\":100}]"));
|
||||
}
|
||||
|
||||
void test_assets_and_units_have_the_ids_of_alexa(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Setting.Opening", AlexaAssets::Setting_Opening);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Setting.2GGuestWiFi", AlexaAssets::Setting_2GGuestWiFi);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.DeviceName.SpaceHeater", AlexaAssets::DeviceName_SpaceHeater);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Value.QuickWash", AlexaAssets::Value_QuickWash);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Button.EighthButton", AlexaAssets::Button_EighthButton);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Gestures.DoubleTap", AlexaAssets::Gesture_DoubleTap);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Shower.RainHead", AlexaAssets::Shower_RainHead);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Actions.Vacuum", AlexaAssets::Actions_Vacuum);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Unit.Percent", AlexaUnits::Percent);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Unit.Temperature.Celsius", AlexaUnits::Temperature_Celsius);
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Unit.Volume.CubicMeters", AlexaUnits::Volume_CubicMeters);
|
||||
}
|
||||
|
||||
void test_action_mapping_payload_that_is_no_object_is_left_out_with_an_error(void)
|
||||
|
|
@ -620,6 +742,8 @@ void test_actions_and_states_have_the_names_of_alexa(void)
|
|||
TEST_ASSERT_EQUAL_STRING("Alexa.Actions.Open", alexaActionName(AlexaAction::Open));
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Actions.SetEcoOff", alexaActionName(AlexaAction::SetEcoOff));
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.States.Closed", alexaStateName(AlexaState::Closed));
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.States.EcoOn", alexaStateName(AlexaState::EcoOn));
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.States.Stuck", alexaStateName(AlexaState::Stuck));
|
||||
TEST_ASSERT_EQUAL_STRING("Alexa.Actions.Lower", AlexaActionsUtils::toString(AlexaActions::Lower).c_str());
|
||||
}
|
||||
|
||||
|
|
@ -658,5 +782,10 @@ int main(int, char **)
|
|||
RUN_TEST(test_more_names_and_mappings_than_a_capability_holds_are_refused_with_an_error);
|
||||
RUN_TEST(test_action_mapping_payload_that_is_no_object_is_left_out_with_an_error);
|
||||
RUN_TEST(test_actions_and_states_have_the_names_of_alexa);
|
||||
RUN_TEST(test_capability_is_valid_until_a_setter_refuses_what_it_is_given);
|
||||
RUN_TEST(test_capability_that_is_not_valid_is_left_out_of_discovery_with_an_error);
|
||||
RUN_TEST(test_state_mapping_takes_a_number_of_any_type);
|
||||
RUN_TEST(test_states_of_a_hunch_and_of_an_announcement_are_announced);
|
||||
RUN_TEST(test_assets_and_units_have_the_ids_of_alexa);
|
||||
return UNITY_END();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue