ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
fan_traits.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <type_traits>
6
7namespace esphome::fan {
8
9class Fan; // Forward declaration
10
13
14class FanTraits {
15 friend class Fan; // Allow Fan to access protected pointer setter
16
17 public:
18 FanTraits() = default;
19 FanTraits(bool oscillation, bool speed, bool direction, int speed_count)
20 : oscillation_(oscillation), speed_(speed), direction_(direction), speed_count_(speed_count) {}
21
23 bool supports_oscillation() const { return this->oscillation_; }
25 void set_oscillation(bool oscillation) { this->oscillation_ = oscillation; }
27 bool supports_speed() const { return this->speed_; }
29 void set_speed(bool speed) { this->speed_ = speed; }
31 int supported_speed_count() const { return this->speed_count_; }
33 void set_supported_speed_count(int speed_count) { this->speed_count_ = speed_count; }
35 bool supports_direction() const { return this->direction_; }
40
42 bool supports_preset_modes() const { return this->preset_modes_ != nullptr && !this->preset_modes_->empty(); }
44 const char *find_preset_mode(const char *preset_mode) const {
45 return this->find_preset_mode(preset_mode, preset_mode ? strlen(preset_mode) : 0);
46 }
47 const char *find_preset_mode(const char *preset_mode, size_t len) const {
48 if (preset_mode == nullptr || len == 0) {
49 return nullptr;
50 }
51 for (const char *mode : this->supported_preset_modes()) {
52 if (strncmp(mode, preset_mode, len) == 0 && mode[len] == '\0') {
53 return mode;
54 }
55 }
56 return nullptr;
57 }
58
59 protected:
61 void set_supported_preset_modes_(const FanPresetModes *preset_modes) { this->preset_modes_ = preset_modes; }
62
63 bool oscillation_{false};
64 bool speed_{false};
65 bool direction_{false};
67 const FanPresetModes *preset_modes_{nullptr}; // owned by the Fan entity
68};
69static_assert(std::is_trivially_copyable_v<FanTraits>, "FanTraits is returned by value from get_traits()");
70
71} // namespace esphome::fan
BedjetMode mode
BedJet operating mode.
constexpr bool empty() const
Definition helpers.h:141
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:37
bool supports_preset_modes() const
Return if preset modes are supported.
Definition fan_traits.h:42
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:29
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:31
const char * find_preset_mode(const char *preset_mode, size_t len) const
Definition fan_traits.h:47
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:33
const FanPresetModes & supported_preset_modes() const
Empty when the fan has no preset modes. Set them on the Fan entity.
Definition fan.cpp:13
bool supports_direction() const
Return if this fan supports changing direction.
Definition fan_traits.h:35
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:25
bool supports_speed() const
Return if this fan supports speed modes.
Definition fan_traits.h:27
FanTraits(bool oscillation, bool speed, bool direction, int speed_count)
Definition fan_traits.h:19
void set_supported_preset_modes_(const FanPresetModes *preset_modes)
Set the preset modes pointer (only Fan::wire_preset_modes_() should call this).
Definition fan_traits.h:61
const FanPresetModes * preset_modes_
Definition fan_traits.h:67
const char * find_preset_mode(const char *preset_mode) const
Find and return the matching preset mode pointer from supported modes, or nullptr if not found.
Definition fan_traits.h:44
bool supports_oscillation() const
Return if this fan supports oscillation.
Definition fan_traits.h:23
FanDirection direction
Definition fan.h:5
int speed
Definition fan.h:3
uint8_t preset_mode
Definition fan.h:6
const void size_t len
Definition hal.h:68