mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 00:51:49 +00:00 
			
		
		
		
	Compare commits
	
		
			7 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					c9d93ff685 | ||
| 
						 | 
					fa72990a63 | ||
| 
						 | 
					e5afb1c4ea | ||
| 
						 | 
					73ead5f328 | ||
| 
						 | 
					5c57b51378 | ||
| 
						 | 
					e25935ef21 | ||
| 
						 | 
					c7a52c3894 | 
@@ -11,7 +11,30 @@ namespace adc {
 | 
			
		||||
static const char *const TAG = "adc";
 | 
			
		||||
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
void ADCSensor::set_attenuation(adc_attenuation_t attenuation) { this->attenuation_ = attenuation; }
 | 
			
		||||
void ADCSensor::set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
 | 
			
		||||
 | 
			
		||||
inline adc1_channel_t gpio_to_adc1(uint8_t pin) {
 | 
			
		||||
  switch (pin) {
 | 
			
		||||
    case 36:
 | 
			
		||||
      return ADC1_CHANNEL_0;
 | 
			
		||||
    case 37:
 | 
			
		||||
      return ADC1_CHANNEL_1;
 | 
			
		||||
    case 38:
 | 
			
		||||
      return ADC1_CHANNEL_2;
 | 
			
		||||
    case 39:
 | 
			
		||||
      return ADC1_CHANNEL_3;
 | 
			
		||||
    case 32:
 | 
			
		||||
      return ADC1_CHANNEL_4;
 | 
			
		||||
    case 33:
 | 
			
		||||
      return ADC1_CHANNEL_5;
 | 
			
		||||
    case 34:
 | 
			
		||||
      return ADC1_CHANNEL_6;
 | 
			
		||||
    case 35:
 | 
			
		||||
      return ADC1_CHANNEL_7;
 | 
			
		||||
    default:
 | 
			
		||||
      return ADC1_CHANNEL_MAX;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void ADCSensor::setup() {
 | 
			
		||||
@@ -21,7 +44,9 @@ void ADCSensor::setup() {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
  analogSetPinAttenuation(this->pin_, this->attenuation_);
 | 
			
		||||
  adc1_config_channel_atten(gpio_to_adc1(pin_), attenuation_);
 | 
			
		||||
  adc1_config_width(ADC_WIDTH_BIT_12);
 | 
			
		||||
  adc_gpio_init(ADC_UNIT_1, (adc_channel_t) gpio_to_adc1(pin_));
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
void ADCSensor::dump_config() {
 | 
			
		||||
@@ -36,18 +61,20 @@ void ADCSensor::dump_config() {
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Pin: %u", this->pin_);
 | 
			
		||||
  switch (this->attenuation_) {
 | 
			
		||||
    case ADC_0db:
 | 
			
		||||
    case ADC_ATTEN_DB_0:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, " Attenuation: 0db (max 1.1V)");
 | 
			
		||||
      break;
 | 
			
		||||
    case ADC_2_5db:
 | 
			
		||||
    case ADC_ATTEN_DB_2_5:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, " Attenuation: 2.5db (max 1.5V)");
 | 
			
		||||
      break;
 | 
			
		||||
    case ADC_6db:
 | 
			
		||||
    case ADC_ATTEN_DB_6:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, " Attenuation: 6db (max 2.2V)");
 | 
			
		||||
      break;
 | 
			
		||||
    case ADC_11db:
 | 
			
		||||
    case ADC_ATTEN_DB_11:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, " Attenuation: 11db (max 3.9V)");
 | 
			
		||||
      break;
 | 
			
		||||
    default:  // This is to satisfy the unused ADC_ATTEN_MAX
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
@@ -60,20 +87,23 @@ void ADCSensor::update() {
 | 
			
		||||
}
 | 
			
		||||
float ADCSensor::sample() {
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
  float value_v = analogRead(this->pin_) / 4095.0f;  // NOLINT
 | 
			
		||||
  int raw = adc1_get_raw(gpio_to_adc1(pin_));
 | 
			
		||||
  float value_v = raw / 4095.0f;
 | 
			
		||||
  switch (this->attenuation_) {
 | 
			
		||||
    case ADC_0db:
 | 
			
		||||
    case ADC_ATTEN_DB_0:
 | 
			
		||||
      value_v *= 1.1;
 | 
			
		||||
      break;
 | 
			
		||||
    case ADC_2_5db:
 | 
			
		||||
    case ADC_ATTEN_DB_2_5:
 | 
			
		||||
      value_v *= 1.5;
 | 
			
		||||
      break;
 | 
			
		||||
    case ADC_6db:
 | 
			
		||||
    case ADC_ATTEN_DB_6:
 | 
			
		||||
      value_v *= 2.2;
 | 
			
		||||
      break;
 | 
			
		||||
    case ADC_11db:
 | 
			
		||||
    case ADC_ATTEN_DB_11:
 | 
			
		||||
      value_v *= 3.9;
 | 
			
		||||
      break;
 | 
			
		||||
    default:  // This is to satisfy the unused ADC_ATTEN_MAX
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  return value_v;
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,10 @@
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/voltage_sampler/voltage_sampler.h"
 | 
			
		||||
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
#include "driver/adc.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace adc {
 | 
			
		||||
 | 
			
		||||
@@ -13,7 +17,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
 | 
			
		||||
 public:
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
  /// Set the attenuation for this pin. Only available on the ESP32.
 | 
			
		||||
  void set_attenuation(adc_attenuation_t attenuation);
 | 
			
		||||
  void set_attenuation(adc_atten_t attenuation);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  /// Update adc values.
 | 
			
		||||
@@ -34,7 +38,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
 | 
			
		||||
  uint8_t pin_;
 | 
			
		||||
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
  adc_attenuation_t attenuation_{ADC_0db};
 | 
			
		||||
  adc_atten_t attenuation_{ADC_ATTEN_DB_0};
 | 
			
		||||
#endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -16,10 +16,10 @@ from esphome.const import (
 | 
			
		||||
AUTO_LOAD = ["voltage_sampler"]
 | 
			
		||||
 | 
			
		||||
ATTENUATION_MODES = {
 | 
			
		||||
    "0db": cg.global_ns.ADC_0db,
 | 
			
		||||
    "2.5db": cg.global_ns.ADC_2_5db,
 | 
			
		||||
    "6db": cg.global_ns.ADC_6db,
 | 
			
		||||
    "11db": cg.global_ns.ADC_11db,
 | 
			
		||||
    "0db": cg.global_ns.ADC_ATTEN_DB_0,
 | 
			
		||||
    "2.5db": cg.global_ns.ADC_ATTEN_DB_2_5,
 | 
			
		||||
    "6db": cg.global_ns.ADC_ATTEN_DB_6,
 | 
			
		||||
    "11db": cg.global_ns.ADC_ATTEN_DB_11,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -107,17 +107,22 @@ float ADS1115Component::request_measurement(ADS1115Sensor *sensor) {
 | 
			
		||||
    }
 | 
			
		||||
    this->prev_config_ = config;
 | 
			
		||||
 | 
			
		||||
    // about 1.6 ms with 860 samples per second
 | 
			
		||||
    // about 1.2 ms with 860 samples per second
 | 
			
		||||
    delay(2);
 | 
			
		||||
 | 
			
		||||
    uint32_t start = millis();
 | 
			
		||||
    while (this->read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
 | 
			
		||||
      if (millis() - start > 100) {
 | 
			
		||||
        ESP_LOGW(TAG, "Reading ADS1115 timed out");
 | 
			
		||||
        this->status_set_warning();
 | 
			
		||||
        return NAN;
 | 
			
		||||
    // in continuous mode, conversion will always be running, rely on the delay
 | 
			
		||||
    // to ensure conversion is taking place with the correct settings
 | 
			
		||||
    // can we use the rdy pin to trigger when a conversion is done?
 | 
			
		||||
    if (!this->continuous_mode_) {
 | 
			
		||||
      uint32_t start = millis();
 | 
			
		||||
      while (this->read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
 | 
			
		||||
        if (millis() - start > 100) {
 | 
			
		||||
          ESP_LOGW(TAG, "Reading ADS1115 timed out");
 | 
			
		||||
          this->status_set_warning();
 | 
			
		||||
          return NAN;
 | 
			
		||||
        }
 | 
			
		||||
        yield();
 | 
			
		||||
      }
 | 
			
		||||
      yield();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -102,8 +102,11 @@ class PropertiesFrame : public midea_dongle::BaseFrame {
 | 
			
		||||
  void set_sleep_mode(bool state) { this->set_bytemask_(20, 0x01, state); }
 | 
			
		||||
 | 
			
		||||
  /* TURBO MODE */
 | 
			
		||||
  bool get_turbo_mode() const { return this->pbuf_[18] & 0x20; }
 | 
			
		||||
  void set_turbo_mode(bool state) { this->set_bytemask_(18, 0x20, state); }
 | 
			
		||||
  bool get_turbo_mode() const { return this->pbuf_[18] & 0x20 || this->pbuf_[20] & 0x02; }
 | 
			
		||||
  void set_turbo_mode(bool state) {
 | 
			
		||||
    this->set_bytemask_(18, 0x20, state);
 | 
			
		||||
    this->set_bytemask_(20, 0x02, state);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* FREEZE PROTECTION */
 | 
			
		||||
  bool get_freeze_protection_mode() const { return this->pbuf_[31] & 0x80; }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_MAX_VALUE,
 | 
			
		||||
    CONF_MIN_VALUE,
 | 
			
		||||
    CONF_OPTIMISTIC,
 | 
			
		||||
    CONF_RESTORE_VALUE,
 | 
			
		||||
    CONF_STEP,
 | 
			
		||||
)
 | 
			
		||||
from .. import template_ns
 | 
			
		||||
@@ -26,6 +27,17 @@ def validate_min_max(config):
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate(config):
 | 
			
		||||
    if CONF_LAMBDA in config:
 | 
			
		||||
        if CONF_OPTIMISTIC in config:
 | 
			
		||||
            raise cv.Invalid("optimistic cannot be used with lambda")
 | 
			
		||||
        if CONF_INITIAL_VALUE in config:
 | 
			
		||||
            raise cv.Invalid("initial_value cannot be used with lambda")
 | 
			
		||||
        if CONF_RESTORE_VALUE in config:
 | 
			
		||||
            raise cv.Invalid("restore_value cannot be used with lambda")
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    number.NUMBER_SCHEMA.extend(
 | 
			
		||||
        {
 | 
			
		||||
@@ -33,13 +45,15 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
            cv.Required(CONF_MAX_VALUE): cv.float_,
 | 
			
		||||
            cv.Required(CONF_MIN_VALUE): cv.float_,
 | 
			
		||||
            cv.Required(CONF_STEP): cv.positive_float,
 | 
			
		||||
            cv.Exclusive(CONF_LAMBDA, "lambda-optimistic"): cv.returning_lambda,
 | 
			
		||||
            cv.Exclusive(CONF_OPTIMISTIC, "lambda-optimistic"): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_LAMBDA): cv.returning_lambda,
 | 
			
		||||
            cv.Optional(CONF_OPTIMISTIC): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
 | 
			
		||||
            cv.Optional(CONF_INITIAL_VALUE): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
 | 
			
		||||
        }
 | 
			
		||||
    ).extend(cv.polling_component_schema("60s")),
 | 
			
		||||
    validate_min_max,
 | 
			
		||||
    validate,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -60,13 +74,15 @@ async def to_code(config):
 | 
			
		||||
        )
 | 
			
		||||
        cg.add(var.set_template(template_))
 | 
			
		||||
 | 
			
		||||
    elif CONF_OPTIMISTIC in config:
 | 
			
		||||
        cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
 | 
			
		||||
    else:
 | 
			
		||||
        if CONF_OPTIMISTIC in config:
 | 
			
		||||
            cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
 | 
			
		||||
        if CONF_INITIAL_VALUE in config:
 | 
			
		||||
            cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
 | 
			
		||||
        if CONF_RESTORE_VALUE in config:
 | 
			
		||||
            cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
 | 
			
		||||
 | 
			
		||||
    if CONF_SET_ACTION in config:
 | 
			
		||||
        await automation.build_automation(
 | 
			
		||||
            var.get_set_trigger(), [(float, "x")], config[CONF_SET_ACTION]
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    if CONF_INITIAL_VALUE in config:
 | 
			
		||||
        cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
 | 
			
		||||
 
 | 
			
		||||
@@ -7,16 +7,20 @@ namespace template_ {
 | 
			
		||||
static const char *const TAG = "template.number";
 | 
			
		||||
 | 
			
		||||
void TemplateNumber::setup() {
 | 
			
		||||
  if (this->f_.has_value() || !this->optimistic_)
 | 
			
		||||
  if (this->f_.has_value())
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
 | 
			
		||||
  float value;
 | 
			
		||||
  if (!this->pref_.load(&value)) {
 | 
			
		||||
    if (!isnan(this->initial_value_))
 | 
			
		||||
      value = this->initial_value_;
 | 
			
		||||
    else
 | 
			
		||||
      value = this->traits.get_min_value();
 | 
			
		||||
  if (!this->restore_value_) {
 | 
			
		||||
    value = this->initial_value_;
 | 
			
		||||
  } else {
 | 
			
		||||
    this->pref_ = global_preferences.make_preference<float>(this->get_object_id_hash());
 | 
			
		||||
    if (!this->pref_.load(&value)) {
 | 
			
		||||
      if (!isnan(this->initial_value_))
 | 
			
		||||
        value = this->initial_value_;
 | 
			
		||||
      else
 | 
			
		||||
        value = this->traits.get_min_value();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
}
 | 
			
		||||
@@ -35,10 +39,11 @@ void TemplateNumber::update() {
 | 
			
		||||
void TemplateNumber::control(float value) {
 | 
			
		||||
  this->set_trigger_->trigger(value);
 | 
			
		||||
 | 
			
		||||
  if (this->optimistic_) {
 | 
			
		||||
  if (this->optimistic_)
 | 
			
		||||
    this->publish_state(value);
 | 
			
		||||
 | 
			
		||||
  if (this->restore_value_)
 | 
			
		||||
    this->pref_.save(&value);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void TemplateNumber::dump_config() {
 | 
			
		||||
  LOG_NUMBER("", "Template Number", this);
 | 
			
		||||
 
 | 
			
		||||
@@ -20,11 +20,13 @@ class TemplateNumber : public number::Number, public PollingComponent {
 | 
			
		||||
  Trigger<float> *get_set_trigger() const { return set_trigger_; }
 | 
			
		||||
  void set_optimistic(bool optimistic) { optimistic_ = optimistic; }
 | 
			
		||||
  void set_initial_value(float initial_value) { initial_value_ = initial_value; }
 | 
			
		||||
  void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void control(float value) override;
 | 
			
		||||
  bool optimistic_{false};
 | 
			
		||||
  float initial_value_{NAN};
 | 
			
		||||
  bool restore_value_{false};
 | 
			
		||||
  Trigger<float> *set_trigger_ = new Trigger<float>();
 | 
			
		||||
  optional<std::function<optional<float>()>> f_;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
"""Constants used by esphome."""
 | 
			
		||||
 | 
			
		||||
__version__ = "1.20.0b4"
 | 
			
		||||
__version__ = "1.20.0b5"
 | 
			
		||||
 | 
			
		||||
ESP_PLATFORM_ESP32 = "ESP32"
 | 
			
		||||
ESP_PLATFORM_ESP8266 = "ESP8266"
 | 
			
		||||
 
 | 
			
		||||
@@ -11,4 +11,4 @@ ifaddr==0.1.7
 | 
			
		||||
platformio==5.1.1
 | 
			
		||||
esptool==2.8
 | 
			
		||||
click==7.1.2
 | 
			
		||||
esphome-dashboard==20210623.0
 | 
			
		||||
esphome-dashboard==20210719.0
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user