mirror of
https://github.com/esphome/esphome.git
synced 2025-09-02 19:32:19 +01:00
[switch] Add trigger `on_state
` (#10108)
This commit is contained in:
@@ -10,6 +10,7 @@ from esphome.const import (
|
|||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_INVERTED,
|
CONF_INVERTED,
|
||||||
CONF_MQTT_ID,
|
CONF_MQTT_ID,
|
||||||
|
CONF_ON_STATE,
|
||||||
CONF_ON_TURN_OFF,
|
CONF_ON_TURN_OFF,
|
||||||
CONF_ON_TURN_ON,
|
CONF_ON_TURN_ON,
|
||||||
CONF_RESTORE_MODE,
|
CONF_RESTORE_MODE,
|
||||||
@@ -56,6 +57,9 @@ TurnOnAction = switch_ns.class_("TurnOnAction", automation.Action)
|
|||||||
SwitchPublishAction = switch_ns.class_("SwitchPublishAction", automation.Action)
|
SwitchPublishAction = switch_ns.class_("SwitchPublishAction", automation.Action)
|
||||||
|
|
||||||
SwitchCondition = switch_ns.class_("SwitchCondition", Condition)
|
SwitchCondition = switch_ns.class_("SwitchCondition", Condition)
|
||||||
|
SwitchStateTrigger = switch_ns.class_(
|
||||||
|
"SwitchStateTrigger", automation.Trigger.template(bool)
|
||||||
|
)
|
||||||
SwitchTurnOnTrigger = switch_ns.class_(
|
SwitchTurnOnTrigger = switch_ns.class_(
|
||||||
"SwitchTurnOnTrigger", automation.Trigger.template()
|
"SwitchTurnOnTrigger", automation.Trigger.template()
|
||||||
)
|
)
|
||||||
@@ -77,6 +81,11 @@ _SWITCH_SCHEMA = (
|
|||||||
cv.Optional(CONF_RESTORE_MODE, default="ALWAYS_OFF"): cv.enum(
|
cv.Optional(CONF_RESTORE_MODE, default="ALWAYS_OFF"): cv.enum(
|
||||||
RESTORE_MODES, upper=True, space="_"
|
RESTORE_MODES, upper=True, space="_"
|
||||||
),
|
),
|
||||||
|
cv.Optional(CONF_ON_STATE): automation.validate_automation(
|
||||||
|
{
|
||||||
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SwitchStateTrigger),
|
||||||
|
}
|
||||||
|
),
|
||||||
cv.Optional(CONF_ON_TURN_ON): automation.validate_automation(
|
cv.Optional(CONF_ON_TURN_ON): automation.validate_automation(
|
||||||
{
|
{
|
||||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SwitchTurnOnTrigger),
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SwitchTurnOnTrigger),
|
||||||
@@ -140,6 +149,9 @@ async def setup_switch_core_(var, config):
|
|||||||
|
|
||||||
if (inverted := config.get(CONF_INVERTED)) is not None:
|
if (inverted := config.get(CONF_INVERTED)) is not None:
|
||||||
cg.add(var.set_inverted(inverted))
|
cg.add(var.set_inverted(inverted))
|
||||||
|
for conf in config.get(CONF_ON_STATE, []):
|
||||||
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||||
|
await automation.build_automation(trigger, [(bool, "x")], conf)
|
||||||
for conf in config.get(CONF_ON_TURN_ON, []):
|
for conf in config.get(CONF_ON_TURN_ON, []):
|
||||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||||
await automation.build_automation(trigger, [], conf)
|
await automation.build_automation(trigger, [], conf)
|
||||||
|
@@ -64,6 +64,13 @@ template<typename... Ts> class SwitchCondition : public Condition<Ts...> {
|
|||||||
bool state_;
|
bool state_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SwitchStateTrigger : public Trigger<bool> {
|
||||||
|
public:
|
||||||
|
SwitchStateTrigger(Switch *a_switch) {
|
||||||
|
a_switch->add_on_state_callback([this](bool state) { this->trigger(state); });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class SwitchTurnOnTrigger : public Trigger<> {
|
class SwitchTurnOnTrigger : public Trigger<> {
|
||||||
public:
|
public:
|
||||||
SwitchTurnOnTrigger(Switch *a_switch) {
|
SwitchTurnOnTrigger(Switch *a_switch) {
|
||||||
|
@@ -9,6 +9,18 @@ switch:
|
|||||||
name: "Template Switch"
|
name: "Template Switch"
|
||||||
id: the_switch
|
id: the_switch
|
||||||
optimistic: true
|
optimistic: true
|
||||||
|
on_state:
|
||||||
|
- if:
|
||||||
|
condition:
|
||||||
|
- lambda: return x;
|
||||||
|
then:
|
||||||
|
- logger.log: "Switch turned ON"
|
||||||
|
else:
|
||||||
|
- logger.log: "Switch turned OFF"
|
||||||
|
on_turn_on:
|
||||||
|
- logger.log: "Switch is now ON"
|
||||||
|
on_turn_off:
|
||||||
|
- logger.log: "Switch is now OFF"
|
||||||
|
|
||||||
esphome:
|
esphome:
|
||||||
on_boot:
|
on_boot:
|
||||||
|
Reference in New Issue
Block a user