1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-21 12:42:21 +01:00

add template fan (#6310)

This commit is contained in:
Samuel Sieb
2024-03-10 15:42:02 -07:00
committed by GitHub
parent 0cdd0b295e
commit 6a46548a8b
10 changed files with 213 additions and 20 deletions

View File

@@ -0,0 +1,43 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import fan
from esphome.components.fan import validate_preset_modes
from esphome.const import (
CONF_OUTPUT_ID,
CONF_PRESET_MODES,
CONF_SPEED_COUNT,
)
from .. import template_ns
CODEOWNERS = ["@ssieb"]
TemplateFan = template_ns.class_("TemplateFan", cg.Component, fan.Fan)
CONF_HAS_DIRECTION = "has_direction"
CONF_HAS_OSCILLATING = "has_oscillating"
CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
{
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(TemplateFan),
cv.Optional(CONF_HAS_DIRECTION, default=False): cv.boolean,
cv.Optional(CONF_HAS_OSCILLATING, default=False): cv.boolean,
cv.Optional(CONF_SPEED_COUNT): cv.int_range(min=1),
cv.Optional(CONF_PRESET_MODES): validate_preset_modes,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
await cg.register_component(var, config)
await fan.register_fan(var, config)
cg.add(var.set_has_direction(config[CONF_HAS_DIRECTION]))
cg.add(var.set_has_oscillating(config[CONF_HAS_OSCILLATING]))
if CONF_SPEED_COUNT in config:
cg.add(var.set_speed_count(config[CONF_SPEED_COUNT]))
if CONF_PRESET_MODES in config:
cg.add(var.set_preset_modes(config[CONF_PRESET_MODES]))

View File

@@ -0,0 +1,38 @@
#include "template_fan.h"
#include "esphome/core/log.h"
namespace esphome {
namespace template_ {
static const char *const TAG = "template.fan";
void TemplateFan::setup() {
auto restore = this->restore_state_();
if (restore.has_value()) {
restore->apply(*this);
}
// Construct traits
this->traits_ =
fan::FanTraits(this->has_oscillating_, this->speed_count_ > 0, this->has_direction_, this->speed_count_);
this->traits_.set_supported_preset_modes(this->preset_modes_);
}
void TemplateFan::dump_config() { LOG_FAN("", "Template Fan", this); }
void TemplateFan::control(const fan::FanCall &call) {
if (call.get_state().has_value())
this->state = *call.get_state();
if (call.get_speed().has_value() && (this->speed_count_ > 0))
this->speed = *call.get_speed();
if (call.get_oscillating().has_value() && this->has_oscillating_)
this->oscillating = *call.get_oscillating();
if (call.get_direction().has_value() && this->has_direction_)
this->direction = *call.get_direction();
this->preset_mode = call.get_preset_mode();
this->publish_state();
}
} // namespace template_
} // namespace esphome

View File

@@ -0,0 +1,33 @@
#pragma once
#include <set>
#include "esphome/core/component.h"
#include "esphome/components/fan/fan.h"
namespace esphome {
namespace template_ {
class TemplateFan : public Component, public fan::Fan {
public:
TemplateFan() {}
void setup() override;
void dump_config() override;
void set_has_direction(bool has_direction) { this->has_direction_ = has_direction; }
void set_has_oscillating(bool has_oscillating) { this->has_oscillating_ = has_oscillating; }
void set_speed_count(int count) { this->speed_count_ = count; }
void set_preset_modes(const std::set<std::string> &presets) { this->preset_modes_ = presets; }
fan::FanTraits get_traits() override { return this->traits_; }
protected:
void control(const fan::FanCall &call) override;
bool has_oscillating_{false};
bool has_direction_{false};
int speed_count_{0};
fan::FanTraits traits_;
std::set<std::string> preset_modes_{};
};
} // namespace template_
} // namespace esphome