ESPHome  2024.11.0
copy_fan.cpp
Go to the documentation of this file.
1 #include "copy_fan.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace copy {
6 
7 static const char *const TAG = "copy.fan";
8 
9 void CopyFan::setup() {
10  source_->add_on_state_callback([this]() {
11  this->state = source_->state;
13  this->speed = source_->speed;
14  this->direction = source_->direction;
16  this->publish_state();
17  });
18 
19  this->state = source_->state;
21  this->speed = source_->speed;
22  this->direction = source_->direction;
24  this->publish_state();
25 }
26 
27 void CopyFan::dump_config() { LOG_FAN("", "Copy Fan", this); }
28 
30  fan::FanTraits traits;
31  auto base = source_->get_traits();
32  // copy traits manually so it doesn't break when new options are added
33  // but the control() method hasn't implemented them yet.
34  traits.set_oscillation(base.supports_oscillation());
35  traits.set_speed(base.supports_speed());
36  traits.set_supported_speed_count(base.supported_speed_count());
37  traits.set_direction(base.supports_direction());
38  traits.set_supported_preset_modes(base.supported_preset_modes());
39  return traits;
40 }
41 
43  auto call2 = source_->make_call();
44  if (call.get_state().has_value())
45  call2.set_state(*call.get_state());
46  if (call.get_oscillating().has_value())
47  call2.set_oscillating(*call.get_oscillating());
48  if (call.get_speed().has_value())
49  call2.set_speed(*call.get_speed());
50  if (call.get_direction().has_value())
51  call2.set_direction(*call.get_direction());
52  if (!call.get_preset_mode().empty())
53  call2.set_preset_mode(call.get_preset_mode());
54  call2.perform();
55 }
56 
57 } // namespace copy
58 } // namespace esphome
bool state
The current on/off state of the fan.
Definition: fan.h:110
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:112
FanDirection direction
The current direction of the fan.
Definition: fan.h:116
optional< FanDirection > get_direction() const
Definition: fan.h:74
void publish_state()
Definition: fan.cpp:117
optional< int > get_speed() const
Definition: fan.h:65
virtual FanTraits get_traits()=0
void setup() override
Definition: copy_fan.cpp:9
bool has_value() const
Definition: optional.h:87
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition: fan.cpp:116
FanCall & set_speed(int speed)
Definition: fan.h:59
std::string preset_mode
Definition: fan.h:118
fan::Fan * source_
Definition: copy_fan.h:20
int speed
The current fan speed level.
Definition: fan.h:114
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition: fan_traits.h:18
optional< bool > get_state() const
Definition: fan.h:49
optional< bool > get_oscillating() const
Definition: fan.h:58
FanCall & set_oscillating(bool oscillating)
Definition: fan.h:50
void dump_config() override
Definition: copy_fan.cpp:27
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition: fan_traits.h:22
FanCall & set_state(bool binary_state)
Definition: fan.h:41
FanCall & set_preset_mode(const std::string &preset_mode)
Definition: fan.h:75
void control(const fan::FanCall &call) override
Definition: copy_fan.cpp:42
void set_supported_preset_modes(const std::set< std::string > &preset_modes)
Set the preset modes supported by the fan.
Definition: fan_traits.h:34
fan::FanTraits get_traits() override
Definition: copy_fan.cpp:29
FanCall make_call()
Definition: fan.cpp:114
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition: fan_traits.h:30
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::string get_preset_mode() const
Definition: fan.h:79
FanCall & set_direction(FanDirection direction)
Definition: fan.h:66
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition: fan_traits.h:26