ESPHome  2022.8.0
copy_cover.cpp
Go to the documentation of this file.
1 #include "copy_cover.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace copy {
6 
7 static const char *const TAG = "copy.cover";
8 
10  source_->add_on_state_callback([this]() {
12  this->position = this->source_->position;
13  this->tilt = this->source_->tilt;
14  this->publish_state();
15  });
16 
18  this->position = this->source_->position;
19  this->tilt = this->source_->tilt;
20  this->publish_state();
21 }
22 
23 void CopyCover::dump_config() { LOG_COVER("", "Copy Cover", this); }
24 
26  auto base = source_->get_traits();
27  cover::CoverTraits traits{};
28  // copy traits manually so it doesn't break when new options are added
29  // but the control() method hasn't implemented them yet.
30  traits.set_is_assumed_state(base.get_is_assumed_state());
31  traits.set_supports_position(base.get_supports_position());
32  traits.set_supports_tilt(base.get_supports_tilt());
33  traits.set_supports_toggle(base.get_supports_toggle());
34  return traits;
35 }
36 
38  auto call2 = source_->make_call();
39  call2.set_stop(call.get_stop());
40  if (call.get_tilt().has_value())
41  call2.set_tilt(*call.get_tilt());
42  if (call.get_position().has_value())
43  call2.set_position(*call.get_position());
44  if (call.get_tilt().has_value())
45  call2.set_tilt(*call.get_tilt());
46  call2.perform();
47 }
48 
49 } // namespace copy
50 } // namespace esphome
void publish_state(bool save=true)
Publish the current state of the cover.
Definition: cover.cpp:166
void setup() override
Definition: copy_cover.cpp:9
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition: cover.cpp:149
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition: cover.h:117
cover::Cover * source_
Definition: copy_cover.h:21
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition: cover.cpp:67
void perform()
Perform the cover call.
Definition: cover.cpp:75
bool has_value() const
Definition: optional.h:87
CoverCall & set_stop(bool stop)
Set whether this cover call should stop the cover.
Definition: cover.cpp:143
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition: cover.h:125
virtual CoverTraits get_traits()=0
void add_on_state_callback(std::function< void()> &&f)
Definition: cover.cpp:165
void set_is_assumed_state(bool is_assumed_state)
Definition: cover_traits.h:11
void control(const cover::CoverCall &call) override
Definition: copy_cover.cpp:37
cover::CoverTraits get_traits() override
Definition: copy_cover.cpp:25
void dump_config() override
Definition: copy_cover.cpp:23
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:123
Definition: a4988.cpp:4
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition: cover.cpp:71
const optional< float > & get_tilt() const
Definition: cover.cpp:98
const optional< float > & get_position() const
Definition: cover.cpp:97
bool get_stop() const
Definition: cover.cpp:147