1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 07:02:21 +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

@@ -1,65 +1,57 @@
import voluptuous as vol
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import mqtt
from esphome.components.mqtt import setup_mqtt_component
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_MQTT_ID, CONF_ON_VALUE, \
CONF_TRIGGER_ID
from esphome.core import CORE
from esphome.cpp_generator import Pvariable, add
from esphome.cpp_types import App, Nameable, Trigger, esphome_ns, std_string, Action
from esphome.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_ON_VALUE, \
CONF_TRIGGER_ID, CONF_MQTT_ID
from esphome.core import CORE, coroutine
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
})
IS_PLATFORM_COMPONENT = True
# pylint: disable=invalid-name
text_sensor_ns = esphome_ns.namespace('text_sensor')
TextSensor = text_sensor_ns.class_('TextSensor', Nameable)
text_sensor_ns = cg.esphome_ns.namespace('text_sensor')
TextSensor = text_sensor_ns.class_('TextSensor', cg.Nameable)
TextSensorPtr = TextSensor.operator('ptr')
MQTTTextSensor = text_sensor_ns.class_('MQTTTextSensor', mqtt.MQTTComponent)
TextSensorStateTrigger = text_sensor_ns.class_('TextSensorStateTrigger',
Trigger.template(std_string))
TextSensorPublishAction = text_sensor_ns.class_('TextSensorPublishAction', Action)
cg.Trigger.template(cg.std_string))
TextSensorPublishAction = text_sensor_ns.class_('TextSensorPublishAction', cg.Action)
icon = cv.icon
TEXT_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({
cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTTextSensor),
vol.Optional(CONF_ICON): cv.icon,
vol.Optional(CONF_ON_VALUE): automation.validate_automation({
cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_variable_id(mqtt.MQTTTextSensor),
cv.Optional(CONF_ICON): icon,
cv.Optional(CONF_ON_VALUE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(TextSensorStateTrigger),
}),
})
TEXT_SENSOR_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(TEXT_SENSOR_SCHEMA.schema)
def setup_text_sensor_core_(text_sensor_var, config):
@coroutine
def setup_text_sensor_core_(var, config):
if CONF_INTERNAL in config:
add(text_sensor_var.set_internal(config[CONF_INTERNAL]))
cg.add(var.set_internal(config[CONF_INTERNAL]))
if CONF_ICON in config:
add(text_sensor_var.set_icon(config[CONF_ICON]))
cg.add(var.set_icon(config[CONF_ICON]))
for conf in config.get(CONF_ON_VALUE, []):
rhs = text_sensor_var.make_state_trigger()
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
automation.build_automations(trigger, [(std_string, 'x')], conf)
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
yield automation.build_automation(trigger, [(cg.std_string, 'x')], conf)
setup_mqtt_component(text_sensor_var.Pget_mqtt(), config)
def setup_text_sensor(text_sensor_obj, config):
if not CORE.has_id(config[CONF_ID]):
text_sensor_obj = Pvariable(config[CONF_ID], text_sensor_obj, has_side_effects=True)
CORE.add_job(setup_text_sensor_core_, text_sensor_obj, config)
if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
yield mqtt.register_mqtt_component(mqtt_, config)
@coroutine
def register_text_sensor(var, config):
if not CORE.has_id(config[CONF_ID]):
var = Pvariable(config[CONF_ID], var, has_side_effects=True)
add(App.register_text_sensor(var))
CORE.add_job(setup_text_sensor_core_, var, config)
var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_text_sensor(var))
yield setup_text_sensor_core_(var, config)
BUILD_FLAGS = '-DUSE_TEXT_SENSOR'
def to_code(config):
cg.add_define('USE_TEXT_SENSOR')
cg.add_global(text_sensor_ns.using)

View File

@@ -0,0 +1,10 @@
#include "automation.h"
#include "esphome/core/log.h"
namespace esphome {
namespace text_sensor {
static const char *TAG = "text_sensor.automation";
} // namespace text_sensor
} // namespace esphome

View File

@@ -0,0 +1,31 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/text_sensor/text_sensor.h"
namespace esphome {
namespace text_sensor {
class TextSensorStateTrigger : public Trigger<std::string> {
public:
explicit TextSensorStateTrigger(TextSensor *parent) {
parent->add_on_state_callback([this](std::string value) { this->trigger(value); });
}
};
template<typename... Ts> class TextSensorPublishAction : public Action<Ts...> {
public:
TextSensorPublishAction(TextSensor *sensor) : sensor_(sensor) {}
TEMPLATABLE_VALUE(std::string, state)
void play(Ts... x) override {
this->sensor_->publish_state(this->state_.value(x...));
this->play_next(x...);
}
protected:
TextSensor *sensor_;
};
} // namespace text_sensor
} // namespace esphome

