mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 14:43:51 +00:00 
			
		
		
		
	test
This commit is contained in:
		| @@ -1,19 +1,79 @@ | ||||
| from esphome import automation | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.const import CONF_ID | ||||
| from esphome.const import CONF_ID, CONF_NAME | ||||
|  | ||||
| CODEOWNERS = ["@esphome/tests"] | ||||
|  | ||||
| loop_test_component_ns = cg.esphome_ns.namespace("loop_test_component") | ||||
| LoopTestComponent = loop_test_component_ns.class_("LoopTestComponent", cg.Component) | ||||
|  | ||||
| CONF_DISABLE_AFTER = "disable_after" | ||||
| CONF_TEST_REDUNDANT_OPERATIONS = "test_redundant_operations" | ||||
| CONF_COMPONENTS = "components" | ||||
|  | ||||
| COMPONENT_CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(LoopTestComponent), | ||||
|         cv.Required(CONF_NAME): cv.string, | ||||
|         cv.Optional(CONF_DISABLE_AFTER, default=0): cv.int_, | ||||
|         cv.Optional(CONF_TEST_REDUNDANT_OPERATIONS, default=False): cv.boolean, | ||||
|     } | ||||
| ) | ||||
|  | ||||
| CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(LoopTestComponent), | ||||
|         cv.Required(CONF_COMPONENTS): cv.ensure_list(COMPONENT_CONFIG_SCHEMA), | ||||
|     } | ||||
| ).extend(cv.COMPONENT_SCHEMA) | ||||
|  | ||||
| # Define actions | ||||
| EnableAction = loop_test_component_ns.class_("EnableAction", automation.Action) | ||||
| DisableAction = loop_test_component_ns.class_("DisableAction", automation.Action) | ||||
|  | ||||
|  | ||||
| @automation.register_action( | ||||
|     "loop_test_component.enable", | ||||
|     EnableAction, | ||||
|     cv.Schema( | ||||
|         { | ||||
|             cv.Required(CONF_ID): cv.use_id(LoopTestComponent), | ||||
|         } | ||||
|     ), | ||||
| ) | ||||
| async def enable_to_code(config, action_id, template_arg, args): | ||||
|     parent = await cg.get_variable(config[CONF_ID]) | ||||
|     var = cg.new_Pvariable(action_id, template_arg, parent) | ||||
|     return var | ||||
|  | ||||
|  | ||||
| @automation.register_action( | ||||
|     "loop_test_component.disable", | ||||
|     DisableAction, | ||||
|     cv.Schema( | ||||
|         { | ||||
|             cv.Required(CONF_ID): cv.use_id(LoopTestComponent), | ||||
|         } | ||||
|     ), | ||||
| ) | ||||
| async def disable_to_code(config, action_id, template_arg, args): | ||||
|     parent = await cg.get_variable(config[CONF_ID]) | ||||
|     var = cg.new_Pvariable(action_id, template_arg, parent) | ||||
|     return var | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = cg.new_Pvariable(config[CONF_ID]) | ||||
|     await cg.register_component(var, config) | ||||
|     # The parent config doesn't actually create a component | ||||
|     # We just create each sub-component | ||||
|     for comp_config in config[CONF_COMPONENTS]: | ||||
|         var = cg.new_Pvariable(comp_config[CONF_ID]) | ||||
|         await cg.register_component(var, comp_config) | ||||
|  | ||||
|         cg.add(var.set_name(comp_config[CONF_NAME])) | ||||
|         cg.add(var.set_disable_after(comp_config[CONF_DISABLE_AFTER])) | ||||
|         cg.add( | ||||
|             var.set_test_redundant_operations( | ||||
|                 comp_config[CONF_TEST_REDUNDANT_OPERATIONS] | ||||
|             ) | ||||
|         ) | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/core/log.h" | ||||
| #include "esphome/core/application.h" | ||||
| #include "esphome/core/automation.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace loop_test_component { | ||||
| @@ -11,78 +12,76 @@ static const char *const TAG = "loop_test_component"; | ||||
|  | ||||
| class LoopTestComponent : public Component { | ||||
|  public: | ||||
|   void setup() override { | ||||
|     ESP_LOGI(TAG, "LoopTestComponent setup()"); | ||||
|     this->loop_count_ = 0; | ||||
|     this->setup_disable_count_ = 0; | ||||
|     this->setup_enable_count_ = 0; | ||||
|   void set_name(const std::string &name) { this->name_ = name; } | ||||
|   void set_disable_after(int count) { this->disable_after_ = count; } | ||||
|   void set_test_redundant_operations(bool test) { this->test_redundant_operations_ = test; } | ||||
|  | ||||
|     // Test 1: Try to disable/enable in setup (before calculate_looping_components_) | ||||
|     ESP_LOGI(TAG, "Test 1: Disable in setup"); | ||||
|     this->disable_loop(); | ||||
|     this->setup_disable_count_++; | ||||
|  | ||||
|     ESP_LOGI(TAG, "Test 1: Enable in setup"); | ||||
|     this->enable_loop(); | ||||
|     this->setup_enable_count_++; | ||||
|   } | ||||
|   void setup() override { ESP_LOGI(TAG, "[%s] Setup called", this->name_.c_str()); } | ||||
|  | ||||
|   void loop() override { | ||||
|     this->loop_count_++; | ||||
|     ESP_LOGI(TAG, "[%s] Loop count: %d", this->name_.c_str(), this->loop_count_); | ||||
|  | ||||
|     if (this->loop_count_ <= 10 || this->loop_count_ % 10 == 0) { | ||||
|       ESP_LOGI(TAG, "Loop count: %d", this->loop_count_); | ||||
|     } | ||||
|  | ||||
|     // Test 2: Disable after 50 loops | ||||
|     if (this->loop_count_ == 50) { | ||||
|       ESP_LOGI(TAG, "Test 2: Disabling loop after 50 iterations"); | ||||
|     // Test self-disable after specified count | ||||
|     if (this->disable_after_ > 0 && this->loop_count_ == this->disable_after_) { | ||||
|       ESP_LOGI(TAG, "[%s] Disabling self after %d loops", this->name_.c_str(), this->disable_after_); | ||||
|       this->disable_loop(); | ||||
|       this->loop_disable_count_++; | ||||
|     } | ||||
|  | ||||
|     // This should not happen | ||||
|     if (this->loop_count_ > 50 && this->loop_count_ < 100) { | ||||
|       ESP_LOGE(TAG, "ERROR: Loop called after disable! Count: %d", this->loop_count_); | ||||
|     } | ||||
|  | ||||
|     // Test 3: Re-enable after being disabled (shouldn't get here) | ||||
|     if (this->loop_count_ == 75) { | ||||
|       ESP_LOGE(TAG, "ERROR: This code should never execute!"); | ||||
|       this->enable_loop(); | ||||
|     // Test redundant operations | ||||
|     if (this->test_redundant_operations_ && this->loop_count_ == 5) { | ||||
|       if (this->name_ == "redundant_enable") { | ||||
|         ESP_LOGI(TAG, "[%s] Testing enable when already enabled", this->name_.c_str()); | ||||
|         this->enable_loop(); | ||||
|       } else if (this->name_ == "redundant_disable") { | ||||
|         ESP_LOGI(TAG, "[%s] Testing disable when will be disabled", this->name_.c_str()); | ||||
|         // We'll disable at count 10, but try to disable again at 5 | ||||
|         this->disable_loop(); | ||||
|         ESP_LOGI(TAG, "[%s] First disable complete", this->name_.c_str()); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   // For testing from outside | ||||
|   void test_enable_from_outside() { | ||||
|     ESP_LOGI(TAG, "Test 3: Enabling from outside call"); | ||||
|   // Service methods for external control | ||||
|   void service_enable() { | ||||
|     ESP_LOGI(TAG, "[%s] Service enable called", this->name_.c_str()); | ||||
|     this->enable_loop(); | ||||
|     this->external_enable_count_++; | ||||
|   } | ||||
|  | ||||
|   void test_disable_from_outside() { | ||||
|     ESP_LOGI(TAG, "Test 4: Disabling from outside call"); | ||||
|   void service_disable() { | ||||
|     ESP_LOGI(TAG, "[%s] Service disable called", this->name_.c_str()); | ||||
|     this->disable_loop(); | ||||
|     this->external_disable_count_++; | ||||
|   } | ||||
|  | ||||
|   // Getters for test validation | ||||
|   int get_loop_count() const { return this->loop_count_; } | ||||
|   int get_setup_disable_count() const { return this->setup_disable_count_; } | ||||
|   int get_setup_enable_count() const { return this->setup_enable_count_; } | ||||
|   int get_loop_disable_count() const { return this->loop_disable_count_; } | ||||
|   int get_external_enable_count() const { return this->external_enable_count_; } | ||||
|   int get_external_disable_count() const { return this->external_disable_count_; } | ||||
|  | ||||
|   float get_setup_priority() const override { return setup_priority::DATA; } | ||||
|  | ||||
|  protected: | ||||
|   std::string name_; | ||||
|   int loop_count_{0}; | ||||
|   int setup_disable_count_{0}; | ||||
|   int setup_enable_count_{0}; | ||||
|   int loop_disable_count_{0}; | ||||
|   int external_enable_count_{0}; | ||||
|   int external_disable_count_{0}; | ||||
|   int disable_after_{0}; | ||||
|   bool test_redundant_operations_{false}; | ||||
| }; | ||||
|  | ||||
| template<typename... Ts> class EnableAction : public Action<Ts...> { | ||||
|  public: | ||||
|   EnableAction(LoopTestComponent *parent) : parent_(parent) {} | ||||
|  | ||||
|   void play(Ts... x) override { this->parent_->service_enable(); } | ||||
|  | ||||
|  protected: | ||||
|   LoopTestComponent *parent_; | ||||
| }; | ||||
|  | ||||
| template<typename... Ts> class DisableAction : public Action<Ts...> { | ||||
|  public: | ||||
|   DisableAction(LoopTestComponent *parent) : parent_(parent) {} | ||||
|  | ||||
|   void play(Ts... x) override { this->parent_->service_disable(); } | ||||
|  | ||||
|  protected: | ||||
|   LoopTestComponent *parent_; | ||||
| }; | ||||
|  | ||||
| }  // namespace loop_test_component | ||||
|   | ||||
| @@ -1,63 +0,0 @@ | ||||
| import esphome.codegen as cg | ||||
| from esphome.components import sensor | ||||
| import esphome.config_validation as cv | ||||
| from esphome.const import CONF_ID, ENTITY_CATEGORY_DIAGNOSTIC, STATE_CLASS_MEASUREMENT | ||||
|  | ||||
| from . import LoopTestComponent | ||||
|  | ||||
| DEPENDENCIES = ["loop_test_component"] | ||||
|  | ||||
| CONF_LOOP_COUNT = "loop_count" | ||||
| CONF_SETUP_DISABLE_COUNT = "setup_disable_count" | ||||
| CONF_SETUP_ENABLE_COUNT = "setup_enable_count" | ||||
| CONF_LOOP_DISABLE_COUNT = "loop_disable_count" | ||||
| CONF_EXTERNAL_ENABLE_COUNT = "external_enable_count" | ||||
| CONF_EXTERNAL_DISABLE_COUNT = "external_disable_count" | ||||
|  | ||||
| CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.GenerateID(CONF_ID): cv.use_id(LoopTestComponent), | ||||
|         cv.Optional(CONF_LOOP_COUNT): sensor.sensor_schema( | ||||
|             accuracy_decimals=0, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|             entity_category=ENTITY_CATEGORY_DIAGNOSTIC, | ||||
|         ), | ||||
|         cv.Optional(CONF_SETUP_DISABLE_COUNT): sensor.sensor_schema( | ||||
|             accuracy_decimals=0, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|             entity_category=ENTITY_CATEGORY_DIAGNOSTIC, | ||||
|         ), | ||||
|         cv.Optional(CONF_SETUP_ENABLE_COUNT): sensor.sensor_schema( | ||||
|             accuracy_decimals=0, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|             entity_category=ENTITY_CATEGORY_DIAGNOSTIC, | ||||
|         ), | ||||
|         cv.Optional(CONF_LOOP_DISABLE_COUNT): sensor.sensor_schema( | ||||
|             accuracy_decimals=0, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|             entity_category=ENTITY_CATEGORY_DIAGNOSTIC, | ||||
|         ), | ||||
|         cv.Optional(CONF_EXTERNAL_ENABLE_COUNT): sensor.sensor_schema( | ||||
|             accuracy_decimals=0, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|             entity_category=ENTITY_CATEGORY_DIAGNOSTIC, | ||||
|         ), | ||||
|         cv.Optional(CONF_EXTERNAL_DISABLE_COUNT): sensor.sensor_schema( | ||||
|             accuracy_decimals=0, | ||||
|             state_class=STATE_CLASS_MEASUREMENT, | ||||
|             entity_category=ENTITY_CATEGORY_DIAGNOSTIC, | ||||
|         ), | ||||
|     } | ||||
| ) | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     parent = await cg.get_variable(config[CONF_ID]) | ||||
|  | ||||
|     if CONF_LOOP_COUNT in config: | ||||
|         sens = await sensor.new_sensor(config[CONF_LOOP_COUNT]) | ||||
|         cg.add( | ||||
|             parent.set_loop_count_sensor(sens) | ||||
|         )  # We'll implement this in the component | ||||
|  | ||||
|     # For simplicity, let's just expose loop_count for now in the test | ||||
| @@ -1,22 +0,0 @@ | ||||
| esphome: | ||||
|   name: loop-test | ||||
|   on_boot: | ||||
|     - logger.log: "System booted!" | ||||
|      | ||||
| host: | ||||
| api: | ||||
| logger: | ||||
|   level: DEBUG | ||||
|  | ||||
| external_components: | ||||
|   - source:  | ||||
|       type: local | ||||
|       path: EXTERNAL_COMPONENT_PATH | ||||
|  | ||||
| loop_test_component: | ||||
|   id: loop_test | ||||
|  | ||||
| interval: | ||||
|   - interval: 500ms | ||||
|     then: | ||||
|       - logger.log: "Interval tick" | ||||
| @@ -1,24 +1,48 @@ | ||||
| esphome: | ||||
|   name: loop-test | ||||
|   on_boot: | ||||
|     - logger.log: "System booted!" | ||||
|      | ||||
|  | ||||
| host: | ||||
| api: | ||||
| logger: | ||||
|   level: DEBUG | ||||
|  | ||||
| external_components: | ||||
|   - source:  | ||||
|   - source: | ||||
|       type: local | ||||
|       path: EXTERNAL_COMPONENT_PATH | ||||
|  | ||||
| loop_test_component: | ||||
|   id: loop_test | ||||
|   components: | ||||
|     # Component that disables itself after 10 loops | ||||
|     - id: self_disable_10 | ||||
|       name: "self_disable_10" | ||||
|       disable_after: 10 | ||||
|  | ||||
|     # Component that never disables itself (for re-enable test) | ||||
|     - id: normal_component | ||||
|       name: "normal_component" | ||||
|       disable_after: 0 | ||||
|  | ||||
|     # Component that tests enable when already enabled | ||||
|     - id: redundant_enable | ||||
|       name: "redundant_enable" | ||||
|       test_redundant_operations: true | ||||
|       disable_after: 0 | ||||
|  | ||||
|     # Component that tests disable when already disabled | ||||
|     - id: redundant_disable | ||||
|       name: "redundant_disable" | ||||
|       test_redundant_operations: true | ||||
|       disable_after: 10 | ||||
|  | ||||
| # Interval to re-enable the self_disable_10 component after some time | ||||
| interval: | ||||
|   - interval: 1s | ||||
|   - interval: 2s | ||||
|     then: | ||||
|       - logger.log: "Interval tick" | ||||
|  | ||||
| # We'll check the loop behavior through logs and API | ||||
|       - if: | ||||
|           condition: | ||||
|             lambda: 'return id(self_disable_10).get_loop_count() == 10;' | ||||
|           then: | ||||
|             - logger.log: "Re-enabling self_disable_10 via service" | ||||
|             - loop_test_component.enable: | ||||
|                 id: self_disable_10 | ||||
|   | ||||
| @@ -1,14 +0,0 @@ | ||||
| esphome: | ||||
|   name: loop-test | ||||
| host: | ||||
| api: | ||||
| logger: | ||||
|   level: DEBUG | ||||
|  | ||||
| external_components: | ||||
|   - source:  | ||||
|       type: local | ||||
|       path: EXTERNAL_COMPONENT_PATH | ||||
|  | ||||
| loop_test_component: | ||||
|   id: loop_test | ||||
| @@ -1,44 +0,0 @@ | ||||
| esphome: | ||||
|   name: loop-test | ||||
|   on_boot: | ||||
|     priority: -100  # After all components are initialized | ||||
|     then: | ||||
|       - logger.log: "Boot complete, testing loop disable/enable" | ||||
| host: | ||||
| api: | ||||
| logger: | ||||
|   level: DEBUG | ||||
|  | ||||
| # Use interval component which already supports disable/enable | ||||
| interval: | ||||
|   - interval: 100ms | ||||
|     id: test_interval_1 | ||||
|     then: | ||||
|       - lambda: |- | ||||
|           static int count = 0; | ||||
|           count++; | ||||
|           ESP_LOGD("test", "Interval 1 count: %d", count); | ||||
|            | ||||
|           if (count == 10) { | ||||
|             ESP_LOGD("test", "Disabling interval 1 after 10 iterations"); | ||||
|             id(test_interval_1).disable(); | ||||
|           } | ||||
|            | ||||
|   - interval: 200ms  | ||||
|     id: test_interval_2 | ||||
|     then: | ||||
|       - lambda: |- | ||||
|           static int count = 0; | ||||
|           count++; | ||||
|           ESP_LOGD("test", "Interval 2 count: %d", count); | ||||
|            | ||||
|           // Re-enable interval 1 after 5 iterations | ||||
|           if (count == 5) { | ||||
|             ESP_LOGD("test", "Re-enabling interval 1"); | ||||
|             id(test_interval_1).enable(); | ||||
|           } | ||||
|            | ||||
|           if (count == 15) { | ||||
|             ESP_LOGD("test", "Disabling interval 2"); | ||||
|             id(test_interval_2).disable(); | ||||
|           } | ||||
		Reference in New Issue
	
	Block a user