ESPHome 2026.1.5
Loading...
Searching...
No Matches
copy_fan.cpp
Go to the documentation of this file.
1#include "copy_fan.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace copy {
6
7static const char *const TAG = "copy.fan";
8
12 this->publish_state();
13 });
14
16 this->publish_state();
17}
18
20 this->state = source_->state;
22 this->speed = source_->speed;
24 if (source_->has_preset_mode()) {
26 } else {
27 this->clear_preset_mode_();
28 }
29}
30
31void CopyFan::dump_config() { LOG_FAN("", "Copy Fan", this); }
32
34 fan::FanTraits traits;
35 auto base = source_->get_traits();
36 // copy traits manually so it doesn't break when new options are added
37 // but the control() method hasn't implemented them yet.
38 traits.set_oscillation(base.supports_oscillation());
39 traits.set_speed(base.supports_speed());
40 traits.set_supported_speed_count(base.supported_speed_count());
41 traits.set_direction(base.supports_direction());
42 traits.set_supported_preset_modes(base.supported_preset_modes());
43 return traits;
44}
45
46void CopyFan::control(const fan::FanCall &call) {
47 auto call2 = source_->make_call();
48 if (call.get_state().has_value())
49 call2.set_state(*call.get_state());
50 if (call.get_oscillating().has_value())
51 call2.set_oscillating(*call.get_oscillating());
52 if (call.get_speed().has_value())
53 call2.set_speed(*call.get_speed());
54 if (call.get_direction().has_value())
55 call2.set_direction(*call.get_direction());
56 if (call.has_preset_mode())
57 call2.set_preset_mode(call.get_preset_mode());
58 call2.perform();
59}
60
61} // namespace copy
62} // namespace esphome
void control(const fan::FanCall &call) override
Definition copy_fan.cpp:46
void dump_config() override
Definition copy_fan.cpp:31
fan::FanTraits get_traits() override
Definition copy_fan.cpp:33
void setup() override
Definition copy_fan.cpp:9
FanCall & set_oscillating(bool oscillating)
Definition fan.h:51
FanCall & set_direction(FanDirection direction)
Definition fan.h:65
FanCall & set_speed(int speed)
Definition fan.h:60
FanCall & set_state(bool binary_state)
Definition fan.h:42
FanCall & set_preset_mode(const std::string &preset_mode)
Definition fan.cpp:22
void publish_state()
Definition fan.cpp:200
FanCall make_call()
Definition fan.cpp:147
virtual FanTraits get_traits()=0
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:188
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:157
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:136
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:199
FanDirection direction
The current direction of the fan.
Definition fan.h:115
bool oscillating
The current oscillation state of the fan.
Definition fan.h:111
bool state
The current on/off state of the fan.
Definition fan.h:109
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:139
int speed
The current fan speed level.
Definition fan.h:113
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:32
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:24
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:28
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:20
void set_supported_preset_modes(std::initializer_list< const char * > preset_modes)
Set the preset modes supported by the fan (from initializer list).
Definition fan_traits.h:36
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7