View File

@@ -1,33 +0,0 @@
import voluptuous as vol
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_TEXT_SENSORS
from esphome.cpp_generator import add, process_lambda, variable
from esphome.cpp_types import std_vector
CustomTextSensorConstructor = text_sensor.text_sensor_ns.class_('CustomTextSensorConstructor')
PLATFORM_SCHEMA = text_sensor.PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(CustomTextSensorConstructor),
vol.Required(CONF_LAMBDA): cv.lambda_,
vol.Required(CONF_TEXT_SENSORS):
cv.ensure_list(text_sensor.TEXT_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(text_sensor.TextSensor),
})),
})
def to_code(config):
template_ = yield process_lambda(config[CONF_LAMBDA], [],
return_type=std_vector.template(text_sensor.TextSensorPtr))
rhs = CustomTextSensorConstructor(template_)
custom = variable(config[CONF_ID], rhs)
for i, conf in enumerate(config[CONF_TEXT_SENSORS]):
rhs = custom.Pget_text_sensor(i)
add(rhs.set_name(conf[CONF_NAME]))
text_sensor.register_text_sensor(rhs, conf)
BUILD_FLAGS = '-DUSE_CUSTOM_TEXT_SENSOR'

View File

@@ -1,26 +0,0 @@
import voluptuous as vol
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ENTITY_ID, CONF_ID, CONF_NAME
from esphome.cpp_generator import Pvariable
from esphome.cpp_types import App, Component
DEPENDENCIES = ['api']
HomeassistantTextSensor = text_sensor.text_sensor_ns.class_('HomeassistantTextSensor',
text_sensor.TextSensor, Component)
PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(HomeassistantTextSensor),
vol.Required(CONF_ENTITY_ID): cv.entity_id,
}))
def to_code(config):
rhs = App.make_homeassistant_text_sensor(config[CONF_NAME], config[CONF_ENTITY_ID])
sensor_ = Pvariable(config[CONF_ID], rhs)
text_sensor.setup_text_sensor(sensor_, config)
BUILD_FLAGS = '-DUSE_HOMEASSISTANT_TEXT_SENSOR'

View File

@@ -1,33 +0,0 @@
import voluptuous as vol
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_NAME, CONF_QOS, CONF_TOPIC
from esphome.cpp_generator import Pvariable, add
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, Component
DEPENDENCIES = ['mqtt']
MQTTSubscribeTextSensor = text_sensor.text_sensor_ns.class_('MQTTSubscribeTextSensor',
text_sensor.TextSensor, Component)
PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(MQTTSubscribeTextSensor),
vol.Required(CONF_TOPIC): cv.subscribe_topic,
vol.Optional(CONF_QOS): cv.mqtt_qos,
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.make_mqtt_subscribe_text_sensor(config[CONF_NAME], config[CONF_TOPIC])
sensor_ = Pvariable(config[CONF_ID], rhs)
if CONF_QOS in config:
add(sensor_.set_qos(config[CONF_QOS]))
text_sensor.setup_text_sensor(sensor_, config)
setup_component(sensor_, config)
BUILD_FLAGS = '-DUSE_MQTT_SUBSCRIBE_TEXT_SENSOR'

View File

@@ -1,52 +0,0 @@
import voluptuous as vol
from esphome.automation import ACTION_REGISTRY
from esphome.components import text_sensor
from esphome.components.text_sensor import TextSensorPublishAction
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_STATE, CONF_UPDATE_INTERVAL
from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, PollingComponent, optional, std_string
TemplateTextSensor = text_sensor.text_sensor_ns.class_('TemplateTextSensor',
text_sensor.TextSensor, PollingComponent)
PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(TemplateTextSensor),
vol.Optional(CONF_LAMBDA): cv.lambda_,
vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.make_template_text_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL))
template = Pvariable(config[CONF_ID], rhs)
text_sensor.setup_text_sensor(template, config)
setup_component(template, config)
if CONF_LAMBDA in config:
template_ = yield process_lambda(config[CONF_LAMBDA], [],
return_type=optional.template(std_string))
add(template.set_template(template_))
BUILD_FLAGS = '-DUSE_TEMPLATE_TEXT_SENSOR'
CONF_TEXT_SENSOR_TEMPLATE_PUBLISH = 'text_sensor.template.publish'
TEXT_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(text_sensor.TextSensor),
vol.Required(CONF_STATE): cv.templatable(cv.string_strict),
})
@ACTION_REGISTRY.register(CONF_TEXT_SENSOR_TEMPLATE_PUBLISH,
TEXT_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA)
def text_sensor_template_publish_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_text_sensor_publish_action(template_arg)
type = TextSensorPublishAction.template(template_arg)
action = Pvariable(action_id, rhs, type=type)
template_ = yield templatable(config[CONF_STATE], args, std_string)
add(action.set_state(template_))
yield action

