1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-23 12:13:49 +01:00
This commit is contained in:
J. Nick Koston
2025-10-15 19:23:01 -10:00
parent e3089ff0f6
commit a72c494b75

View File

@@ -453,8 +453,12 @@ async def skip_initial_filter_to_code(config, filter_id):
return cg.new_Pvariable(filter_id, config) return cg.new_Pvariable(filter_id, config)
@FILTER_REGISTRY.register("min", MinFilter, MIN_SCHEMA) def _create_sliding_window_filter(config, filter_id, sliding_class, streaming_class):
async def min_filter_to_code(config, filter_id): """Helper to create sliding window or streaming filter based on config.
When window_size == send_every, use streaming filter (O(1) memory).
Otherwise, use sliding window filter (O(n) memory).
"""
window_size = config[CONF_WINDOW_SIZE] window_size = config[CONF_WINDOW_SIZE]
send_every = config[CONF_SEND_EVERY] send_every = config[CONF_SEND_EVERY]
send_first_at = config[CONF_SEND_FIRST_AT] send_first_at = config[CONF_SEND_FIRST_AT]
@@ -462,17 +466,14 @@ async def min_filter_to_code(config, filter_id):
# Optimization: Use streaming filter for batch windows (window_size == send_every) # Optimization: Use streaming filter for batch windows (window_size == send_every)
# Saves 99.98% memory for large windows (e.g., 20KB → 4 bytes for window_size=5000) # Saves 99.98% memory for large windows (e.g., 20KB → 4 bytes for window_size=5000)
if window_size == send_every: if window_size == send_every:
return cg.new_Pvariable( return cg.new_Pvariable(filter_id, streaming_class, window_size, send_first_at)
filter_id, return cg.new_Pvariable(filter_id, window_size, send_every, send_first_at)
StreamingMinFilter,
window_size,
send_first_at, @FILTER_REGISTRY.register("min", MinFilter, MIN_SCHEMA)
) async def min_filter_to_code(config, filter_id):
return cg.new_Pvariable( return _create_sliding_window_filter(
filter_id, config, filter_id, MinFilter, StreamingMinFilter
config[CONF_WINDOW_SIZE],
config[CONF_SEND_EVERY],
config[CONF_SEND_FIRST_AT],
) )
@@ -490,24 +491,8 @@ MAX_SCHEMA = cv.All(
@FILTER_REGISTRY.register("max", MaxFilter, MAX_SCHEMA) @FILTER_REGISTRY.register("max", MaxFilter, MAX_SCHEMA)
async def max_filter_to_code(config, filter_id): async def max_filter_to_code(config, filter_id):
window_size = config[CONF_WINDOW_SIZE] return _create_sliding_window_filter(
send_every = config[CONF_SEND_EVERY] config, filter_id, MaxFilter, StreamingMaxFilter
send_first_at = config[CONF_SEND_FIRST_AT]
# Optimization: Use streaming filter for batch windows (window_size == send_every)
# Saves 99.98% memory for large windows (e.g., 20KB → 4 bytes for window_size=5000)
if window_size == send_every:
return cg.new_Pvariable(
filter_id,
StreamingMaxFilter,
window_size,
send_first_at,
)
return cg.new_Pvariable(
filter_id,
config[CONF_WINDOW_SIZE],
config[CONF_SEND_EVERY],
config[CONF_SEND_FIRST_AT],
) )
@@ -529,24 +514,11 @@ SLIDING_AVERAGE_SCHEMA = cv.All(
SLIDING_AVERAGE_SCHEMA, SLIDING_AVERAGE_SCHEMA,
) )
async def sliding_window_moving_average_filter_to_code(config, filter_id): async def sliding_window_moving_average_filter_to_code(config, filter_id):
window_size = config[CONF_WINDOW_SIZE] return _create_sliding_window_filter(
send_every = config[CONF_SEND_EVERY] config,
send_first_at = config[CONF_SEND_FIRST_AT]
# Optimization: Use streaming filter for batch windows (window_size == send_every)
# Saves 99.94% memory for large windows (e.g., 20KB → 12 bytes for window_size=5000)
if window_size == send_every:
return cg.new_Pvariable(
filter_id, filter_id,
SlidingWindowMovingAverageFilter,
StreamingMovingAverageFilter, StreamingMovingAverageFilter,
window_size,
send_first_at,
)
return cg.new_Pvariable(
filter_id,
config[CONF_WINDOW_SIZE],
config[CONF_SEND_EVERY],
config[CONF_SEND_FIRST_AT],
) )