mirror of
https://github.com/esphome/esphome.git
synced 2026-09-17 18:18:43 +00:00
[output] Skip the power limit setters when they match the defaults (#19225)
This commit is contained in:
@@ -53,12 +53,17 @@ async def setup_output_platform_(obj, config):
|
||||
if CONF_POWER_SUPPLY in config:
|
||||
power_supply_ = await cg.get_variable(config[CONF_POWER_SUPPLY])
|
||||
cg.add(obj.set_power_supply(power_supply_))
|
||||
if CONF_MAX_POWER in config:
|
||||
# The C++ initializers are max_power 1.0 and min_power 0.0; skip the setter when
|
||||
# the config matches them. The define stays whenever the key is present because
|
||||
# platforms such as ac_dimmer read the scaling fields directly.
|
||||
if (max_power := config.get(CONF_MAX_POWER)) is not None:
|
||||
cg.add_define("USE_OUTPUT_FLOAT_POWER_SCALING")
|
||||
cg.add(obj.set_max_power(config[CONF_MAX_POWER]))
|
||||
if CONF_MIN_POWER in config:
|
||||
if max_power != 1.0:
|
||||
cg.add(obj.set_max_power(max_power))
|
||||
if (min_power := config.get(CONF_MIN_POWER)) is not None:
|
||||
cg.add_define("USE_OUTPUT_FLOAT_POWER_SCALING")
|
||||
cg.add(obj.set_min_power(config[CONF_MIN_POWER]))
|
||||
if min_power != 0.0:
|
||||
cg.add(obj.set_min_power(min_power))
|
||||
# Only emit when zero_means_zero is actually enabled. The schema defaults to False
|
||||
# so this key is always present; emitting unconditionally would force
|
||||
# USE_OUTPUT_FLOAT_POWER_SCALING on for every output, defeating the gate.
|
||||
|
||||
@@ -123,6 +123,7 @@ class FloatOutput : public BinaryOutput {
|
||||
virtual void write_state(float state) = 0;
|
||||
|
||||
#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
|
||||
// Codegen skips the setters for these values; keep in sync with output/__init__.py
|
||||
float max_power_{1.0f};
|
||||
float min_power_{0.0f};
|
||||
bool zero_means_zero_{false};
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
|
||||
output:
|
||||
- platform: ac_dimmer
|
||||
id: dimmer
|
||||
gate_pin: GPIO4
|
||||
zero_cross_pin: GPIO5
|
||||
min_power: 0%
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
|
||||
output:
|
||||
- platform: ledc
|
||||
id: default_power
|
||||
pin: GPIO4
|
||||
max_power: 100%
|
||||
min_power: 0%
|
||||
- platform: ledc
|
||||
id: custom_power
|
||||
pin: GPIO5
|
||||
max_power: 90%
|
||||
min_power: 1%
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Tests for the output platform codegen."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
from esphome.core import CORE
|
||||
|
||||
|
||||
def test_default_power_limits_are_not_emitted(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
component_config_path: Callable[[str], Path],
|
||||
) -> None:
|
||||
"""max_power 100% and min_power 0% already live in the C++ initializers."""
|
||||
main_cpp = generate_main(component_config_path("power_limits.yaml"))
|
||||
|
||||
assert "default_power->set_max_power(" not in main_cpp
|
||||
assert "default_power->set_min_power(" not in main_cpp
|
||||
assert "custom_power->set_max_power(0.9f);" in main_cpp
|
||||
assert "custom_power->set_min_power(0.01f);" in main_cpp
|
||||
assert "USE_OUTPUT_FLOAT_POWER_SCALING" in {d.name for d in CORE.defines}
|
||||
|
||||
|
||||
def test_default_min_power_keeps_scaling_fields(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
component_config_path: Callable[[str], Path],
|
||||
) -> None:
|
||||
"""ac_dimmer reads min_power_ directly, so the define must stay on for min_power 0%."""
|
||||
main_cpp = generate_main(component_config_path("ac_dimmer_min_power_zero.yaml"))
|
||||
|
||||
assert "dimmer->set_min_power(" not in main_cpp
|
||||
assert "USE_OUTPUT_FLOAT_POWER_SCALING" in {d.name for d in CORE.defines}
|
||||
@@ -4,3 +4,4 @@ output:
|
||||
gate_pin: ${gate_pin}
|
||||
zero_cross_pin: ${zero_cross_pin}
|
||||
zero_cross_interrupt_type: ANY
|
||||
min_power: 0%
|
||||
|
||||
Reference in New Issue
Block a user