mirror of
https://github.com/esphome/esphome.git
synced 2026-09-30 08:20:21 +00:00
[dfrobot_sen0395] Fix output_latency treating milliseconds as seconds (#19873)
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
co-authored by
Jonathan Swoboda
parent
9b9d548ad5
commit
757a1c42f9
@@ -187,19 +187,18 @@ async def dfrobot_sen0395_settings_to_code(
|
||||
template_ = await cg.templatable(segments[7], args, cg.float_)
|
||||
cg.add(var.set_det_max4(template_))
|
||||
if CONF_OUTPUT_LATENCY in config:
|
||||
template_ = await cg.templatable(
|
||||
config[CONF_OUTPUT_LATENCY][CONF_DELAY_AFTER_DETECT], args, float
|
||||
)
|
||||
if isinstance(template_, cv.TimePeriod):
|
||||
template_ = template_.total_milliseconds / 1000
|
||||
cg.add(var.set_delay_after_detect(template_))
|
||||
|
||||
template_ = await cg.templatable(
|
||||
config[CONF_OUTPUT_LATENCY][CONF_DELAY_AFTER_DISAPPEAR], args, float
|
||||
)
|
||||
if isinstance(template_, cv.TimePeriod):
|
||||
template_ = template_.total_milliseconds / 1000
|
||||
cg.add(var.set_delay_after_disappear(template_))
|
||||
latency = config[CONF_OUTPUT_LATENCY]
|
||||
for key, setter in (
|
||||
(CONF_DELAY_AFTER_DETECT, var.set_delay_after_detect),
|
||||
(CONF_DELAY_AFTER_DISAPPEAR, var.set_delay_after_disappear),
|
||||
):
|
||||
template_ = await cg.templatable(
|
||||
latency[key],
|
||||
args,
|
||||
float,
|
||||
to_exp=lambda period: period.total_milliseconds / 1000,
|
||||
)
|
||||
cg.add(setter(template_))
|
||||
if CONF_SENSITIVITY in config:
|
||||
template_ = await cg.templatable(config[CONF_SENSITIVITY], args, cg.int8)
|
||||
cg.add(var.set_sensitivity(template_))
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
esphome:
|
||||
name: test
|
||||
on_boot:
|
||||
then:
|
||||
- dfrobot_sen0395.settings:
|
||||
id: mmwave
|
||||
output_latency:
|
||||
delay_after_detect: 0.1s
|
||||
delay_after_disappear: 5.0s
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
uart:
|
||||
tx_pin: GPIO17
|
||||
rx_pin: GPIO16
|
||||
baud_rate: 115200
|
||||
|
||||
dfrobot_sen0395:
|
||||
id: mmwave
|
||||
@@ -0,0 +1,25 @@
|
||||
"""Codegen tests for dfrobot_sen0395 output_latency units."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
|
||||
def _latency_return(main_cpp: str, setter: str) -> str:
|
||||
match = re.search(
|
||||
rf"{re.escape(setter)}\(\[\]\(\) -> float \{{\s*return ([^;]+);",
|
||||
main_cpp,
|
||||
)
|
||||
assert match is not None, f"{setter} not found in generated main"
|
||||
return match.group(1)
|
||||
|
||||
|
||||
def test_output_latency_emits_seconds_not_milliseconds(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
component_config_path: Callable[[str], Path],
|
||||
) -> None:
|
||||
"""YAML 0.1s / 5.0s must reach SetLatencyCommand as seconds, not milliseconds."""
|
||||
main_cpp = generate_main(component_config_path("output_latency.yaml"))
|
||||
|
||||
assert _latency_return(main_cpp, "set_delay_after_detect") == "0.1f"
|
||||
assert _latency_return(main_cpp, "set_delay_after_disappear") == "5.0f"
|
||||
Reference in New Issue
Block a user