1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-27 21:23:48 +00:00

🏗 Merge C++ into python codebase (#504)

## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
This commit is contained in:
Otto Winter
2019-04-17 12:06:00 +02:00
committed by GitHub
parent 049807e3ab
commit 6682c43dfa
817 changed files with 54156 additions and 10830 deletions

View File

@@ -0,0 +1,80 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_HIGH_VOLTAGE_REFERENCE, CONF_ID, CONF_IIR_FILTER, \
CONF_LOW_VOLTAGE_REFERENCE, CONF_MEASUREMENT_DURATION, CONF_SETUP_MODE, CONF_SLEEP_DURATION, \
CONF_VOLTAGE_ATTENUATION, ESP_PLATFORM_ESP32
from esphome.core import TimePeriod
AUTO_LOAD = ['binary_sensor']
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
esp32_touch_ns = cg.esphome_ns.namespace('esp32_touch')
ESP32TouchComponent = esp32_touch_ns.class_('ESP32TouchComponent', cg.Component)
def validate_voltage(values):
def validator(value):
if isinstance(value, float) and value.is_integer():
value = int(value)
value = cv.string(value)
if not value.endswith('V'):
value += 'V'
return cv.one_of(*values)(value)
return validator
LOW_VOLTAGE_REFERENCE = {
'0.5V': cg.global_ns.TOUCH_LVOLT_0V5,
'0.6V': cg.global_ns.TOUCH_LVOLT_0V6,
'0.7V': cg.global_ns.TOUCH_LVOLT_0V7,
'0.8V': cg.global_ns.TOUCH_LVOLT_0V8,
}
HIGH_VOLTAGE_REFERENCE = {
'2.4V': cg.global_ns.TOUCH_HVOLT_2V4,
'2.5V': cg.global_ns.TOUCH_HVOLT_2V5,
'2.6V': cg.global_ns.TOUCH_HVOLT_2V6,
'2.7V': cg.global_ns.TOUCH_HVOLT_2V7,
}
VOLTAGE_ATTENUATION = {
'1.5V': cg.global_ns.TOUCH_HVOLT_ATTEN_1V5,
'1V': cg.global_ns.TOUCH_HVOLT_ATTEN_1V,
'0.5V': cg.global_ns.TOUCH_HVOLT_ATTEN_0V5,
'0V': cg.global_ns.TOUCH_HVOLT_ATTEN_0V,
}
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_variable_id(ESP32TouchComponent),
cv.Optional(CONF_SETUP_MODE, default=False): cv.boolean,
cv.Optional(CONF_IIR_FILTER, default='0ms'): cv.positive_time_period_milliseconds,
cv.Optional(CONF_SLEEP_DURATION, default='27306us'):
cv.All(cv.positive_time_period, cv.Range(max=TimePeriod(microseconds=436906))),
cv.Optional(CONF_MEASUREMENT_DURATION, default='8192us'):
cv.All(cv.positive_time_period, cv.Range(max=TimePeriod(microseconds=8192))),
cv.Optional(CONF_LOW_VOLTAGE_REFERENCE, default='0.5V'):
validate_voltage(LOW_VOLTAGE_REFERENCE),
cv.Optional(CONF_HIGH_VOLTAGE_REFERENCE, default='2.7V'):
validate_voltage(HIGH_VOLTAGE_REFERENCE),
cv.Optional(CONF_VOLTAGE_ATTENUATION, default='0V'): validate_voltage(VOLTAGE_ATTENUATION),
}).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
touch = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(touch, config)
cg.add(touch.set_setup_mode(config[CONF_SETUP_MODE]))
cg.add(touch.set_iir_filter(config[CONF_IIR_FILTER]))
sleep_duration = int(round(config[CONF_SLEEP_DURATION].total_microseconds * 0.15))
cg.add(touch.set_sleep_duration(sleep_duration))
measurement_duration = int(round(config[CONF_MEASUREMENT_DURATION].total_microseconds *
7.99987793))
cg.add(touch.set_measurement_duration(measurement_duration))
cg.add(touch.set_low_voltage_reference(
LOW_VOLTAGE_REFERENCE[config[CONF_LOW_VOLTAGE_REFERENCE]]))
cg.add(touch.set_high_voltage_reference(
HIGH_VOLTAGE_REFERENCE[config[CONF_HIGH_VOLTAGE_REFERENCE]]))
cg.add(touch.set_voltage_attenuation(VOLTAGE_ATTENUATION[config[CONF_VOLTAGE_ATTENUATION]]))

View File

