mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[switch] Use register_apply_condition for switch.is_on and switch.is_off (#19553)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from esphome import automation
|
||||
from esphome.automation import Condition, maybe_simple_id
|
||||
from esphome.automation import maybe_simple_id
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import mqtt, web_server, zigbee
|
||||
import esphome.config_validation as cv
|
||||
@@ -54,7 +54,6 @@ RESTORE_MODES = {
|
||||
}
|
||||
|
||||
|
||||
SwitchCondition = switch_ns.class_("SwitchCondition", Condition)
|
||||
validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True)
|
||||
|
||||
|
||||
@@ -201,17 +200,10 @@ for _name, _call in (
|
||||
_name, SWITCH_ACTION_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
|
||||
@automation.register_condition("switch.is_on", SwitchCondition, SWITCH_ACTION_SCHEMA)
|
||||
async def switch_is_on_to_code(config, condition_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(condition_id, template_arg, paren, True)
|
||||
|
||||
|
||||
@automation.register_condition("switch.is_off", SwitchCondition, SWITCH_ACTION_SCHEMA)
|
||||
async def switch_is_off_to_code(config, condition_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(condition_id, template_arg, paren, False)
|
||||
automation.register_apply_condition("switch.is_on", SWITCH_ACTION_SCHEMA, "state")
|
||||
automation.register_apply_condition(
|
||||
"switch.is_off", SWITCH_ACTION_SCHEMA, "state == false"
|
||||
)
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.CORE)
|
||||
|
||||
@@ -6,16 +6,6 @@
|
||||
|
||||
namespace esphome::switch_ {
|
||||
|
||||
template<typename... Ts> class SwitchCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
SwitchCondition(Switch *parent, bool state) : parent_(parent), state_(state) {}
|
||||
bool check(const Ts &...x) override { return this->parent_->state == this->state_; }
|
||||
|
||||
protected:
|
||||
Switch *parent_;
|
||||
bool state_;
|
||||
};
|
||||
|
||||
class SwitchStateTrigger final : public Trigger<bool> {
|
||||
public:
|
||||
SwitchStateTrigger(Switch *a_switch) {
|
||||
|
||||
@@ -29,3 +29,13 @@ esphome:
|
||||
- switch.control:
|
||||
id: the_switch
|
||||
state: !lambda return (1 > 2);
|
||||
- if:
|
||||
condition:
|
||||
switch.is_on: the_switch
|
||||
then:
|
||||
- logger.log: "Switch is on"
|
||||
- if:
|
||||
condition:
|
||||
switch.is_off: the_switch
|
||||
then:
|
||||
- logger.log: "Switch is off"
|
||||
|
||||
Reference in New Issue
Block a user