diff --git a/src/analogsensor.cpp b/src/analogsensor.cpp index 60cbe4e9c..38d4583e2 100644 --- a/src/analogsensor.cpp +++ b/src/analogsensor.cpp @@ -333,10 +333,10 @@ void AnalogSensor::loop() { } // update analog information name and offset -// a type of -1 is used to delete the sensor +// a type value of -1 is used to delete the sensor bool AnalogSensor::update(uint8_t gpio, const std::string & name, double offset, double factor, uint8_t uom, int8_t type, bool deleted) { - boolean found_sensor = false; // see if we can find the sensor in our customization list - + // first see if we can find the sensor in our customization list + bool found_sensor = false; EMSESP::webCustomizationService.update( [&](WebCustomization & settings) { for (auto & AnalogCustomization : settings.analogCustomizations) { @@ -374,7 +374,7 @@ bool AnalogSensor::update(uint8_t gpio, const std::string & name, double offset, remove_ha_topic(type, gpio); // the GPIO } - // we didn't find it, it's new, so create and store it + // we didn't find it, it's new, so create and store it in the customization list if (!found_sensor) { EMSESP::webCustomizationService.update( [&](WebCustomization & settings) { @@ -856,15 +856,4 @@ bool AnalogSensor::command_commands(const char * value, const int8_t id, JsonObj return Command::list(EMSdevice::DeviceType::ANALOGSENSOR, output); } -// hard coded tests -#ifdef EMSESP_TEST -void AnalogSensor::test() { - sensors_.emplace_back(36, "test12", 0, 0.1, 17, AnalogType::ADC); - sensors_.back().set_value(12.4); - - sensors_.emplace_back(37, "test13", 0, 0, 0, AnalogType::DIGITAL_IN); - sensors_.back().set_value(13); -} -#endif - } // namespace emsesp \ No newline at end of file diff --git a/src/analogsensor.h b/src/analogsensor.h index 12615298d..09c470397 100644 --- a/src/analogsensor.h +++ b/src/analogsensor.h @@ -158,11 +158,6 @@ class AnalogSensor { bool command_info(const char * value, const int8_t id, JsonObject output) const; - -#if defined(EMSESP_TEST) - void test(); -#endif - private: static constexpr uint8_t MAX_SENSORS = 20; static constexpr uint32_t MEASURE_ANALOG_INTERVAL = 500; diff --git a/src/temperaturesensor.cpp b/src/temperaturesensor.cpp index 5746885bb..eeaa9bc80 100644 --- a/src/temperaturesensor.cpp +++ b/src/temperaturesensor.cpp @@ -640,16 +640,18 @@ bool TemperatureSensor::Sensor::apply_customization() { #if defined(EMSESP_TEST) void TemperatureSensor::test() { // add 2 temperature sensors + // Sensor ID: 01-0203-0405-0607 uint8_t addr[ADDR_LEN] = {1, 2, 3, 4, 5, 6, 7, 8}; sensors_.emplace_back(addr); - // sensors_.back().apply_customization(); + sensors_.back().apply_customization(); sensors_.back().temperature_c = 123; sensors_.back().read = true; publish_sensor(sensors_.back()); // call publish single + // Sensor ID: 0B-0C0D-0E0F-1011 uint8_t addr2[ADDR_LEN] = {11, 12, 13, 14, 15, 16, 17, 18}; sensors_.emplace_back(addr2); - // sensors_.back().apply_customization(); + sensors_.back().apply_customization(); sensors_.back().temperature_c = 456; sensors_.back().read = true; publish_sensor(sensors_.back()); // call publish single diff --git a/src/web/WebCustomEntityService.cpp b/src/web/WebCustomEntityService.cpp index d488ef435..84881310d 100644 --- a/src/web/WebCustomEntityService.cpp +++ b/src/web/WebCustomEntityService.cpp @@ -626,6 +626,7 @@ bool WebCustomEntityService::get_value(std::shared_ptr telegram) void WebCustomEntityService::test() { update( [&](WebCustomEntity & webCustomEntity) { + webCustomEntity.customEntityItems.clear(); // test 1 auto entityItem = CustomEntityItem(); entityItem.ram = 0; @@ -642,15 +643,15 @@ void WebCustomEntityService::test() { // test 2 entityItem.ram = 0; - entityItem.device_id = 8; - entityItem.type_id = 18; - entityItem.offset = 0; + entityItem.device_id = 24; + entityItem.type_id = 677; + entityItem.offset = 3; entityItem.factor = 1; entityItem.name = "test_read_only"; - entityItem.uom = 3; + entityItem.uom = 0; entityItem.value_type = 2; entityItem.writeable = false; - entityItem.data = "12"; + entityItem.data = "48"; webCustomEntity.customEntityItems.push_back(entityItem); // test 2 diff --git a/src/web/WebCustomizationService.cpp b/src/web/WebCustomizationService.cpp index 61e9f18db..04b6c1b61 100644 --- a/src/web/WebCustomizationService.cpp +++ b/src/web/WebCustomizationService.cpp @@ -89,19 +89,6 @@ void WebCustomization::read(WebCustomization & customizations, JsonObject root) // call on initialization and also when the page is saved via web UI // this loads the data into the internal class StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & customizations) { -#ifdef EMSESP_STANDALONE - // invoke some fake data for testing - const char * json = "{\"ts\":[],\"as\":[],\"masked_entities\":[{\"product_id\":123,\"device_id\":8,\"entity_ids\":[\"08heatingactive|my custom " - "name for heating active (HS1)\",\"08tapwateractive\"]}]}"; - JsonDocument doc; - deserializeJson(doc, json); - root = doc.as(); - Serial.print(COLOR_BRIGHT_MAGENTA); - Serial.print(" Using fake customization file: "); - serializeJson(root, Serial); - Serial.println(COLOR_RESET); -#endif - // Temperature Sensor customization customizations.sensorCustomizations.clear(); if (root["ts"].is()) { @@ -120,35 +107,36 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c if (root["as"].is()) { for (const JsonObject analogJson : root["as"].as()) { // create each of the sensor, overwriting any previous settings - auto sensor = AnalogCustomization(); - sensor.gpio = analogJson["gpio"]; - sensor.name = analogJson["name"].as(); - sensor.offset = analogJson["offset"]; - sensor.factor = analogJson["factor"]; - sensor.uom = analogJson["uom"]; - sensor.type = analogJson["type"]; - if (_start && sensor.type == EMSESP::analogsensor_.AnalogType::DIGITAL_OUT && sensor.uom > DeviceValue::DeviceValueUOM::NONE) { - sensor.offset = sensor.uom - 1; + auto analog = AnalogCustomization(); + analog.gpio = analogJson["gpio"]; + analog.name = analogJson["name"].as(); + analog.offset = analogJson["offset"]; + analog.factor = analogJson["factor"]; + analog.uom = analogJson["uom"]; + analog.type = analogJson["type"]; + if (_start && analog.type == EMSESP::analogsensor_.AnalogType::DIGITAL_OUT && analog.uom > DeviceValue::DeviceValueUOM::NONE) { + analog.offset = analog.uom - 1; } - customizations.analogCustomizations.push_back(sensor); // add to list + customizations.analogCustomizations.push_back(analog); // add to list } } + _start = false; // load array of entities id's with masks, building up the object class customizations.entityCustomizations.clear(); if (root["masked_entities"].is()) { for (const JsonObject masked_entities : root["masked_entities"].as()) { - auto new_entry = EntityCustomization(); - new_entry.product_id = masked_entities["product_id"]; - new_entry.device_id = masked_entities["device_id"]; + auto emsEntity = EntityCustomization(); + emsEntity.product_id = masked_entities["product_id"]; + emsEntity.device_id = masked_entities["device_id"]; for (const JsonVariant masked_entity_id : masked_entities["entity_ids"].as()) { if (masked_entity_id.is()) { - new_entry.entity_ids.push_back(masked_entity_id.as()); // add entity list + emsEntity.entity_ids.push_back(masked_entity_id.as()); // add entity list } } - customizations.entityCustomizations.push_back(new_entry); // save the new object + customizations.entityCustomizations.push_back(emsEntity); // save the new object } } @@ -256,6 +244,7 @@ void WebCustomizationService::customization_entities(AsyncWebServerRequest * req } // emsesp::EMSESP::logger().info(id.as()); } + // add deleted entities from file read([&](WebCustomization & settings) { for (EntityCustomization entityCustomization : settings.entityCustomizations) { @@ -282,6 +271,7 @@ void WebCustomizationService::customization_entities(AsyncWebServerRequest * req } } }); + // get list of entities that have masks set or a custom fullname emsdevice->getCustomizationEntities(entity_ids); @@ -326,4 +316,61 @@ void WebCustomizationService::begin() { _fsPersistence.readFromFS(); } -} // namespace emsesp \ No newline at end of file + +// hard coded tests +#ifdef EMSESP_TEST +void WebCustomizationService::test() { + update( + [&](WebCustomization & webCustomization) { + // Temperature sensors + webCustomization.sensorCustomizations.clear(); + auto sensor = SensorCustomization(); + sensor.id = "01-0203-0405-0607"; + sensor.name = "test_sensor1"; + sensor.offset = 0; + webCustomization.sensorCustomizations.push_back(sensor); + + sensor = SensorCustomization(); + sensor.id = "0B-0C0D-0E0F-1011"; + sensor.name = "test_sensor2"; + sensor.offset = 4; + webCustomization.sensorCustomizations.push_back(sensor); + + // Analog sensors + // This actually adds the sensors as we use customizations to store them + webCustomization.analogCustomizations.clear(); + auto analog = AnalogCustomization(); + analog.gpio = 36; + analog.name = "test_analog1"; + analog.offset = 0; + analog.factor = 0.1; + analog.uom = 17; + analog.type = 3; + webCustomization.analogCustomizations.push_back(analog); + + analog = AnalogCustomization(); + analog.gpio = 37; + analog.name = "test_analog2"; + analog.offset = 0; + analog.factor = 1; + analog.uom = 0; + analog.type = 1; + webCustomization.analogCustomizations.push_back(analog); + + // EMS entities + webCustomization.entityCustomizations.clear(); + auto emsEntity = EntityCustomization(); + emsEntity.product_id = 123; + emsEntity.device_id = 8; + emsEntity.entity_ids.push_back("08heatingactive|is my heating on?"); + webCustomization.entityCustomizations.push_back(emsEntity); + + return StateUpdateResult::CHANGED; // persist the changes + }, + "local"); + + EMSESP::analogsensor_.reload(); // this is needed to active the analog sensors +} +#endif + +} // namespace emsesp diff --git a/src/web/WebCustomizationService.h b/src/web/WebCustomizationService.h index 8ec322b71..41ceea90c 100644 --- a/src/web/WebCustomizationService.h +++ b/src/web/WebCustomizationService.h @@ -83,6 +83,10 @@ class WebCustomizationService : public StatefulService { void begin(); +#if defined(EMSESP_TEST) + void test(); +#endif + // make all functions public so we can test in the debug and standalone mode #ifndef EMSESP_STANDALONE private: diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 80b01d806..bd2b6cb9a 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -419,12 +419,13 @@ void WebSchedulerService::loop() { void WebSchedulerService::test() { update( [&](WebScheduler & webScheduler) { + webScheduler.scheduleItems.clear(); // test 1 auto si = ScheduleItem(); si.active = true; si.flags = 1; si.time = "12:00"; - si.cmd = "test"; + si.cmd = "system/fetch"; si.value = "10"; si.name = "test_scheduler"; si.elapsed_min = 0;