@@ -0,0 +1,50 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.components.esp32_touch import ESP32TouchComponent
from esphome.const import CONF_NAME, CONF_PIN, CONF_THRESHOLD, ESP_PLATFORM_ESP32, CONF_ID
from esphome.pins import validate_gpio_pin
from . import esp32_touch_ns
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
DEPENDENCIES = ['esp32_touch']
CONF_ESP32_TOUCH_ID = 'esp32_touch_id'
TOUCH_PADS = {
4: cg.global_ns.TOUCH_PAD_NUM0,
0: cg.global_ns.TOUCH_PAD_NUM1,
2: cg.global_ns.TOUCH_PAD_NUM2,
15: cg.global_ns.TOUCH_PAD_NUM3,
13: cg.global_ns.TOUCH_PAD_NUM4,
12: cg.global_ns.TOUCH_PAD_NUM5,
14: cg.global_ns.TOUCH_PAD_NUM6,
27: cg.global_ns.TOUCH_PAD_NUM7,
33: cg.global_ns.TOUCH_PAD_NUM8,
32: cg.global_ns.TOUCH_PAD_NUM9,
}
def validate_touch_pad(value):
value = validate_gpio_pin(value)
if value not in TOUCH_PADS:
raise cv.Invalid("Pin {} does not support touch pads.".format(value))
return value
ESP32TouchBinarySensor = esp32_touch_ns.class_('ESP32TouchBinarySensor', binary_sensor.BinarySensor)
CONFIG_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(ESP32TouchBinarySensor),
cv.GenerateID(CONF_ESP32_TOUCH_ID): cv.use_variable_id(ESP32TouchComponent),
cv.Required(CONF_PIN): validate_touch_pad,
cv.Required(CONF_THRESHOLD): cv.uint16_t,
}))
def to_code(config):
hub = yield cg.get_variable(config[CONF_ESP32_TOUCH_ID])
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], TOUCH_PADS[config[CONF_PIN]],
config[CONF_THRESHOLD])
yield binary_sensor.register_binary_sensor(var, config)
cg.add(hub.register_touch_pad(var))

View File

