9static const char *
const TAG =
"fan";
14static const std::vector<const char *> EMPTY_PRESET_MODES;
24 return EMPTY_PRESET_MODES;
31 return FanDirectionStrings::get_log_str(
static_cast<uint8_t
>(
direction), FanDirectionStrings::LAST_INDEX);
51 if (validated_mode !=
nullptr) {
68 ESP_LOGV(TAG,
" Oscillating: %s", YESNO(*this->
oscillating_));
70 if (this->
speed_.has_value()) {
71 ESP_LOGV(TAG,
" Speed: %d", *this->
speed_);
85 if (this->
speed_.has_value()) {
86 this->
speed_ = clamp(*this->
speed_, 1, traits.supported_speed_count());
94 if (!this->
parent_.
state && this->binary_state_.has_value() &&
97 && !this->has_preset_mode() &&
98 !this->parent_.has_preset_mode()
100 && traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
102 this->
speed_ = traits.supported_speed_count();
105 if (this->
oscillating_.has_value() && !traits.supports_oscillation()) {
110 if (this->
speed_.has_value() && !traits.supports_speed()) {
115 if (this->
direction_.has_value() && !traits.supports_direction()) {
129 if (traits.supports_preset_modes()) {
131 const auto &preset_modes = traits.supported_preset_modes();
133 call.set_preset_mode(preset_modes[this->
preset_mode]);
145 if (traits.supports_preset_modes()) {
147 const auto &preset_modes = traits.supported_preset_modes();
169 if (this->supported_preset_modes_) {
170 for (
const char *
mode : *this->supported_preset_modes_) {
184 if (this->preset_mode_ ==
nullptr) {
191 if (validated ==
nullptr || this->preset_mode_ == validated) {
194 this->preset_mode_ = validated;
217 }
else if (call.
get_speed().has_value()) {
230 if (traits.supports_speed()) {
231 ESP_LOGV(TAG,
" Speed: %d", this->
speed);
233 if (traits.supports_oscillation()) {
234 ESP_LOGV(TAG,
" Oscillating: %s", YESNO(this->
oscillating));
236 if (traits.supports_direction()) {
239 if (this->preset_mode_ !=
nullptr) {
240 ESP_LOGV(TAG,
" Preset Mode: %s", this->preset_mode_);
243#if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY)
244 ControllerRegistry::notify_fan_update(
this);
254 bool restored = this->
rtc_.
load(&recovered);
265 recovered.state =
false;
268 recovered.state =
true;
271 recovered.state = restored ? recovered.state :
false;
274 recovered.state = restored ? recovered.state :
true;
277 recovered.state = restored ? !recovered.state :
false;
280 recovered.state = restored ? !recovered.state :
true;
299 if (this->supported_preset_modes_) {
301 for (
size_t i = 0; i < this->supported_preset_modes_->size(); i++) {
302 if ((*this->supported_preset_modes_)[i] == this->preset_mode_) {
303 state.preset_mode = i;
312 const auto &preset_modes = traits.supported_preset_modes();
313 for (
size_t i = 0; i < preset_modes.size(); i++) {
314 if (preset_modes[i] == this->preset_mode_) {
315 state.preset_mode = i;
328 if (traits.supports_speed()) {
331 "%s Speed count: %d",
332 prefix, prefix, traits.supported_speed_count());
334 if (traits.supports_oscillation()) {
335 ESP_LOGCONFIG(
tag,
"%s Oscillation: YES", prefix);
337 if (traits.supports_direction()) {
338 ESP_LOGCONFIG(
tag,
"%s Direction: YES", prefix);
340 if (traits.supports_preset_modes()) {
341 ESP_LOGCONFIG(
tag,
"%s Supported presets:", prefix);
342 for (
const char *s : traits.supported_preset_modes())
343 ESP_LOGCONFIG(
tag,
"%s - %s", prefix, s);
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
FanCall & set_oscillating(bool oscillating)
FanCall & set_direction(FanDirection direction)
optional< bool > binary_state_
optional< FanDirection > direction_
const char * preset_mode_
const char * get_preset_mode() const
bool has_preset_mode() const
FanCall & set_speed(int speed)
FanCall & set_state(bool binary_state)
optional< int > get_speed() const
optional< bool > oscillating_
FanCall & set_preset_mode(const std::string &preset_mode)
virtual FanTraits get_traits()=0
LazyCallbackManager< void()> state_callback_
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
void clear_preset_mode_()
Clear the preset mode.
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
FanDirection direction
The current direction of the fan.
FanRestoreMode restore_mode_
bool oscillating
The current oscillation state of the fan.
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
const char * find_preset_mode_(const char *preset_mode)
Find and return the matching preset mode pointer from traits, or nullptr if not found.
bool has_preset_mode() const
Check if a preset mode is currently active.
int speed
The current fan speed level.
void dump_traits_(const char *tag, const char *prefix)
optional< FanRestoreState > restore_state_()
std::vector< const char * > compat_preset_modes_
const std::vector< const char * > * preset_modes_
const std::vector< const char * > & supported_preset_modes() const
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.
@ RESTORE_INVERTED_DEFAULT_OFF
@ RESTORE_INVERTED_DEFAULT_ON
const LogString * fan_direction_to_string(FanDirection direction)
constexpr uint32_t RESTORE_STATE_VERSION
FanDirection
Simple enum to represent the direction of a fan.
PROGMEM_STRING_TABLE(FanDirectionStrings, "FORWARD", "REVERSE", "UNKNOWN")
static constexpr uint8_t NO_PRESET
void apply(Fan &fan)
Apply these settings to the fan.
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.