From 88bb051f376eb600f6169b907e7490dda6acba13 Mon Sep 17 00:00:00 2001 From: kahrendt Date: Tue, 3 Oct 2023 05:58:11 -0400 Subject: [PATCH] Add xor automation condition (#5453) --- esphome/automation.py | 7 +++++++ esphome/core/base_automation.h | 16 +++++++++++++++ tests/test1.yaml | 36 ++++++++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/esphome/automation.py b/esphome/automation.py index d90a9cb99a..8475858a9c 100644 --- a/esphome/automation.py +++ b/esphome/automation.py @@ -141,6 +141,7 @@ AUTOMATION_SCHEMA = cv.Schema( AndCondition = cg.esphome_ns.class_("AndCondition", Condition) OrCondition = cg.esphome_ns.class_("OrCondition", Condition) NotCondition = cg.esphome_ns.class_("NotCondition", Condition) +XorCondition = cg.esphome_ns.class_("XorCondition", Condition) @register_condition("and", AndCondition, validate_condition_list) @@ -161,6 +162,12 @@ async def not_condition_to_code(config, condition_id, template_arg, args): return cg.new_Pvariable(condition_id, template_arg, condition) +@register_condition("xor", XorCondition, validate_condition_list) +async def xor_condition_to_code(config, condition_id, template_arg, args): + conditions = await build_condition_list(config, template_arg, args) + return cg.new_Pvariable(condition_id, template_arg, conditions) + + @register_condition("lambda", LambdaCondition, cv.returning_lambda) async def lambda_condition_to_code(config, condition_id, template_arg, args): lambda_ = await cg.process_lambda(config, args, return_type=bool) diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index af618af99a..9b3377f694 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -48,6 +48,22 @@ template class NotCondition : public Condition { Condition *condition_; }; +template class XorCondition : public Condition { + public: + explicit XorCondition(const std::vector *> &conditions) : conditions_(conditions) {} + bool check(Ts... x) override { + bool xor_state = false; + for (auto *condition : this->conditions_) { + xor_state = xor_state ^ condition->check(x...); + } + + return xor_state; + } + + protected: + std::vector *> conditions_; +}; + template class LambdaCondition : public Condition { public: explicit LambdaCondition(std::function &&f) : f_(std::move(f)) {} diff --git a/tests/test1.yaml b/tests/test1.yaml index b84aa21439..11ce86c5f3 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -3104,11 +3104,39 @@ time: - platform: ds1307 id: ds1307_time update_interval: never - on_time: - seconds: 0 - then: ds1307.read_time i2c_id: i2c_bus - + on_time: + - seconds: 0 + then: ds1307.read_time + - at: "16:00:00" + then: + - if: + condition: + or: + - binary_sensor.is_on: close_sensor + - binary_sensor.is_on: open_sensor + then: + logger.log: "close_sensor or open_sensor is on" + - if: + condition: + and: + - binary_sensor.is_on: close_sensor + - binary_sensor.is_on: open_sensor + then: + logger.log: "close_sensor and open_sensor are both on" + - if: + condition: + xor: + - binary_sensor.is_on: close_sensor + - binary_sensor.is_on: open_sensor + then: + logger.log: "close_sensor or open_sensor is exclusively on" + - if: + condition: + not: + - binary_sensor.is_on: close_sensor + then: + logger.log: "close_sensor is not on" cover: - platform: template name: Template Cover