🏗 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
+133
View File
@@ -0,0 +1,133 @@
#include "ina3221.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ina3221 {
static const char *TAG = "ina3221";
static const uint8_t INA3221_REGISTER_CONFIG = 0x00;
static const uint8_t INA3221_REGISTER_CHANNEL1_SHUNT_VOLTAGE = 0x01;
static const uint8_t INA3221_REGISTER_CHANNEL1_BUS_VOLTAGE = 0x02;
static const uint8_t INA3221_REGISTER_CHANNEL2_SHUNT_VOLTAGE = 0x03;
static const uint8_t INA3221_REGISTER_CHANNEL2_BUS_VOLTAGE = 0x04;
static const uint8_t INA3221_REGISTER_CHANNEL3_SHUNT_VOLTAGE = 0x05;
static const uint8_t INA3221_REGISTER_CHANNEL3_BUS_VOLTAGE = 0x06;
// Addresses:
// A0 = GND -> 0x40
// A0 = VS -> 0x41
// A0 = SDA -> 0x42
// A0 = SCL -> 0x43
void INA3221Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up INA3221...");
// Config Register
// 0bx000000000000000 << 15 RESET Bit (1 -> trigger reset)
if (!this->write_byte_16(INA3221_REGISTER_CONFIG, 0x8000)) {
this->mark_failed();
return;
}
delay(1);
uint16_t config = 0;
// 0b0xxx000000000000 << 12 Channel Enables (1 -> ON)
if (this->channels_[0].exists()) {
config |= 0b0100000000000000;
}
if (this->channels_[1].exists()) {
config |= 0b0010000000000000;
}
if (this->channels_[2].exists()) {
config |= 0b0001000000000000;
}
// 0b0000xxx000000000 << 9 Averaging Mode (0 -> 1 sample, 111 -> 1024 samples)
config |= 0b0000111000000000;
// 0b0000000xxx000000 << 6 Bus Voltage Conversion time (100 -> 1.1ms, 111 -> 8.244 ms)
config |= 0b0000000111000000;
// 0b0000000000xxx000 << 3 Shunt Voltage Conversion time (same as above)
config |= 0b0000000000111000;
// 0b0000000000000xxx << 0 Operating mode (111 -> Shunt and bus, continuous)
config |= 0b0000000000000111;
if (!this->write_byte_16(INA3221_REGISTER_CONFIG, config)) {
this->mark_failed();
return;
}
}
void INA3221Component::dump_config() {
ESP_LOGCONFIG(TAG, "INA3221:");
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, "Communication with INA3221 failed!");
}
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Bus Voltage #1", this->channels_[0].bus_voltage_sensor_);
LOG_SENSOR(" ", "Shunt Voltage #1", this->channels_[0].shunt_voltage_sensor_);
LOG_SENSOR(" ", "Current #1", this->channels_[0].current_sensor_);
LOG_SENSOR(" ", "Power #1", this->channels_[0].power_sensor_);
LOG_SENSOR(" ", "Bus Voltage #2", this->channels_[1].bus_voltage_sensor_);
LOG_SENSOR(" ", "Shunt Voltage #2", this->channels_[1].shunt_voltage_sensor_);
LOG_SENSOR(" ", "Current #2", this->channels_[1].current_sensor_);
LOG_SENSOR(" ", "Power #2", this->channels_[1].power_sensor_);
LOG_SENSOR(" ", "Bus Voltage #3", this->channels_[2].bus_voltage_sensor_);
LOG_SENSOR(" ", "Shunt Voltage #3", this->channels_[2].shunt_voltage_sensor_);
LOG_SENSOR(" ", "Current #3", this->channels_[2].current_sensor_);
LOG_SENSOR(" ", "Power #3", this->channels_[2].power_sensor_);
}
inline uint8_t ina3221_bus_voltage_register(int channel) { return 0x02 + channel * 2; }
inline uint8_t ina3221_shunt_voltage_register(int channel) { return 0x01 + channel * 2; }
void INA3221Component::update() {
for (int i = 0; i < 3; i++) {
INA3221Channel &channel = this->channels_[i];
float bus_voltage_v = NAN, current_a = NAN;
uint16_t raw;
if (channel.should_measure_bus_voltage()) {
if (!this->read_byte_16(ina3221_bus_voltage_register(i), &raw, 1)) {
this->status_set_warning();
return;
}
bus_voltage_v = int16_t(raw) / 1000.0f;
if (channel.bus_voltage_sensor_ != nullptr)
channel.bus_voltage_sensor_->publish_state(bus_voltage_v);
}
if (channel.should_measure_shunt_voltage()) {
if (!this->read_byte_16(ina3221_shunt_voltage_register(i), &raw, 1)) {
this->status_set_warning();
return;
}
const float shunt_voltage_v = int16_t(raw) * 40.0f / 1000000.0f;
if (channel.shunt_voltage_sensor_ != nullptr)
channel.shunt_voltage_sensor_->publish_state(shunt_voltage_v);
current_a = shunt_voltage_v / channel.shunt_resistance_;
if (channel.current_sensor_ != nullptr)
channel.current_sensor_->publish_state(current_a);
}
if (channel.power_sensor_ != nullptr) {
channel.power_sensor_->publish_state(bus_voltage_v * current_a);
}
}
}
float INA3221Component::get_setup_priority() const { return setup_priority::DATA; }
void INA3221Component::set_shunt_resistance(int channel, float resistance_ohm) {
this->channels_[channel].shunt_resistance_ = resistance_ohm;
}
bool INA3221Component::INA3221Channel::exists() {
return this->bus_voltage_sensor_ != nullptr || this->shunt_voltage_sensor_ != nullptr ||
this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
}
bool INA3221Component::INA3221Channel::should_measure_shunt_voltage() {
return this->shunt_voltage_sensor_ != nullptr || this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
}
bool INA3221Component::INA3221Channel::should_measure_bus_voltage() {
return this->bus_voltage_sensor_ != nullptr || this->power_sensor_ != nullptr;
}
} // namespace ina3221
} // namespace esphome
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace ina3221 {
class INA3221Component : public PollingComponent, public i2c::I2CDevice {
public:
INA3221Component(uint32_t update_interval) : PollingComponent(update_interval) {}
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override;
void set_bus_voltage_sensor(int channel, sensor::Sensor *obj) { this->channels_[channel].bus_voltage_sensor_ = obj; }
void set_shunt_voltage_sensor(int channel, sensor::Sensor *obj) {
this->channels_[channel].shunt_voltage_sensor_ = obj;
}
void set_current_sensor(int channel, sensor::Sensor *obj) { this->channels_[channel].current_sensor_ = obj; }
void set_power_sensor(int channel, sensor::Sensor *obj) { this->channels_[channel].power_sensor_ = obj; }
void set_shunt_resistance(int channel, float resistance_ohm);
protected:
struct INA3221Channel {
float shunt_resistance_{0.1f};
sensor::Sensor *bus_voltage_sensor_{nullptr};
sensor::Sensor *shunt_voltage_sensor_{nullptr};
sensor::Sensor *current_sensor_{nullptr};
sensor::Sensor *power_sensor_{nullptr};
bool exists();
bool should_measure_shunt_voltage();
bool should_measure_bus_voltage();
} channels_[3];
};
} // namespace ina3221
} // namespace esphome
+58
View File
@@ -0,0 +1,58 @@
# coding=utf-8
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import CONF_BUS_VOLTAGE, CONF_CURRENT, CONF_ID, CONF_POWER, \
CONF_SHUNT_RESISTANCE, CONF_SHUNT_VOLTAGE, CONF_UPDATE_INTERVAL, ICON_FLASH, \
UNIT_VOLT, UNIT_AMPERE, UNIT_WATT
DEPENDENCIES = ['i2c']
CONF_CHANNEL_1 = 'channel_1'
CONF_CHANNEL_2 = 'channel_2'
CONF_CHANNEL_3 = 'channel_3'
ina3221_ns = cg.esphome_ns.namespace('ina3221')
INA3221Component = ina3221_ns.class_('INA3221Component', cg.PollingComponent, i2c.I2CDevice)
INA3221_CHANNEL_SCHEMA = cv.Schema({
cv.Optional(CONF_BUS_VOLTAGE): cv.nameable(sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 2)),
cv.Optional(CONF_SHUNT_VOLTAGE): cv.nameable(sensor.sensor_schema(UNIT_VOLT, ICON_FLASH, 2)),
cv.Optional(CONF_CURRENT): cv.nameable(sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2)),
cv.Optional(CONF_POWER): cv.nameable(sensor.sensor_schema(UNIT_WATT, ICON_FLASH, 2)),
cv.Optional(CONF_SHUNT_RESISTANCE, default=0.1): cv.All(cv.resistance,
cv.Range(min=0.0, max=32.0)),
})
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_variable_id(INA3221Component),
cv.Optional(CONF_CHANNEL_1): INA3221_CHANNEL_SCHEMA,
cv.Optional(CONF_CHANNEL_2): INA3221_CHANNEL_SCHEMA,
cv.Optional(CONF_CHANNEL_3): INA3221_CHANNEL_SCHEMA,
cv.Optional(CONF_UPDATE_INTERVAL, default='60s'): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x40))
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)
for i, channel in enumerate([CONF_CHANNEL_1, CONF_CHANNEL_2, CONF_CHANNEL_3]):
if channel not in config:
continue
conf = config[channel]
if CONF_SHUNT_RESISTANCE in conf:
cg.add(var.set_shunt_resistance(i, conf[CONF_SHUNT_RESISTANCE]))
if CONF_BUS_VOLTAGE in conf:
sens = yield sensor.new_sensor(conf[CONF_BUS_VOLTAGE])
cg.add(var.set_bus_voltage_sensor(i, sens))
if CONF_SHUNT_VOLTAGE in conf:
sens = yield sensor.new_sensor(conf[CONF_SHUNT_VOLTAGE])
cg.add(var.set_shunt_voltage_sensor(i, sens))
if CONF_CURRENT in conf:
sens = yield sensor.new_sensor(conf[CONF_CURRENT])
cg.add(var.set_current_sensor(i, sens))
if CONF_POWER in conf:
sens = yield sensor.new_sensor(conf[CONF_POWER])
cg.add(var.set_power_sensor(i, sens))