ESPHome 2026.6.0-dev
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::copy {
5
6static const char *const TAG = "copy.fan";
7
9 // Copy preset modes once from source fan — stored on Fan base class
10 auto source_traits = source_->get_traits();
11 if (source_traits.supports_preset_modes()) {
12 this->set_supported_preset_modes(source_traits.supported_preset_modes());
13 }
14
17 this->publish_state();
18 });
19
21 this->publish_state();
22}
23
25 this->state = source_->state;
27 this->speed = source_->speed;
29 if (source_->has_preset_mode()) {
31 } else {
32 this->clear_preset_mode_();
33 }
34}
35
36void CopyFan::dump_config() { LOG_FAN("", "Copy Fan", this); }
37
39 fan::FanTraits traits;
40 auto base = source_->get_traits();
41 // copy traits manually so it doesn't break when new options are added
42 // but the control() method hasn't implemented them yet.
43 traits.set_oscillation(base.supports_oscillation());
44 traits.set_speed(base.supports_speed());
45 traits.set_supported_speed_count(base.supported_speed_count());
46 traits.set_direction(base.supports_direction());
47 // Preset modes are set once in setup() and wired via wire_preset_modes_()
48 this->wire_preset_modes_(traits);
49 return traits;
50}
51
52void CopyFan::control(const fan::FanCall &call) {
53 auto call2 = source_->make_call();
54 auto state = call.get_state();
55 if (state.has_value())
56 call2.set_state(*state);
57 auto oscillating = call.get_oscillating();
58 if (oscillating.has_value())
59 call2.set_oscillating(*oscillating);
60 auto speed = call.get_speed();
61 if (speed.has_value())
62 call2.set_speed(*speed);
63 auto direction = call.get_direction();
64 if (direction.has_value())
65 call2.set_direction(*direction);
66 if (call.has_preset_mode())
67 call2.set_preset_mode(call.get_preset_mode());
68 call2.perform();
69}
70
71} // namespace esphome::copy
void control(const fan::FanCall &call) override
Definition copy_fan.cpp:52
void dump_config() override
Definition copy_fan.cpp:36
fan::FanTraits get_traits() override
Definition copy_fan.cpp:38
void setup() override
Definition copy_fan.cpp:8
optional< bool > get_state() const
Definition fan.h:49
void set_supported_preset_modes(std::initializer_list< const char * > preset_modes)
Set the supported preset modes (stored on Fan, referenced by FanTraits via pointer).
Definition fan.h:133
void add_on_state_callback(F &&callback)
Register a callback that will be called each time the state changes.
Definition fan.h:124
void publish_state()
Definition fan.cpp:223
FanCall make_call()
Definition fan.cpp:159
virtual FanTraits get_traits()=0
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:212
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:181
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:147
FanDirection direction
The current direction of the fan.
Definition fan.h:116
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
bool state
The current on/off state of the fan.
Definition fan.h:110
void wire_preset_modes_(FanTraits &traits)
Wire the Fan-owned preset modes pointer into the given traits object.
Definition fan.h:178
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:150
int speed
The current fan speed level.
Definition fan.h:114
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:35
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:27
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:31
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:23