mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[midea] Use register_apply_action for the eight midea_ac actions (#19625)
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if (defined(USE_ARDUINO) && !defined(USE_RP2) && !defined(USE_LIBRETINY)) || defined(USE_ESP_IDF)
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "air_conditioner.h"
|
||||
|
||||
namespace esphome::midea::ac {
|
||||
|
||||
template<typename... Ts> class MideaActionBase : public Action<Ts...> {
|
||||
public:
|
||||
void set_parent(AirConditioner *parent) { this->parent_ = parent; }
|
||||
|
||||
protected:
|
||||
AirConditioner *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class FollowMeAction : public MideaActionBase<Ts...> {
|
||||
TEMPLATABLE_VALUE(float, temperature)
|
||||
TEMPLATABLE_VALUE(bool, use_fahrenheit)
|
||||
TEMPLATABLE_VALUE(bool, beeper)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
this->parent_->do_follow_me(this->temperature_.value(x...), this->use_fahrenheit_.value(x...),
|
||||
this->beeper_.value(x...));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class SwingStepAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_swing_step(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class DisplayToggleAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_display_toggle(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class BeeperOnAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_beeper_on(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class BeeperOffAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_beeper_off(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class PowerOnAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_power_on(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class PowerOffAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_power_off(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class PowerToggleAction : public MideaActionBase<Ts...> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->do_power_toggle(); }
|
||||
};
|
||||
|
||||
} // namespace esphome::midea::ac
|
||||
|
||||
#endif // USE_ARDUINO || USE_ESP_IDF
|
||||
@@ -32,7 +32,7 @@ from esphome.const import (
|
||||
UNIT_PERCENT,
|
||||
UNIT_WATT,
|
||||
)
|
||||
from esphome.core import CORE, coroutine
|
||||
from esphome.core import CORE
|
||||
|
||||
CODEOWNERS = ["@dudanov"]
|
||||
DEPENDENCIES = ["climate", "uart"]
|
||||
@@ -53,25 +53,6 @@ def templatize(value):
|
||||
return cv.Schema(ret)
|
||||
|
||||
|
||||
def register_action(name, type_, schema):
|
||||
validator = templatize(schema).extend(MIDEA_ACTION_BASE_SCHEMA)
|
||||
registerer = automation.register_action(
|
||||
f"midea_ac.{name}", type_, validator, synchronous=True
|
||||
)
|
||||
|
||||
def decorator(func):
|
||||
async def new_func(config, action_id, template_arg, args):
|
||||
ac_ = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
cg.add(var.set_parent(ac_))
|
||||
await coroutine(func)(var, config, args)
|
||||
return var
|
||||
|
||||
return registerer(new_func)
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
ALLOWED_CLIMATE_MODES = {
|
||||
"HEAT_COOL": ClimateMode.CLIMATE_MODE_HEAT_COOL,
|
||||
"COOL": ClimateMode.CLIMATE_MODE_COOL,
|
||||
@@ -161,16 +142,6 @@ CONFIG_SCHEMA = cv.All(
|
||||
),
|
||||
)
|
||||
|
||||
# Actions
|
||||
FollowMeAction = midea_ac_ns.class_("FollowMeAction", automation.Action)
|
||||
DisplayToggleAction = midea_ac_ns.class_("DisplayToggleAction", automation.Action)
|
||||
SwingStepAction = midea_ac_ns.class_("SwingStepAction", automation.Action)
|
||||
BeeperOnAction = midea_ac_ns.class_("BeeperOnAction", automation.Action)
|
||||
BeeperOffAction = midea_ac_ns.class_("BeeperOffAction", automation.Action)
|
||||
PowerOnAction = midea_ac_ns.class_("PowerOnAction", automation.Action)
|
||||
PowerOffAction = midea_ac_ns.class_("PowerOffAction", automation.Action)
|
||||
PowerToggleAction = midea_ac_ns.class_("PowerToggleAction", automation.Action)
|
||||
|
||||
MIDEA_ACTION_BASE_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_ID): cv.use_id(AirConditioner),
|
||||
@@ -187,84 +158,31 @@ MIDEA_FOLLOW_ME_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
@register_action("follow_me", FollowMeAction, MIDEA_FOLLOW_ME_SCHEMA)
|
||||
async def follow_me_to_code(var, config, args):
|
||||
template_ = await cg.templatable(config[CONF_BEEPER], args, cg.bool_)
|
||||
cg.add(var.set_beeper(template_))
|
||||
template_ = await cg.templatable(config[CONF_USE_FAHRENHEIT], args, cg.bool_)
|
||||
cg.add(var.set_use_fahrenheit(template_))
|
||||
template_ = await cg.templatable(config[CONF_TEMPERATURE], args, cg.float_)
|
||||
cg.add(var.set_temperature(template_))
|
||||
|
||||
|
||||
# Toggle Display action
|
||||
@register_action(
|
||||
"display_toggle",
|
||||
DisplayToggleAction,
|
||||
cv.Schema({}),
|
||||
automation.register_apply_action(
|
||||
"midea_ac.follow_me",
|
||||
templatize(MIDEA_FOLLOW_ME_SCHEMA).extend(MIDEA_ACTION_BASE_SCHEMA),
|
||||
automation.ApplyCall(
|
||||
"do_follow_me({}, {}, {})",
|
||||
(
|
||||
(CONF_TEMPERATURE, cg.float_),
|
||||
(CONF_USE_FAHRENHEIT, cg.bool_),
|
||||
(CONF_BEEPER, cg.bool_),
|
||||
),
|
||||
),
|
||||
)
|
||||
async def display_toggle_to_code(var, config, args):
|
||||
pass
|
||||
|
||||
|
||||
# Swing Step action
|
||||
@register_action(
|
||||
"swing_step",
|
||||
SwingStepAction,
|
||||
cv.Schema({}),
|
||||
)
|
||||
async def swing_step_to_code(var, config, args):
|
||||
pass
|
||||
|
||||
|
||||
# Beeper On action
|
||||
@register_action(
|
||||
"beeper_on",
|
||||
BeeperOnAction,
|
||||
cv.Schema({}),
|
||||
)
|
||||
async def beeper_on_to_code(var, config, args):
|
||||
pass
|
||||
|
||||
|
||||
# Beeper Off action
|
||||
@register_action(
|
||||
"beeper_off",
|
||||
BeeperOffAction,
|
||||
cv.Schema({}),
|
||||
)
|
||||
async def beeper_off_to_code(var, config, args):
|
||||
pass
|
||||
|
||||
|
||||
# Power On action
|
||||
@register_action(
|
||||
"power_on",
|
||||
PowerOnAction,
|
||||
cv.Schema({}),
|
||||
)
|
||||
async def power_on_to_code(var, config, args):
|
||||
pass
|
||||
|
||||
|
||||
# Power Off action
|
||||
@register_action(
|
||||
"power_off",
|
||||
PowerOffAction,
|
||||
cv.Schema({}),
|
||||
)
|
||||
async def power_off_to_code(var, config, args):
|
||||
pass
|
||||
|
||||
|
||||
# Power Toggle action
|
||||
@register_action(
|
||||
"power_toggle",
|
||||
PowerToggleAction,
|
||||
cv.Schema({}),
|
||||
)
|
||||
async def power_inv_to_code(var, config, args):
|
||||
pass
|
||||
for _name, _call in (
|
||||
("midea_ac.display_toggle", "do_display_toggle()"),
|
||||
("midea_ac.swing_step", "do_swing_step()"),
|
||||
("midea_ac.beeper_on", "do_beeper_on()"),
|
||||
("midea_ac.beeper_off", "do_beeper_off()"),
|
||||
("midea_ac.power_on", "do_power_on()"),
|
||||
("midea_ac.power_off", "do_power_off()"),
|
||||
("midea_ac.power_toggle", "do_power_toggle()"),
|
||||
):
|
||||
automation.register_apply_action(
|
||||
_name, MIDEA_ACTION_BASE_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
button:
|
||||
- platform: template
|
||||
name: Midea Actions
|
||||
on_press:
|
||||
- midea_ac.follow_me:
|
||||
id: midea_unit
|
||||
temperature: 23.0
|
||||
beeper: true
|
||||
- midea_ac.follow_me:
|
||||
id: midea_unit
|
||||
temperature: !lambda return id(midea_unit).target_temperature + 1.0f;
|
||||
use_fahrenheit: !lambda return false;
|
||||
- midea_ac.display_toggle:
|
||||
id: midea_unit
|
||||
- midea_ac.swing_step:
|
||||
id: midea_unit
|
||||
- midea_ac.beeper_on:
|
||||
id: midea_unit
|
||||
- midea_ac.beeper_off:
|
||||
id: midea_unit
|
||||
- midea_ac.power_on:
|
||||
id: midea_unit
|
||||
- midea_ac.power_off:
|
||||
id: midea_unit
|
||||
- midea_ac.power_toggle:
|
||||
id: midea_unit
|
||||
@@ -0,0 +1,9 @@
|
||||
packages:
|
||||
remote_transmitter: !include ../../test_build_components/common/remote_transmitter/esp8266-ard.yaml
|
||||
uart: !include ../../test_build_components/common/uart/esp8266-ard.yaml
|
||||
midea: !include common.yaml
|
||||
actions: !include common-actions.yaml
|
||||
|
||||
wifi:
|
||||
ssid: MySSID
|
||||
password: password1
|
||||
Reference in New Issue
Block a user