mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 12:43:28 +02:00
6682c43dfa
## 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).
67 lines
2.1 KiB
C++
67 lines
2.1 KiB
C++
#include "template_switch.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace template_ {
|
|
|
|
static const char *TAG = "template.switch";
|
|
|
|
TemplateSwitch::TemplateSwitch(const std::string &name)
|
|
: switch_::Switch(name), Component(), turn_on_trigger_(new Trigger<>()), turn_off_trigger_(new Trigger<>()) {}
|
|
void TemplateSwitch::loop() {
|
|
if (!this->f_.has_value())
|
|
return;
|
|
auto s = (*this->f_)();
|
|
if (!s.has_value())
|
|
return;
|
|
|
|
this->publish_state(*s);
|
|
}
|
|
void TemplateSwitch::write_state(bool state) {
|
|
if (this->prev_trigger_ != nullptr) {
|
|
this->prev_trigger_->stop();
|
|
}
|
|
|
|
if (state) {
|
|
this->prev_trigger_ = this->turn_on_trigger_;
|
|
this->turn_on_trigger_->trigger();
|
|
} else {
|
|
this->prev_trigger_ = this->turn_off_trigger_;
|
|
this->turn_off_trigger_->trigger();
|
|
}
|
|
|
|
if (this->optimistic_)
|
|
this->publish_state(state);
|
|
}
|
|
void TemplateSwitch::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
|
bool TemplateSwitch::assumed_state() { return this->assumed_state_; }
|
|
void TemplateSwitch::set_state_lambda(std::function<optional<bool>()> &&f) { this->f_ = f; }
|
|
float TemplateSwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
|
|
Trigger<> *TemplateSwitch::get_turn_on_trigger() const { return this->turn_on_trigger_; }
|
|
Trigger<> *TemplateSwitch::get_turn_off_trigger() const { return this->turn_off_trigger_; }
|
|
void TemplateSwitch::setup() {
|
|
if (!this->restore_state_)
|
|
return;
|
|
|
|
auto restored = this->get_initial_state();
|
|
if (!restored.has_value())
|
|
return;
|
|
|
|
ESP_LOGD(TAG, " Restored state %s", ONOFF(*restored));
|
|
if (*restored) {
|
|
this->turn_on();
|
|
} else {
|
|
this->turn_off();
|
|
}
|
|
}
|
|
void TemplateSwitch::dump_config() {
|
|
LOG_SWITCH("", "Template Switch", this);
|
|
ESP_LOGCONFIG(TAG, " Restore State: %s", YESNO(this->restore_state_));
|
|
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
|
}
|
|
void TemplateSwitch::set_restore_state(bool restore_state) { this->restore_state_ = restore_state; }
|
|
void TemplateSwitch::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
|
|
|
} // namespace template_
|
|
} // namespace esphome
|