mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 13:34:54 +01:00
add template fan (#6310)
This commit is contained in:
parent
0cdd0b295e
commit
6a46548a8b
10 changed files with 213 additions and 20 deletions
|
@ -342,6 +342,7 @@ esphome/components/tee501/* @Stock-M
|
|||
esphome/components/teleinfo/* @0hax
|
||||
esphome/components/template/alarm_control_panel/* @grahambrown11 @hwstar
|
||||
esphome/components/template/datetime/* @rfdarter
|
||||
esphome/components/template/fan/* @ssieb
|
||||
esphome/components/text/* @mauritskorse
|
||||
esphome/components/thermostat/* @kbx81
|
||||
esphome/components/time/* @OttoWinter
|
||||
|
|
|
@ -15,7 +15,10 @@ from esphome.const import (
|
|||
CONF_SPEED_COMMAND_TOPIC,
|
||||
CONF_SPEED_STATE_TOPIC,
|
||||
CONF_OFF_SPEED_CYCLE,
|
||||
CONF_ON_DIRECTION_SET,
|
||||
CONF_ON_OSCILLATING_SET,
|
||||
CONF_ON_SPEED_SET,
|
||||
CONF_ON_STATE,
|
||||
CONF_ON_TURN_OFF,
|
||||
CONF_ON_TURN_ON,
|
||||
CONF_ON_PRESET_SET,
|
||||
|
@ -55,11 +58,22 @@ TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action)
|
|||
ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
|
||||
CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action)
|
||||
|
||||
FanStateTrigger = fan_ns.class_(
|
||||
"FanStateTrigger", automation.Trigger.template(Fan.operator("ptr"))
|
||||
)
|
||||
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
|
||||
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
|
||||
FanSpeedSetTrigger = fan_ns.class_("FanSpeedSetTrigger", automation.Trigger.template())
|
||||
FanDirectionSetTrigger = fan_ns.class_(
|
||||
"FanDirectionSetTrigger", automation.Trigger.template(FanDirection)
|
||||
)
|
||||
FanOscillatingSetTrigger = fan_ns.class_(
|
||||
"FanOscillatingSetTrigger", automation.Trigger.template(cg.bool_)
|
||||
)
|
||||
FanSpeedSetTrigger = fan_ns.class_(
|
||||
"FanSpeedSetTrigger", automation.Trigger.template(cg.int_)
|
||||
)
|
||||
FanPresetSetTrigger = fan_ns.class_(
|
||||
"FanPresetSetTrigger", automation.Trigger.template()
|
||||
"FanPresetSetTrigger", automation.Trigger.template(cg.std_string)
|
||||
)
|
||||
|
||||
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
|
||||
|
@ -90,6 +104,11 @@ FAN_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).exte
|
|||
cv.Optional(CONF_SPEED_COMMAND_TOPIC): cv.All(
|
||||
cv.requires_component("mqtt"), cv.subscribe_topic
|
||||
),
|
||||
cv.Optional(CONF_ON_STATE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanStateTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_TURN_ON): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanTurnOnTrigger),
|
||||
|
@ -100,6 +119,16 @@ FAN_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).exte
|
|||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanTurnOffTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_DIRECTION_SET): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanDirectionSetTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_OSCILLATING_SET): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanOscillatingSetTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_SPEED_SET): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanSpeedSetTrigger),
|
||||
|
@ -186,18 +215,27 @@ async def setup_fan_core_(var, config):
|
|||
mqtt_.set_custom_speed_command_topic(config[CONF_SPEED_COMMAND_TOPIC])
|
||||
)
|
||||
|
||||
for conf in config.get(CONF_ON_STATE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(Fan.operator("ptr"), "x")], conf)
|
||||
for conf in config.get(CONF_ON_TURN_ON, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
for conf in config.get(CONF_ON_TURN_OFF, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
for conf in config.get(CONF_ON_DIRECTION_SET, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(FanDirection, "x")], conf)
|
||||
for conf in config.get(CONF_ON_OSCILLATING_SET, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(cg.bool_, "x")], conf)
|
||||
for conf in config.get(CONF_ON_SPEED_SET, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
await automation.build_automation(trigger, [(cg.int_, "x")], conf)
|
||||
for conf in config.get(CONF_ON_PRESET_SET, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
await automation.build_automation(trigger, [(cg.std_string, "x")], conf)
|
||||
|
||||
|
||||
async def register_fan(var, config):
|
||||
|
|
|
@ -111,6 +111,13 @@ template<typename... Ts> class FanIsOffCondition : public Condition<Ts...> {
|
|||
Fan *state_;
|
||||
};
|
||||
|
||||
class FanStateTrigger : public Trigger<Fan *> {
|
||||
public:
|
||||
FanStateTrigger(Fan *state) {
|
||||
state->add_on_state_callback([this, state]() { this->trigger(state); });
|
||||
}
|
||||
};
|
||||
|
||||
class FanTurnOnTrigger : public Trigger<> {
|
||||
public:
|
||||
FanTurnOnTrigger(Fan *state) {
|
||||
|
@ -147,15 +154,51 @@ class FanTurnOffTrigger : public Trigger<> {
|
|||
bool last_on_;
|
||||
};
|
||||
|
||||
class FanSpeedSetTrigger : public Trigger<> {
|
||||
class FanDirectionSetTrigger : public Trigger<FanDirection> {
|
||||
public:
|
||||
FanDirectionSetTrigger(Fan *state) {
|
||||
state->add_on_state_callback([this, state]() {
|
||||
auto direction = state->direction;
|
||||
auto should_trigger = direction != this->last_direction_;
|
||||
this->last_direction_ = direction;
|
||||
if (should_trigger) {
|
||||
this->trigger(direction);
|
||||
}
|
||||
});
|
||||
this->last_direction_ = state->direction;
|
||||
}
|
||||
|
||||
protected:
|
||||
FanDirection last_direction_;
|
||||
};
|
||||
|
||||
class FanOscillatingSetTrigger : public Trigger<bool> {
|
||||
public:
|
||||
FanOscillatingSetTrigger(Fan *state) {
|
||||
state->add_on_state_callback([this, state]() {
|
||||
auto oscillating = state->oscillating;
|
||||
auto should_trigger = oscillating != this->last_oscillating_;
|
||||
this->last_oscillating_ = oscillating;
|
||||
if (should_trigger) {
|
||||
this->trigger(oscillating);
|
||||
}
|
||||
});
|
||||
this->last_oscillating_ = state->oscillating;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool last_oscillating_;
|
||||
};
|
||||
|
||||
class FanSpeedSetTrigger : public Trigger<int> {
|
||||
public:
|
||||
FanSpeedSetTrigger(Fan *state) {
|
||||
state->add_on_state_callback([this, state]() {
|
||||
auto speed = state->speed;
|
||||
auto should_trigger = speed != !this->last_speed_;
|
||||
auto should_trigger = speed != this->last_speed_;
|
||||
this->last_speed_ = speed;
|
||||
if (should_trigger) {
|
||||
this->trigger();
|
||||
this->trigger(speed);
|
||||
}
|
||||
});
|
||||
this->last_speed_ = state->speed;
|
||||
|
@ -165,7 +208,7 @@ class FanSpeedSetTrigger : public Trigger<> {
|
|||
int last_speed_;
|
||||
};
|
||||
|
||||
class FanPresetSetTrigger : public Trigger<> {
|
||||
class FanPresetSetTrigger : public Trigger<std::string> {
|
||||
public:
|
||||
FanPresetSetTrigger(Fan *state) {
|
||||
state->add_on_state_callback([this, state]() {
|
||||
|
@ -173,7 +216,7 @@ class FanPresetSetTrigger : public Trigger<> {
|
|||
auto should_trigger = preset_mode != this->last_preset_mode_;
|
||||
this->last_preset_mode_ = preset_mode;
|
||||
if (should_trigger) {
|
||||
this->trigger();
|
||||
this->trigger(preset_mode);
|
||||
}
|
||||
});
|
||||
this->last_preset_mode_ = state->preset_mode;
|
||||
|
|
|
@ -14,14 +14,12 @@ from esphome.const import (
|
|||
|
||||
from .. import speed_ns
|
||||
|
||||
AUTO_LOAD = ["output"]
|
||||
|
||||
SpeedFan = speed_ns.class_("SpeedFan", cg.Component, fan.Fan)
|
||||
|
||||
CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(SpeedFan),
|
||||
cv.Optional(CONF_OUTPUT): cv.use_id(output.FloatOutput),
|
||||
cv.Required(CONF_OUTPUT): cv.use_id(output.FloatOutput),
|
||||
cv.Optional(CONF_OSCILLATION_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||
cv.Optional(CONF_DIRECTION_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||
cv.Optional(CONF_SPEED): cv.invalid(
|
||||
|
@ -38,9 +36,8 @@ async def to_code(config):
|
|||
await cg.register_component(var, config)
|
||||
await fan.register_fan(var, config)
|
||||
|
||||
if CONF_OUTPUT in config:
|
||||
output_ = await cg.get_variable(config[CONF_OUTPUT])
|
||||
cg.add(var.set_output(output_))
|
||||
output_ = await cg.get_variable(config[CONF_OUTPUT])
|
||||
cg.add(var.set_output(output_))
|
||||
|
||||
if CONF_OSCILLATION_OUTPUT in config:
|
||||
oscillation_output = await cg.get_variable(config[CONF_OSCILLATION_OUTPUT])
|
||||
|
|
|
@ -36,10 +36,8 @@ void SpeedFan::control(const fan::FanCall &call) {
|
|||
}
|
||||
|
||||
void SpeedFan::write_state_() {
|
||||
if (this->output_ != nullptr) {
|
||||
float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
|
||||
this->output_->set_level(speed);
|
||||
}
|
||||
float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
|
||||
this->output_->set_level(speed);
|
||||
if (this->oscillating_ != nullptr)
|
||||
this->oscillating_->set_state(this->oscillating);
|
||||
if (this->direction_ != nullptr)
|
||||
|
|
|
@ -25,7 +25,7 @@ class SpeedFan : public Component, public fan::Fan {
|
|||
void control(const fan::FanCall &call) override;
|
||||
void write_state_();
|
||||
|
||||
output::FloatOutput *output_{nullptr};
|
||||
output::FloatOutput *output_;
|
||||
output::BinaryOutput *oscillating_{nullptr};
|
||||
output::BinaryOutput *direction_{nullptr};
|
||||
int speed_count_{};
|
||||
|
|
43
esphome/components/template/fan/__init__.py
Normal file
43
esphome/components/template/fan/__init__.py
Normal 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]))
|
38
esphome/components/template/fan/template_fan.cpp
Normal file
38
esphome/components/template/fan/template_fan.cpp
Normal 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
|
33
esphome/components/template/fan/template_fan.h
Normal file
33
esphome/components/template/fan/template_fan.h
Normal 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
|
|
@ -508,6 +508,7 @@ CONF_ON_CLIENT_CONNECTED = "on_client_connected"
|
|||
CONF_ON_CLIENT_DISCONNECTED = "on_client_disconnected"
|
||||
CONF_ON_CONNECT = "on_connect"
|
||||
CONF_ON_CONTROL = "on_control"
|
||||
CONF_ON_DIRECTION_SET = "on_direction_set"
|
||||
CONF_ON_DISCONNECT = "on_disconnect"
|
||||
CONF_ON_DOUBLE_CLICK = "on_double_click"
|
||||
CONF_ON_ENROLLMENT_DONE = "on_enrollment_done"
|
||||
|
@ -524,6 +525,7 @@ CONF_ON_LOOP = "on_loop"
|
|||
CONF_ON_MESSAGE = "on_message"
|
||||
CONF_ON_MULTI_CLICK = "on_multi_click"
|
||||
CONF_ON_OPEN = "on_open"
|
||||
CONF_ON_OSCILLATING_SET = "on_oscillating_set"
|
||||
CONF_ON_PRESET_SET = "on_preset_set"
|
||||
CONF_ON_PRESS = "on_press"
|
||||
CONF_ON_RAW_VALUE = "on_raw_value"
|
||||
|
|
Loading…
Reference in a new issue