ESPHome  2024.9.2
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_stop(base.get_supports_stop());
32  traits.set_supports_position(base.get_supports_position());
33  traits.set_supports_tilt(base.get_supports_tilt());
34  traits.set_supports_toggle(base.get_supports_toggle());
35  return traits;
36 }
37 
39  auto call2 = source_->make_call();
40  call2.set_stop(call.get_stop());
41  if (call.get_tilt().has_value())
42  call2.set_tilt(*call.get_tilt());
43  if (call.get_position().has_value())
44  call2.set_position(*call.get_position());
45  if (call.get_tilt().has_value())
46  call2.set_tilt(*call.get_tilt());
47  call2.perform();
48 }
49 
50 } // namespace copy
51 } // 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:116
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:124
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:38
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:122
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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