View File

@@ -0,0 +1,33 @@
#include "text_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace text_sensor {
static const char *TAG = "text_sensor";
TextSensor::TextSensor() : TextSensor("") {}
TextSensor::TextSensor(const std::string &name) : Nameable(name) {}
void TextSensor::publish_state(std::string state) {
this->state = state;
this->has_state_ = true;
ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
this->callback_.call(state);
}
void TextSensor::set_icon(const std::string &icon) { this->icon_ = icon; }
void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
this->callback_.add(std::move(callback));
}
std::string TextSensor::get_icon() {
if (this->icon_.has_value())
return *this->icon_;
return this->icon();
}
std::string TextSensor::icon() { return ""; }
std::string TextSensor::unique_id() { return ""; }
bool TextSensor::has_state() { return this->has_state_; }
uint32_t TextSensor::hash_base() { return 334300109UL; }
} // namespace text_sensor
} // namespace esphome

View File

@@ -0,0 +1,52 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace text_sensor {
#define LOG_TEXT_SENSOR(prefix, type, obj) \
if (obj != nullptr) { \
ESP_LOGCONFIG(TAG, prefix type " '%s'", obj->get_name().c_str()); \
if (!obj->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, prefix " Icon: '%s'", obj->get_icon().c_str()); \
} \
if (!obj->unique_id().empty()) { \
ESP_LOGV(TAG, prefix " Unique ID: '%s'", obj->unique_id().c_str()); \
} \
}
class TextSensor : public Nameable {
public:
explicit TextSensor(const std::string &name);
explicit TextSensor();
void publish_state(std::string state);
void set_icon(const std::string &icon);
void add_on_state_callback(std::function<void(std::string)> callback);
std::string state;
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
std::string get_icon();
virtual std::string icon();
virtual std::string unique_id();
bool has_state();
protected:
uint32_t hash_base() override;
CallbackManager<void(std::string)> callback_;
optional<std::string> icon_;
bool has_state_{false};
};
} // namespace text_sensor
} // namespace esphome

View File

@@ -1,23 +0,0 @@
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_NAME
from esphome.cpp_generator import Pvariable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, Component
VersionTextSensor = text_sensor.text_sensor_ns.class_('VersionTextSensor',
text_sensor.TextSensor, Component)
PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(VersionTextSensor),
}).extend(cv.COMPONENT_SCHEMA.schema))
def to_code(config):
rhs = App.make_version_text_sensor(config[CONF_NAME])
sens = Pvariable(config[CONF_ID], rhs)
text_sensor.setup_text_sensor(sens, config)
setup_component(sens, config)
BUILD_FLAGS = '-DUSE_VERSION_TEXT_SENSOR'

View File

@@ -1,45 +0,0 @@
import voluptuous as vol
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_BSSID, CONF_ID, CONF_IP_ADDRESS, CONF_NAME, CONF_SSID
from esphome.cpp_generator import Pvariable
from esphome.cpp_types import App, Component
DEPENDENCIES = ['wifi']
IPAddressWiFiInfo = text_sensor.text_sensor_ns.class_('IPAddressWiFiInfo',
text_sensor.TextSensor, Component)
SSIDWiFiInfo = text_sensor.text_sensor_ns.class_('SSIDWiFiInfo',
text_sensor.TextSensor, Component)
BSSIDWiFiInfo = text_sensor.text_sensor_ns.class_('BSSIDWiFiInfo',
text_sensor.TextSensor, Component)
PLATFORM_SCHEMA = text_sensor.PLATFORM_SCHEMA.extend({
vol.Optional(CONF_IP_ADDRESS): cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(IPAddressWiFiInfo),
})),
vol.Optional(CONF_SSID): cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(SSIDWiFiInfo),
})),
vol.Optional(CONF_BSSID): cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(BSSIDWiFiInfo),
})),
})
def setup_conf(config, key, klass):
if key in config:
conf = config[key]
rhs = App.register_component(klass.new(conf[CONF_NAME]))
sensor_ = Pvariable(conf[CONF_ID], rhs)
text_sensor.register_text_sensor(sensor_, conf)
def to_code(config):
setup_conf(config, CONF_IP_ADDRESS, IPAddressWiFiInfo)
setup_conf(config, CONF_SSID, SSIDWiFiInfo)
setup_conf(config, CONF_BSSID, BSSIDWiFiInfo)
BUILD_FLAGS = '-DUSE_WIFI_INFO_TEXT_SENSOR'