ESPHome 2026.5.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 {
5namespace copy {
6
7static const char *const TAG = "copy.fan";
8
10 // Copy preset modes once from source fan — stored on Fan base class
11 auto source_traits = source_->get_traits();
12 if (source_traits.supports_preset_modes()) {
13 this->set_supported_preset_modes(source_traits.supported_preset_modes());
14 }
15
18 this->publish_state();
19 });
20
22 this->publish_state();
23}
24
26 this->state = source_->state;
28 this->speed = source_->speed;
30 if (source_->has_preset_mode()) {
32 } else {
33 this->clear_preset_mode_();
34 }
35}
36
37void CopyFan::dump_config() { LOG_FAN("", "Copy Fan", this); }
38
40 fan::FanTraits traits;
41 auto base = source_->get_traits();
42 // copy traits manually so it doesn't break when new options are added
43 // but the control() method hasn't implemented them yet.
44 traits.set_oscillation(base.supports_oscillation());
45 traits.set_speed(base.supports_speed());
46 traits.set_supported_speed_count(base.supported_speed_count());
47 traits.set_direction(base.supports_direction());
48 // Preset modes are set once in setup() and wired via wire_preset_modes_()
49 this->wire_preset_modes_(traits);
50 return traits;
51}
52
53void CopyFan::control(const fan::FanCall &call) {
54 auto call2 = source_->make_call();
55 auto state = call.get_state();
56 if (state.has_value())
57 call2.set_state(*state);
58 auto oscillating = call.get_oscillating();
59 if (oscillating.has_value())
60 call2.set_oscillating(*oscillating);
61 auto speed = call.get_speed();
62 if (speed.has_value())
63 call2.set_speed(*speed);
64 auto direction = call.get_direction();
65 if (direction.has_value())
66 call2.set_direction(*direction);
67 if (call.has_preset_mode())
68 call2.set_preset_mode(call.get_preset_mode());
69 call2.perform();
70}
71
72} // namespace copy
73} // namespace esphome
void control(const fan::FanCall &call) override
Definition copy_fan.cpp:53
void dump_config() override
Definition copy_fan.cpp:37
fan::FanTraits get_traits() override
Definition copy_fan.cpp:39
void setup() override
Definition copy_fan.cpp:9
optional< bool > get_state() const
Definition fan.h:50
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:134
void add_on_state_callback(F &&callback)
Register a callback that will be called each time the state changes.
Definition fan.h:125
void publish_state()
Definition fan.cpp:224
FanCall make_call()
Definition fan.cpp:160
virtual FanTraits get_traits()=0
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:213
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:182
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:148
FanDirection direction
The current direction of the fan.
Definition fan.h:117
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
bool state
The current on/off state of the fan.
Definition fan.h:111
void wire_preset_modes_(FanTraits &traits)
Wire the Fan-owned preset modes pointer into the given traits object.
Definition fan.h:179
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:151
int speed
The current fan speed level.
Definition fan.h:115
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:37
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:29
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:33
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:25
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7