mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-25 15:18:29 +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).
138 lines
4.3 KiB
C++
138 lines
4.3 KiB
C++
#include "template_cover.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace template_ {
|
|
|
|
using namespace esphome::cover;
|
|
|
|
static const char *TAG = "template.cover";
|
|
|
|
TemplateCover::TemplateCover(const std::string &name)
|
|
: Cover(name),
|
|
open_trigger_(new Trigger<>()),
|
|
close_trigger_(new Trigger<>),
|
|
stop_trigger_(new Trigger<>()),
|
|
position_trigger_(new Trigger<float>()),
|
|
tilt_trigger_(new Trigger<float>()) {}
|
|
void TemplateCover::setup() {
|
|
ESP_LOGCONFIG(TAG, "Setting up template cover '%s'...", this->name_.c_str());
|
|
switch (this->restore_mode_) {
|
|
case COVER_NO_RESTORE:
|
|
break;
|
|
case COVER_RESTORE: {
|
|
auto restore = this->restore_state_();
|
|
if (restore.has_value())
|
|
restore->apply(this);
|
|
break;
|
|
}
|
|
case COVER_RESTORE_AND_CALL: {
|
|
auto restore = this->restore_state_();
|
|
if (restore.has_value()) {
|
|
restore->to_call(this).perform();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
void TemplateCover::loop() {
|
|
bool changed = false;
|
|
|
|
if (this->state_f_.has_value()) {
|
|
auto s = (*this->state_f_)();
|
|
if (s.has_value()) {
|
|
auto pos = clamp(*s, 0.0f, 1.0f);
|
|
if (pos != this->position) {
|
|
this->position = pos;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
if (this->tilt_f_.has_value()) {
|
|
auto s = (*this->tilt_f_)();
|
|
if (s.has_value()) {
|
|
auto tilt = clamp(*s, 0.0f, 1.0f);
|
|
if (tilt != this->tilt) {
|
|
this->tilt = tilt;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (changed)
|
|
this->publish_state();
|
|
}
|
|
void TemplateCover::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
|
void TemplateCover::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
|
void TemplateCover::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
|
|
float TemplateCover::get_setup_priority() const { return setup_priority::HARDWARE; }
|
|
Trigger<> *TemplateCover::get_open_trigger() const { return this->open_trigger_; }
|
|
Trigger<> *TemplateCover::get_close_trigger() const { return this->close_trigger_; }
|
|
Trigger<> *TemplateCover::get_stop_trigger() const { return this->stop_trigger_; }
|
|
void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
|
|
void TemplateCover::control(const CoverCall &call) {
|
|
if (call.get_stop()) {
|
|
this->stop_prev_trigger_();
|
|
this->stop_trigger_->trigger();
|
|
this->prev_command_trigger_ = this->stop_trigger_;
|
|
this->current_operation = COVER_OPERATION_IDLE;
|
|
this->publish_state();
|
|
}
|
|
if (call.get_position().has_value()) {
|
|
auto pos = *call.get_position();
|
|
this->stop_prev_trigger_();
|
|
|
|
if (pos < this->position) {
|
|
this->current_operation = COVER_OPERATION_CLOSING;
|
|
} else if (pos > this->position) {
|
|
this->current_operation = COVER_OPERATION_OPENING;
|
|
}
|
|
|
|
if (pos == COVER_OPEN) {
|
|
this->open_trigger_->trigger();
|
|
this->prev_command_trigger_ = this->open_trigger_;
|
|
} else if (pos == COVER_CLOSED) {
|
|
this->close_trigger_->trigger();
|
|
this->prev_command_trigger_ = this->close_trigger_;
|
|
}
|
|
|
|
this->position_trigger_->trigger(pos);
|
|
|
|
if (this->optimistic_) {
|
|
this->position = pos;
|
|
}
|
|
}
|
|
|
|
if (call.get_tilt().has_value()) {
|
|
auto tilt = *call.get_tilt();
|
|
this->tilt_trigger_->trigger(tilt);
|
|
|
|
if (this->optimistic_) {
|
|
this->tilt = tilt;
|
|
}
|
|
}
|
|
|
|
this->publish_state();
|
|
}
|
|
CoverTraits TemplateCover::get_traits() {
|
|
auto traits = CoverTraits();
|
|
traits.set_is_assumed_state(this->assumed_state_);
|
|
traits.set_supports_position(this->has_position_);
|
|
traits.set_supports_tilt(this->has_tilt_);
|
|
return traits;
|
|
}
|
|
Trigger<float> *TemplateCover::get_position_trigger() const { return this->position_trigger_; }
|
|
Trigger<float> *TemplateCover::get_tilt_trigger() const { return this->tilt_trigger_; }
|
|
void TemplateCover::set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
|
|
void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
|
|
void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
|
|
void TemplateCover::stop_prev_trigger_() {
|
|
if (this->prev_command_trigger_ != nullptr) {
|
|
this->prev_command_trigger_->stop();
|
|
this->prev_command_trigger_ = nullptr;
|
|
}
|
|
}
|
|
|
|
} // namespace template_
|
|
} // namespace esphome
|