mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	Add publish_initial_value option to rotary encoder (#2503)
This commit is contained in:
		@@ -189,9 +189,10 @@ void RotaryEncoderSensor::loop() {
 | 
				
			|||||||
    this->store_.counter = 0;
 | 
					    this->store_.counter = 0;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  int counter = this->store_.counter;
 | 
					  int counter = this->store_.counter;
 | 
				
			||||||
  if (this->store_.last_read != counter) {
 | 
					  if (this->store_.last_read != counter || this->publish_initial_value_) {
 | 
				
			||||||
    this->store_.last_read = counter;
 | 
					    this->store_.last_read = counter;
 | 
				
			||||||
    this->publish_state(counter);
 | 
					    this->publish_state(counter);
 | 
				
			||||||
 | 
					    this->publish_initial_value_ = false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -58,6 +58,7 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
 | 
				
			|||||||
  void set_reset_pin(GPIOPin *pin_i) { this->pin_i_ = pin_i; }
 | 
					  void set_reset_pin(GPIOPin *pin_i) { this->pin_i_ = pin_i; }
 | 
				
			||||||
  void set_min_value(int32_t min_value);
 | 
					  void set_min_value(int32_t min_value);
 | 
				
			||||||
  void set_max_value(int32_t max_value);
 | 
					  void set_max_value(int32_t max_value);
 | 
				
			||||||
 | 
					  void set_publish_initial_value(bool publish_initial_value) { publish_initial_value_ = publish_initial_value; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // ========== INTERNAL METHODS ==========
 | 
					  // ========== INTERNAL METHODS ==========
 | 
				
			||||||
  // (In most use cases you won't need these)
 | 
					  // (In most use cases you won't need these)
 | 
				
			||||||
@@ -79,6 +80,7 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
 | 
				
			|||||||
  InternalGPIOPin *pin_a_;
 | 
					  InternalGPIOPin *pin_a_;
 | 
				
			||||||
  InternalGPIOPin *pin_b_;
 | 
					  InternalGPIOPin *pin_b_;
 | 
				
			||||||
  GPIOPin *pin_i_{nullptr};  /// Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.
 | 
					  GPIOPin *pin_i_{nullptr};  /// Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.
 | 
				
			||||||
 | 
					  bool publish_initial_value_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  RotaryEncoderSensorStore store_{};
 | 
					  RotaryEncoderSensorStore store_{};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,6 +27,7 @@ RESOLUTIONS = {
 | 
				
			|||||||
CONF_PIN_RESET = "pin_reset"
 | 
					CONF_PIN_RESET = "pin_reset"
 | 
				
			||||||
CONF_ON_CLOCKWISE = "on_clockwise"
 | 
					CONF_ON_CLOCKWISE = "on_clockwise"
 | 
				
			||||||
CONF_ON_ANTICLOCKWISE = "on_anticlockwise"
 | 
					CONF_ON_ANTICLOCKWISE = "on_anticlockwise"
 | 
				
			||||||
 | 
					CONF_PUBLISH_INITIAL_VALUE = "publish_initial_value"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
RotaryEncoderSensor = rotary_encoder_ns.class_(
 | 
					RotaryEncoderSensor = rotary_encoder_ns.class_(
 | 
				
			||||||
    "RotaryEncoderSensor", sensor.Sensor, cg.Component
 | 
					    "RotaryEncoderSensor", sensor.Sensor, cg.Component
 | 
				
			||||||
@@ -70,6 +71,7 @@ CONFIG_SCHEMA = cv.All(
 | 
				
			|||||||
            cv.Optional(CONF_RESOLUTION, default=1): cv.enum(RESOLUTIONS, int=True),
 | 
					            cv.Optional(CONF_RESOLUTION, default=1): cv.enum(RESOLUTIONS, int=True),
 | 
				
			||||||
            cv.Optional(CONF_MIN_VALUE): cv.int_,
 | 
					            cv.Optional(CONF_MIN_VALUE): cv.int_,
 | 
				
			||||||
            cv.Optional(CONF_MAX_VALUE): cv.int_,
 | 
					            cv.Optional(CONF_MAX_VALUE): cv.int_,
 | 
				
			||||||
 | 
					            cv.Optional(CONF_PUBLISH_INITIAL_VALUE, default=False): cv.boolean,
 | 
				
			||||||
            cv.Optional(CONF_ON_CLOCKWISE): automation.validate_automation(
 | 
					            cv.Optional(CONF_ON_CLOCKWISE): automation.validate_automation(
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
 | 
					                    cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
 | 
				
			||||||
@@ -99,6 +101,7 @@ async def to_code(config):
 | 
				
			|||||||
    cg.add(var.set_pin_a(pin_a))
 | 
					    cg.add(var.set_pin_a(pin_a))
 | 
				
			||||||
    pin_b = await cg.gpio_pin_expression(config[CONF_PIN_B])
 | 
					    pin_b = await cg.gpio_pin_expression(config[CONF_PIN_B])
 | 
				
			||||||
    cg.add(var.set_pin_b(pin_b))
 | 
					    cg.add(var.set_pin_b(pin_b))
 | 
				
			||||||
 | 
					    cg.add(var.set_publish_initial_value(config[CONF_PUBLISH_INITIAL_VALUE]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if CONF_PIN_RESET in config:
 | 
					    if CONF_PIN_RESET in config:
 | 
				
			||||||
        pin_i = await cg.gpio_pin_expression(config[CONF_PIN_RESET])
 | 
					        pin_i = await cg.gpio_pin_expression(config[CONF_PIN_RESET])
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user