1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-25 06:32:22 +01: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

View File

@@ -0,0 +1,111 @@
#include "ms5611.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ms5611 {
static const char *TAG = "ms5611";
static const uint8_t MS5611_ADDRESS = 0x77;
static const uint8_t MS5611_CMD_ADC_READ = 0x00;
static const uint8_t MS5611_CMD_RESET = 0x1E;
static const uint8_t MS5611_CMD_CONV_D1 = 0x40;
static const uint8_t MS5611_CMD_CONV_D2 = 0x50;
static const uint8_t MS5611_CMD_READ_PROM = 0xA2;
void MS5611Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up MS5611...");
if (!this->write_bytes(MS5611_CMD_RESET, nullptr, 0)) {
this->mark_failed();
return;
}
delay(100);
for (uint8_t offset = 0; offset < 6; offset++) {
if (!this->read_byte_16(MS5611_CMD_READ_PROM + (offset * 2), &this->prom_[offset])) {
this->mark_failed();
return;
}
}
}
void MS5611Component::dump_config() {
ESP_LOGCONFIG(TAG, "MS5611:");
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, "Communication with MS5611 failed!");
}
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
}
float MS5611Component::get_setup_priority() const { return setup_priority::DATA; }
void MS5611Component::update() {
// request temperature reading
if (!this->write_bytes(MS5611_CMD_CONV_D2 + 0x08, nullptr, 0)) {
this->status_set_warning();
return;
}
auto f = std::bind(&MS5611Component::read_temperature_, this);
this->set_timeout("temperature", 10, f);
}
void MS5611Component::read_temperature_() {
uint8_t bytes[3];
if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
this->status_set_warning();
return;
}
const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
// request pressure reading
if (!this->write_bytes(MS5611_CMD_CONV_D1 + 0x08, nullptr, 0)) {
this->status_set_warning();
return;
}
auto f = std::bind(&MS5611Component::read_pressure_, this, raw_temperature);
this->set_timeout("pressure", 10, f);
}
void MS5611Component::read_pressure_(uint32_t raw_temperature) {
uint8_t bytes[3];
if (!this->read_bytes(MS5611_CMD_ADC_READ, bytes, 3)) {
this->status_set_warning();
return;
}
const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]));
this->calculate_values_(raw_temperature, raw_pressure);
}
void MS5611Component::calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure) {
const int32_t d_t = int32_t(raw_temperature) - (uint32_t(this->prom_[4]) << 8);
float temperature = (2000 + (int64_t(d_t) * this->prom_[5]) / 8388608.0f) / 100.0f;
float pressure_offset = (uint32_t(this->prom_[1]) << 16) + ((this->prom_[3] * d_t) >> 7);
float pressure_sensitivity = (uint32_t(this->prom_[0]) << 15) + ((this->prom_[2] * d_t) >> 8);
if (temperature < 20.0f) {
const float t2 = (d_t * d_t) / 2147483648.0f;
const float temp20 = (temperature - 20.0f) * 100.0f;
float pressure_offset_2 = 2.5f * temp20 * temp20;
float pressure_sensitivity_2 = 1.25f * temp20 * temp20;
if (temp20 < -15.0f) {
const float temp15 = (temperature + 15.0f) * 100.0f;
pressure_offset_2 += 7.0f * temp15;
pressure_sensitivity_2 += 5.5f * temp15;
}
temperature -= t2;
pressure_offset -= pressure_offset_2;
pressure_sensitivity -= pressure_sensitivity_2;
}
const float pressure = ((raw_pressure * pressure_sensitivity) / 2097152.0f - pressure_offset) / 3276800.0f;
ESP_LOGD(TAG, "Got temperature=%0.02f°C pressure=%0.01fhPa", temperature, pressure);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temperature);
if (this->pressure_sensor_ != nullptr)
this->pressure_sensor_->publish_state(pressure); // hPa
this->status_clear_warning();
}
} // namespace ms5611
} // namespace esphome

View File

@@ -0,0 +1,32 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace ms5611 {
class MS5611Component : public PollingComponent, public i2c::I2CDevice {
public:
MS5611Component(uint32_t update_interval) : PollingComponent(update_interval) {}
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
void update() override;
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
protected:
void read_temperature_();
void read_pressure_(uint32_t raw_temperature);
void calculate_values_(uint32_t raw_temperature, uint32_t raw_pressure);
sensor::Sensor *temperature_sensor_;
sensor::Sensor *pressure_sensor_;
uint16_t prom_[6];
};
} // namespace ms5611
} // namespace esphome

View File

@@ -0,0 +1,33 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import CONF_ID, CONF_PRESSURE, \
CONF_TEMPERATURE, CONF_UPDATE_INTERVAL, ICON_THERMOMETER, UNIT_CELSIUS, ICON_GAUGE, \
UNIT_HECTOPASCAL
DEPENDENCIES = ['i2c']
ms5611_ns = cg.esphome_ns.namespace('ms5611')
MS5611Component = ms5611_ns.class_('MS5611Component', cg.PollingComponent, i2c.I2CDevice)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_variable_id(MS5611Component),
cv.Required(CONF_TEMPERATURE): cv.nameable(
sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1)),
cv.Required(CONF_PRESSURE): cv.nameable(sensor.sensor_schema(UNIT_HECTOPASCAL, ICON_GAUGE, 1)),
cv.Optional(CONF_UPDATE_INTERVAL, default='60s'): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x77))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_UPDATE_INTERVAL])
yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config)
if CONF_TEMPERATURE in config:
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature_sensor(sens))
if CONF_PRESSURE in config:
sens = yield sensor.new_sensor(config[CONF_PRESSURE])
cg.add(var.set_pressure_sensor(sens))