mirror of
https://github.com/esphome/esphome.git
synced 2025-09-20 12:12:24 +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:
113
esphome/core/application.cpp
Normal file
113
esphome/core/application.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_STATUS_LED
|
||||
#include "esphome/components/status_led/status_led.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
|
||||
static const char *TAG = "app";
|
||||
|
||||
void Application::register_component_(Component *comp) {
|
||||
if (comp == nullptr) {
|
||||
ESP_LOGW(TAG, "Tried to register null component!");
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto *c : this->components_) {
|
||||
if (comp == c) {
|
||||
ESP_LOGW(TAG, "Component already registered! (%p)", c);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->components_.push_back(comp);
|
||||
}
|
||||
void Application::setup() {
|
||||
ESP_LOGI(TAG, "Running through setup()...");
|
||||
ESP_LOGV(TAG, "Sorting components by setup priority...");
|
||||
std::stable_sort(this->components_.begin(), this->components_.end(), [](const Component *a, const Component *b) {
|
||||
return a->get_actual_setup_priority() > b->get_actual_setup_priority();
|
||||
});
|
||||
|
||||
for (uint32_t i = 0; i < this->components_.size(); i++) {
|
||||
Component *component = this->components_[i];
|
||||
if (component->is_failed())
|
||||
continue;
|
||||
|
||||
component->call_setup();
|
||||
if (component->can_proceed())
|
||||
continue;
|
||||
|
||||
std::stable_sort(this->components_.begin(), this->components_.begin() + i + 1,
|
||||
[](Component *a, Component *b) { return a->get_loop_priority() > b->get_loop_priority(); });
|
||||
|
||||
do {
|
||||
uint32_t new_app_state = STATUS_LED_WARNING;
|
||||
for (uint32_t j = 0; j <= i; j++) {
|
||||
if (!this->components_[j]->is_failed()) {
|
||||
this->components_[j]->call_loop();
|
||||
}
|
||||
new_app_state |= this->components_[j]->get_component_state();
|
||||
this->app_state_ |= new_app_state;
|
||||
}
|
||||
this->app_state_ = new_app_state;
|
||||
yield();
|
||||
} while (!component->can_proceed());
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "setup() finished successfully!");
|
||||
this->dump_config();
|
||||
}
|
||||
void Application::dump_config() {
|
||||
ESP_LOGI(TAG, "esphome-core version " ESPHOME_VERSION " compiled on %s", this->compilation_time_.c_str());
|
||||
|
||||
for (auto component : this->components_) {
|
||||
component->dump_config();
|
||||
}
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR HOT Application::feed_wdt() {
|
||||
static uint32_t LAST_FEED = 0;
|
||||
uint32_t now = millis();
|
||||
if (now - LAST_FEED > 3) {
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
ESP.wdtFeed();
|
||||
#endif
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
yield();
|
||||
#endif
|
||||
LAST_FEED = now;
|
||||
#ifdef USE_STATUS_LED
|
||||
if (status_led::global_status_led != nullptr) {
|
||||
status_led::global_status_led->call_loop();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
void Application::reboot() {
|
||||
ESP_LOGI(TAG, "Forcing a reboot...");
|
||||
for (auto *comp : this->components_)
|
||||
comp->on_shutdown();
|
||||
ESP.restart();
|
||||
// restart() doesn't always end execution
|
||||
while (true) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
void Application::safe_reboot() {
|
||||
ESP_LOGI(TAG, "Rebooting safely...");
|
||||
for (auto *comp : this->components_)
|
||||
comp->on_safe_shutdown();
|
||||
for (auto *comp : this->components_)
|
||||
comp->on_shutdown();
|
||||
ESP.restart();
|
||||
// restart() doesn't always end execution
|
||||
while (true) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
Application App;
|
||||
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user