@@ -0,0 +1,164 @@
#include "esp32_touch.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace esp32_touch {
static const char *TAG = "esp32_touch";
void ESP32TouchComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up ESP32 Touch Hub...");
touch_pad_init();
if (this->iir_filter_enabled_()) {
touch_pad_filter_start(this->iir_filter_);
}
touch_pad_set_meas_time(this->sleep_cycle_, this->meas_cycle_);
touch_pad_set_voltage(this->high_voltage_reference_, this->low_voltage_reference_, this->voltage_attenuation_);
for (auto *child : this->children_) {
// Disable interrupt threshold
touch_pad_config(child->get_touch_pad(), 0);
}
}
void ESP32TouchComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Config for ESP32 Touch Hub:");
ESP_LOGCONFIG(TAG, " Meas cycle: %.2fms", this->meas_cycle_ / (8000000.0f / 1000.0f));
ESP_LOGCONFIG(TAG, " Sleep cycle: %.2fms", this->sleep_cycle_ / (150000.0f / 1000.0f));
const char *lv_s;
switch (this->low_voltage_reference_) {
case TOUCH_LVOLT_0V5:
lv_s = "0.5V";
break;
case TOUCH_LVOLT_0V6:
lv_s = "0.6V";
break;
case TOUCH_LVOLT_0V7:
lv_s = "0.7V";
break;
case TOUCH_LVOLT_0V8:
lv_s = "0.8V";
break;
default:
lv_s = "UNKNOWN";
break;
}
ESP_LOGCONFIG(TAG, " Low Voltage Reference: %s", lv_s);
const char *hv_s;
switch (this->high_voltage_reference_) {
case TOUCH_HVOLT_2V4:
hv_s = "2.4V";
break;
case TOUCH_HVOLT_2V5:
hv_s = "2.5V";
break;
case TOUCH_HVOLT_2V6:
hv_s = "2.6V";
break;
case TOUCH_HVOLT_2V7:
hv_s = "2.7V";
break;
default:
hv_s = "UNKNOWN";
break;
}
ESP_LOGCONFIG(TAG, " High Voltage Reference: %s", hv_s);
const char *atten_s;
switch (this->voltage_attenuation_) {
case TOUCH_HVOLT_ATTEN_1V5:
atten_s = "1.5V";
break;
case TOUCH_HVOLT_ATTEN_1V:
atten_s = "1V";
break;
case TOUCH_HVOLT_ATTEN_0V5:
atten_s = "0.5V";
break;
case TOUCH_HVOLT_ATTEN_0V:
atten_s = "0V";
break;
default:
atten_s = "UNKNOWN";
break;
}
ESP_LOGCONFIG(TAG, " Voltage Attenuation: %s", atten_s);
if (this->iir_filter_enabled_()) {
ESP_LOGCONFIG(TAG, " IIR Filter: %ums", this->iir_filter_);
touch_pad_filter_start(this->iir_filter_);
} else {
ESP_LOGCONFIG(TAG, " IIR Filter DISABLED");
}
if (this->setup_mode_) {
ESP_LOGCONFIG(TAG, " Setup Mode ENABLED!");
}
for (auto *child : this->children_) {
LOG_BINARY_SENSOR(" ", "Touch Pad", child);
ESP_LOGCONFIG(TAG, " Pad: T%d", child->get_touch_pad());
ESP_LOGCONFIG(TAG, " Threshold: %u", child->get_threshold());
}
}
void ESP32TouchComponent::loop() {
for (auto *child : this->children_) {
uint16_t value;
if (this->iir_filter_enabled_()) {
touch_pad_read_filtered(child->get_touch_pad(), &value);
} else {
touch_pad_read(child->get_touch_pad(), &value);
}
child->publish_state(value < child->get_threshold());
if (this->setup_mode_) {
ESP_LOGD(TAG, "Touch Pad '%s' (T%u): %u", child->get_name().c_str(), child->get_touch_pad(), value);
}
}
if (this->setup_mode_) {
// Avoid spamming logs
delay(250);
}
}
void ESP32TouchComponent::register_touch_pad(ESP32TouchBinarySensor *pad) { this->children_.push_back(pad); }
void ESP32TouchComponent::set_setup_mode(bool setup_mode) { this->setup_mode_ = setup_mode; }
bool ESP32TouchComponent::iir_filter_enabled_() const { return this->iir_filter_ > 0; }
void ESP32TouchComponent::set_iir_filter(uint32_t iir_filter) { this->iir_filter_ = iir_filter; }
float ESP32TouchComponent::get_setup_priority() const { return setup_priority::DATA; }
void ESP32TouchComponent::set_sleep_duration(uint16_t sleep_duration) { this->sleep_cycle_ = sleep_duration; }
void ESP32TouchComponent::set_measurement_duration(uint16_t meas_cycle) { this->meas_cycle_ = meas_cycle; }
void ESP32TouchComponent::set_low_voltage_reference(touch_low_volt_t low_voltage_reference) {
this->low_voltage_reference_ = low_voltage_reference;
}
void ESP32TouchComponent::set_high_voltage_reference(touch_high_volt_t high_voltage_reference) {
this->high_voltage_reference_ = high_voltage_reference;
}
void ESP32TouchComponent::set_voltage_attenuation(touch_volt_atten_t voltage_attenuation) {
this->voltage_attenuation_ = voltage_attenuation;
}
void ESP32TouchComponent::on_shutdown() {
if (this->iir_filter_enabled_()) {
touch_pad_filter_stop();
touch_pad_filter_delete();
}
touch_pad_deinit();
}
ESP32TouchBinarySensor::ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold)
: BinarySensor(name), touch_pad_(touch_pad), threshold_(threshold) {}
touch_pad_t ESP32TouchBinarySensor::get_touch_pad() const { return this->touch_pad_; }
uint16_t ESP32TouchBinarySensor::get_threshold() const { return this->threshold_; }
} // namespace esp32_touch
} // namespace esphome
#endif

View File

@@ -0,0 +1,70 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace esp32_touch {
class ESP32TouchBinarySensor;
class ESP32TouchComponent : public Component {
public:
void register_touch_pad(ESP32TouchBinarySensor *pad);
void set_setup_mode(bool setup_mode);
void set_iir_filter(uint32_t iir_filter);
void set_sleep_duration(uint16_t sleep_duration);
void set_measurement_duration(uint16_t meas_cycle);
void set_low_voltage_reference(touch_low_volt_t low_voltage_reference);
void set_high_voltage_reference(touch_high_volt_t high_voltage_reference);
void set_voltage_attenuation(touch_volt_atten_t voltage_attenuation);
void setup() override;
void dump_config() override;
void loop() override;
float get_setup_priority() const override;
void on_shutdown() override;
protected:
/// Is the IIR filter enabled?
bool iir_filter_enabled_() const;
uint16_t sleep_cycle_{};
uint16_t meas_cycle_{65535};
touch_low_volt_t low_voltage_reference_{};
touch_high_volt_t high_voltage_reference_{};
touch_volt_atten_t voltage_attenuation_{};
std::vector<ESP32TouchBinarySensor *> children_;
bool setup_mode_{false};
uint32_t iir_filter_{0};
};
/// Simple helper class to expose a touch pad value as a binary sensor.
class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
public:
ESP32TouchBinarySensor(const std::string &name, touch_pad_t touch_pad, uint16_t threshold);
touch_pad_t get_touch_pad() const;
uint16_t get_threshold() const;
protected:
friend ESP32TouchComponent;
touch_pad_t touch_pad_;
uint16_t threshold_;
};
} // namespace esp32_touch
} // namespace esphome
#endif