mirror of
https://github.com/esphome/esphome.git
synced 2026-09-27 23:10:21 +00:00
[sun] Use register_apply_condition for is_above_horizon and is_below_horizon (#19704)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -109,23 +109,4 @@ class SunTrigger final : public Trigger<>, public PollingComponent, public Paren
|
||||
double elevation_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SunCondition final : public Condition<Ts...>, public Parented<Sun> {
|
||||
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
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
sun: !include common.yaml
|
||||
actions: !include common-actions.yaml
|
||||
Reference in New Issue
Block a user