mirror of
https://github.com/esphome/esphome.git
synced 2026-11-10 23:25:58 +00:00
[sensor] Fix codegen crash for a delta baseline given as a number (#21811)
This commit is contained in:
1 parent
807dfa3abb
commit
b59018147e
4 files changed
+46
-4
No files matched your search
@@ -0,0 +1,16 @@
|
||||
"""Tests for the sensor delta filter baseline."""
|
||||
|
||||
from collections.abc import Callable
|
||||
import re
|
||||
|
||||
|
||||
def test_delta_baseline_number(generate_main: Callable[[str], str]) -> None:
|
||||
"""A plain number baseline, including 0, becomes a lambda returning it."""
|
||||
main_cpp = generate_main("tests/component_tests/sensor/test_delta_baseline.yaml")
|
||||
|
||||
# A user lambda body also carries #line directives around the return.
|
||||
for body in (r"return 20\.0f;", r"return 0\.0f;", r"return x \* 2\.0f;"):
|
||||
assert re.search(
|
||||
rf"set_baseline\(\[\]\(float x\) -> float \{{[^}}]*?{body}[^}}]*\}}\);",
|
||||
main_cpp,
|
||||
), body
|
||||
@@ -0,0 +1,19 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: d1_mini_lite
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
id: s_1
|
||||
filters:
|
||||
- delta:
|
||||
max_value: 3
|
||||
baseline: 20.0
|
||||
- delta:
|
||||
max_value: 3
|
||||
baseline: 0
|
||||
- delta:
|
||||
max_value: 3
|
||||
baseline: !lambda return x * 2.0f;
|
||||
Reference in new issue
Block a user