From 2fc48a3d9e28c80d872b65fab77fff1074a28de8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Sep 2026 01:33:50 +0100 Subject: [PATCH] [sun] Use register_apply_condition for is_above_horizon and is_below_horizon (#19704) --- esphome/components/sun/__init__.py | 63 +++++-------------- esphome/components/sun/sun.h | 19 ------ tests/components/sun/common-actions.yaml | 21 +++++++ .../sun/test-actions.esp8266-ard.yaml | 3 + 4 files changed, 39 insertions(+), 67 deletions(-) create mode 100644 tests/components/sun/common-actions.yaml create mode 100644 tests/components/sun/test-actions.esp8266-ard.yaml diff --git a/esphome/components/sun/__init__.py b/esphome/components/sun/__init__.py index 33a5c677bd..73ddefe972 100644 --- a/esphome/components/sun/__init__.py +++ b/esphome/components/sun/__init__.py @@ -13,8 +13,6 @@ from esphome.const import ( CONF_TIME_ID, CONF_TRIGGER_ID, ) -from esphome.core import ID -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType CODEOWNERS = ["@OttoWinter"] @@ -24,7 +22,6 @@ Sun = sun_ns.class_("Sun") SunTrigger = sun_ns.class_( "SunTrigger", cg.PollingComponent, automation.Trigger.template() ) -SunCondition = sun_ns.class_("SunCondition", automation.Condition) CONF_SUN_ID = "sun_id" CONF_ELEVATION = "elevation" @@ -142,53 +139,23 @@ async def to_code(config: ConfigType) -> None: await automation.build_automation(trigger, [], conf) -@automation.register_condition( +SUN_HORIZON_CONDITION_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.use_id(Sun), + cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): cv.templatable( + elevation + ), + } +) + +automation.register_apply_condition( "sun.is_above_horizon", - SunCondition, - cv.Schema( - { - cv.GenerateID(): cv.use_id(Sun), - cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): cv.templatable( - elevation - ), - } - ), + SUN_HORIZON_CONDITION_SCHEMA, + automation.ApplyCall("elevation() > {}", ((CONF_ELEVATION, cg.double),)), ) -async def sun_above_horizon_to_code( - config: ConfigType, - condition_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - var = cg.new_Pvariable(condition_id, template_arg) - await cg.register_parented(var, config[CONF_ID]) - templ = await cg.templatable(config[CONF_ELEVATION], args, cg.double) - cg.add(var.set_elevation(templ)) - cg.add(var.set_above(True)) - return var - -@automation.register_condition( +automation.register_apply_condition( "sun.is_below_horizon", - SunCondition, - cv.Schema( - { - cv.GenerateID(): cv.use_id(Sun), - cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): cv.templatable( - elevation - ), - } - ), + SUN_HORIZON_CONDITION_SCHEMA, + automation.ApplyCall("elevation() < {}", ((CONF_ELEVATION, cg.double),)), ) -async def sun_below_horizon_to_code( - config: ConfigType, - condition_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - var = cg.new_Pvariable(condition_id, template_arg) - await cg.register_parented(var, config[CONF_ID]) - templ = await cg.templatable(config[CONF_ELEVATION], args, cg.double) - cg.add(var.set_elevation(templ)) - cg.add(var.set_above(False)) - return var diff --git a/esphome/components/sun/sun.h b/esphome/components/sun/sun.h index ea9e05042d..af2af15dd2 100644 --- a/esphome/components/sun/sun.h +++ b/esphome/components/sun/sun.h @@ -109,23 +109,4 @@ class SunTrigger final : public Trigger<>, public PollingComponent, public Paren double elevation_; }; -template class SunCondition final : public Condition, public Parented { - public: - TEMPLATABLE_VALUE(double, elevation); - void set_above(bool above) { above_ = above; } - - bool check(const Ts &...x) override { - double elevation = this->elevation_.value(x...); - double current = this->parent_->elevation(); - if (this->above_) { - return current > elevation; - } else { - return current < elevation; - } - } - - protected: - bool above_; -}; - } // namespace esphome::sun diff --git a/tests/components/sun/common-actions.yaml b/tests/components/sun/common-actions.yaml new file mode 100644 index 0000000000..460be69879 --- /dev/null +++ b/tests/components/sun/common-actions.yaml @@ -0,0 +1,21 @@ +button: + - platform: template + name: Sun position + on_press: + - if: + condition: + sun.is_above_horizon: + then: + - logger.log: Day + - if: + condition: + sun.is_below_horizon: + elevation: -6° + then: + - logger.log: Night + - if: + condition: + sun.is_above_horizon: + elevation: !lambda return 10.0; + then: + - logger.log: High sun diff --git a/tests/components/sun/test-actions.esp8266-ard.yaml b/tests/components/sun/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..82c874ac33 --- /dev/null +++ b/tests/components/sun/test-actions.esp8266-ard.yaml @@ -0,0 +1,3 @@ +packages: + sun: !include common.yaml + actions: !include common-actions.yaml