Move trailing null removal from client-side to get_chartdata service

Co-authored-by: jpawlowski <75446+jpawlowski@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-01 01:27:21 +00:00
parent fd73dbbb1b
commit fe32491938
2 changed files with 7 additions and 6 deletions

View file

@ -131,9 +131,6 @@ async def handle_apexcharts_yaml(call: ServiceCall) -> dict[str, Any]:
else:
filter_param = f"level_filter: ['{level_key}']"
# Data generator fetches chart data and removes trailing nulls for proper header display.
# ApexCharts in_header shows last value, so trailing nulls cause "N/A".
# The pop loop safely removes trailing null price values ([timestamp, null]) from the end.
data_generator = (
f"const response = await hass.callWS({{ "
f"type: 'call_service', "
@ -142,9 +139,7 @@ async def handle_apexcharts_yaml(call: ServiceCall) -> dict[str, Any]:
f"return_response: true, "
f"service_data: {{ entry_id: '{entry_id}', day: ['{day}'], {filter_param}, "
f"output_format: 'array_of_arrays', insert_nulls: 'segments', minor_currency: true }} }}); "
f"const data = response.response.data; "
f"while (data.length > 0 && data[data.length - 1]?.[1] === null) data.pop(); "
f"return data;"
f"return response.response.data;"
)
# Only show extremas for HIGH and LOW levels (not NORMAL)
show_extremas = level_key != "NORMAL"

View file

@ -531,6 +531,12 @@ async def handle_chartdata(call: ServiceCall) -> dict[str, Any]: # noqa: PLR091
)
)
# Remove trailing null values from chart_data (for proper ApexCharts header display).
# Internal nulls at segment boundaries are preserved for gap visualization.
# Only trailing nulls cause issues with in_header showing "N/A".
while chart_data and chart_data[-1].get(price_field) is None:
chart_data.pop()
# Convert to array of arrays format if requested
if output_format == "array_of_arrays":
array_fields_template = call.data.get("array_fields")