mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[gpio_expander] Fix bank caching (#10077)
Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
		| @@ -0,0 +1,25 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.const import CONF_ID | ||||
|  | ||||
| AUTO_LOAD = ["gpio_expander"] | ||||
|  | ||||
| gpio_expander_test_component_ns = cg.esphome_ns.namespace( | ||||
|     "gpio_expander_test_component" | ||||
| ) | ||||
|  | ||||
| GPIOExpanderTestComponent = gpio_expander_test_component_ns.class_( | ||||
|     "GPIOExpanderTestComponent", cg.Component | ||||
| ) | ||||
|  | ||||
|  | ||||
| CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(GPIOExpanderTestComponent), | ||||
|     } | ||||
| ).extend(cv.COMPONENT_SCHEMA) | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = cg.new_Pvariable(config[CONF_ID]) | ||||
|     await cg.register_component(var, config) | ||||
| @@ -0,0 +1,38 @@ | ||||
| #include "gpio_expander_test_component.h" | ||||
|  | ||||
| #include "esphome/core/application.h" | ||||
| #include "esphome/core/log.h" | ||||
|  | ||||
| namespace esphome::gpio_expander_test_component { | ||||
|  | ||||
| static const char *const TAG = "gpio_expander_test"; | ||||
|  | ||||
| void GPIOExpanderTestComponent::setup() { | ||||
|   for (uint8_t pin = 0; pin < 32; pin++) { | ||||
|     this->digital_read(pin); | ||||
|   } | ||||
|  | ||||
|   this->digital_read(3); | ||||
|   this->digital_read(3); | ||||
|   this->digital_read(4); | ||||
|   this->digital_read(3); | ||||
|   this->digital_read(10); | ||||
|   this->reset_pin_cache_();  // Reset cache to ensure next read is from hardware | ||||
|   this->digital_read(15); | ||||
|   this->digital_read(14); | ||||
|   this->digital_read(14); | ||||
|  | ||||
|   ESP_LOGD(TAG, "DONE"); | ||||
| } | ||||
|  | ||||
| bool GPIOExpanderTestComponent::digital_read_hw(uint8_t pin) { | ||||
|   ESP_LOGD(TAG, "digital_read_hw pin=%d", pin); | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool GPIOExpanderTestComponent::digital_read_cache(uint8_t pin) { | ||||
|   ESP_LOGD(TAG, "digital_read_cache pin=%d", pin); | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| }  // namespace esphome::gpio_expander_test_component | ||||
| @@ -0,0 +1,18 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "esphome/components/gpio_expander/cached_gpio.h" | ||||
| #include "esphome/core/component.h" | ||||
|  | ||||
| namespace esphome::gpio_expander_test_component { | ||||
|  | ||||
| class GPIOExpanderTestComponent : public Component, public esphome::gpio_expander::CachedGpioExpander<uint8_t, 32> { | ||||
|  public: | ||||
|   void setup() override; | ||||
|  | ||||
|  protected: | ||||
|   bool digital_read_hw(uint8_t pin) override; | ||||
|   bool digital_read_cache(uint8_t pin) override; | ||||
|   void digital_write_hw(uint8_t pin, bool value) override{}; | ||||
| }; | ||||
|  | ||||
| }  // namespace esphome::gpio_expander_test_component | ||||
							
								
								
									
										17
									
								
								tests/integration/fixtures/gpio_expander_cache.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								tests/integration/fixtures/gpio_expander_cache.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| esphome: | ||||
|   name: gpio-expander-cache | ||||
| host: | ||||
|  | ||||
| logger: | ||||
|   level: DEBUG | ||||
|  | ||||
| api: | ||||
|  | ||||
| # External component that uses gpio_expander::CachedGpioExpander | ||||
| external_components: | ||||
|   - source: | ||||
|       type: local | ||||
|       path: EXTERNAL_COMPONENT_PATH | ||||
|     components: [gpio_expander_test_component] | ||||
|  | ||||
| gpio_expander_test_component: | ||||
		Reference in New Issue
	
	Block a user