mirror of
https://github.com/esphome/esphome.git
synced 2025-09-03 11:52:20 +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:
93
esphome/components/deep_sleep/deep_sleep_component.cpp
Normal file
93
esphome/components/deep_sleep/deep_sleep_component.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "deep_sleep_component.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/application.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace deep_sleep {
|
||||
|
||||
static const char *TAG = "deep_sleep";
|
||||
|
||||
bool global_has_deep_sleep = false;
|
||||
|
||||
void DeepSleepComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
|
||||
global_has_deep_sleep = true;
|
||||
|
||||
if (this->run_duration_.has_value())
|
||||
this->set_timeout(*this->run_duration_, [this]() { this->begin_sleep(); });
|
||||
}
|
||||
void DeepSleepComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
|
||||
if (this->sleep_duration_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Sleep Duration: %llu ms", *this->sleep_duration_ / 1000);
|
||||
}
|
||||
if (this->run_duration_.has_value()) {
|
||||
ESP_LOGCONFIG(TAG, " Run Duration: %u ms", *this->run_duration_);
|
||||
}
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
if (this->wakeup_pin_.has_value()) {
|
||||
LOG_PIN(" Wakeup Pin: ", *this->wakeup_pin_);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void DeepSleepComponent::loop() {
|
||||
if (this->next_enter_deep_sleep_)
|
||||
this->begin_sleep();
|
||||
}
|
||||
float DeepSleepComponent::get_loop_priority() const {
|
||||
return -100.0f; // run after everything else is ready
|
||||
}
|
||||
void DeepSleepComponent::set_sleep_duration(uint32_t time_ms) { this->sleep_duration_ = uint64_t(time_ms) * 1000; }
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
void DeepSleepComponent::set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode) {
|
||||
this->wakeup_pin_mode_ = wakeup_pin_mode;
|
||||
}
|
||||
void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
|
||||
#endif
|
||||
void DeepSleepComponent::set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
|
||||
void DeepSleepComponent::begin_sleep(bool manual) {
|
||||
if (this->prevent_ && !manual) {
|
||||
this->next_enter_deep_sleep_ = true;
|
||||
return;
|
||||
}
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_.has_value() &&
|
||||
!this->sleep_duration_.has_value() && (*this->wakeup_pin_)->digital_read()) {
|
||||
// Defer deep sleep until inactive
|
||||
if (!this->next_enter_deep_sleep_) {
|
||||
this->status_set_warning();
|
||||
ESP_LOGW(TAG, "Waiting for pin_ to switch state to enter deep sleep...");
|
||||
}
|
||||
this->next_enter_deep_sleep_ = true;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
ESP_LOGI(TAG, "Beginning Deep Sleep");
|
||||
|
||||
App.run_safe_shutdown_hooks();
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
if (this->sleep_duration_.has_value())
|
||||
esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
|
||||
if (this->wakeup_pin_.has_value()) {
|
||||
bool level = !(*this->wakeup_pin_)->is_inverted();
|
||||
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && (*this->wakeup_pin_)->digital_read())
|
||||
level = !level;
|
||||
esp_sleep_enable_ext0_wakeup(gpio_num_t((*this->wakeup_pin_)->get_pin()), level);
|
||||
}
|
||||
if (this->ext1_wakeup_.has_value()) {
|
||||
esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
|
||||
}
|
||||
esp_deep_sleep_start();
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
ESP.deepSleep(*this->sleep_duration_);
|
||||
#endif
|
||||
}
|
||||
float DeepSleepComponent::get_setup_priority() const { return -100.0f; }
|
||||
void DeepSleepComponent::prevent_deep_sleep() { this->prevent_ = true; }
|
||||
|
||||
} // namespace deep_sleep
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user