mirror of
https://github.com/esphome/esphome.git
synced 2025-09-29 00:22: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:
3
esphome/components/mqtt_subscribe/__init__.py
Normal file
3
esphome/components/mqtt_subscribe/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import esphome.codegen as cg
|
||||
|
||||
mqtt_subscribe_ns = cg.esphome_ns.namespace('mqtt_subscribe')
|
27
esphome/components/mqtt_subscribe/sensor/__init__.py
Normal file
27
esphome/components/mqtt_subscribe/sensor/__init__.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import mqtt, sensor
|
||||
from esphome.const import CONF_ID, CONF_NAME, CONF_QOS, CONF_TOPIC
|
||||
from .. import mqtt_subscribe_ns
|
||||
|
||||
DEPENDENCIES = ['mqtt']
|
||||
|
||||
CONF_MQTT_PARENT_ID = 'mqtt_parent_id'
|
||||
MQTTSubscribeSensor = mqtt_subscribe_ns.class_('MQTTSubscribeSensor', sensor.Sensor, cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.nameable(sensor.SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_variable_id(MQTTSubscribeSensor),
|
||||
cv.GenerateID(CONF_MQTT_PARENT_ID): cv.use_variable_id(mqtt.MQTTClientComponent),
|
||||
cv.Required(CONF_TOPIC): cv.subscribe_topic,
|
||||
cv.Optional(CONF_QOS, default=0): cv.mqtt_qos,
|
||||
}).extend(cv.COMPONENT_SCHEMA))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
parent = yield cg.get_variable(config[CONF_MQTT_PARENT_ID])
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], parent, config[CONF_TOPIC])
|
||||
yield cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_qos(config[CONF_QOS]))
|
||||
|
||||
yield sensor.register_sensor(var, config)
|
@@ -0,0 +1,32 @@
|
||||
#include "mqtt_subscribe_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt_subscribe {
|
||||
|
||||
static const char *TAG = "mqtt_subscribe.sensor";
|
||||
|
||||
void MQTTSubscribeSensor::setup() {
|
||||
mqtt::global_mqtt_client->subscribe(this->topic_,
|
||||
[this](const std::string &topic, std::string payload) {
|
||||
auto val = parse_float(payload);
|
||||
if (!val.has_value()) {
|
||||
ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
|
||||
this->publish_state(NAN);
|
||||
return;
|
||||
}
|
||||
|
||||
this->publish_state(*val);
|
||||
},
|
||||
this->qos_);
|
||||
}
|
||||
|
||||
float MQTTSubscribeSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
|
||||
void MQTTSubscribeSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
|
||||
void MQTTSubscribeSensor::dump_config() {
|
||||
LOG_SENSOR("", "MQTT Subscribe", this);
|
||||
ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
|
||||
}
|
||||
|
||||
} // namespace mqtt_subscribe
|
||||
} // namespace esphome
|
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/mqtt/mqtt_client.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt_subscribe {
|
||||
|
||||
class MQTTSubscribeSensor : public sensor::Sensor, public Component {
|
||||
public:
|
||||
MQTTSubscribeSensor(const std::string &name, mqtt::MQTTClientComponent *parent, const std::string &topic)
|
||||
: Sensor(name), parent_(parent), topic_(topic) {}
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
|
||||
void set_qos(uint8_t qos);
|
||||
|
||||
protected:
|
||||
mqtt::MQTTClientComponent *parent_;
|
||||
std::string topic_;
|
||||
uint8_t qos_{0};
|
||||
};
|
||||
|
||||
} // namespace mqtt_subscribe
|
||||
} // namespace esphome
|
28
esphome/components/mqtt_subscribe/text_sensor/__init__.py
Normal file
28
esphome/components/mqtt_subscribe/text_sensor/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import text_sensor, mqtt
|
||||
from esphome.const import CONF_ID, CONF_NAME, CONF_QOS, CONF_TOPIC
|
||||
from .. import mqtt_subscribe_ns
|
||||
|
||||
DEPENDENCIES = ['mqtt']
|
||||
|
||||
CONF_MQTT_PARENT_ID = 'mqtt_parent_id'
|
||||
MQTTSubscribeTextSensor = mqtt_subscribe_ns.class_('MQTTSubscribeTextSensor',
|
||||
text_sensor.TextSensor, cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_variable_id(MQTTSubscribeTextSensor),
|
||||
cv.GenerateID(CONF_MQTT_PARENT_ID): cv.use_variable_id(mqtt.MQTTClientComponent),
|
||||
cv.Required(CONF_TOPIC): cv.subscribe_topic,
|
||||
cv.Optional(CONF_QOS, default=0): cv.mqtt_qos,
|
||||
}).extend(cv.COMPONENT_SCHEMA))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
parent = yield cg.get_variable(config[CONF_MQTT_PARENT_ID])
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], parent, config[CONF_TOPIC])
|
||||
yield cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_qos(config[CONF_QOS]))
|
||||
|
||||
yield text_sensor.register_text_sensor(var, config)
|
@@ -0,0 +1,22 @@
|
||||
#include "mqtt_subscribe_text_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt_subscribe {
|
||||
|
||||
static const char *TAG = "mqtt_subscribe.text_sensor";
|
||||
|
||||
void MQTTSubscribeTextSensor::setup() {
|
||||
this->parent_->subscribe(this->topic_,
|
||||
[this](const std::string &topic, std::string payload) { this->publish_state(payload); },
|
||||
this->qos_);
|
||||
}
|
||||
float MQTTSubscribeTextSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
|
||||
void MQTTSubscribeTextSensor::set_qos(uint8_t qos) { this->qos_ = qos; }
|
||||
void MQTTSubscribeTextSensor::dump_config() {
|
||||
LOG_TEXT_SENSOR("", "MQTT Subscribe Text Sensor", this);
|
||||
ESP_LOGCONFIG(TAG, " Topic: %s", this->topic_.c_str());
|
||||
}
|
||||
|
||||
} // namespace mqtt_subscribe
|
||||
} // namespace esphome
|
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
#include "esphome/components/mqtt/mqtt_client.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt_subscribe {
|
||||
|
||||
class MQTTSubscribeTextSensor : public text_sensor::TextSensor, public Component {
|
||||
public:
|
||||
MQTTSubscribeTextSensor(const std::string &name, mqtt::MQTTClientComponent *parent, const std::string &topic)
|
||||
: TextSensor(name), parent_(parent), topic_(topic) {}
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void set_qos(uint8_t qos);
|
||||
|
||||
protected:
|
||||
mqtt::MQTTClientComponent *parent_;
|
||||
std::string topic_;
|
||||
uint8_t qos_{};
|
||||
};
|
||||
|
||||
} // namespace mqtt_subscribe
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user