From fd73dbbb1baff8ffdf86dbbc618fbca52fc9630a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 1 Dec 2025 00:56:09 +0000 Subject: [PATCH] Add optional chaining for safer array access in data_generator Co-authored-by: jpawlowski <75446+jpawlowski@users.noreply.github.com> --- .../tibber_prices/services/get_apexcharts_yaml.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/custom_components/tibber_prices/services/get_apexcharts_yaml.py b/custom_components/tibber_prices/services/get_apexcharts_yaml.py index c622ee6..f4b0998 100644 --- a/custom_components/tibber_prices/services/get_apexcharts_yaml.py +++ b/custom_components/tibber_prices/services/get_apexcharts_yaml.py @@ -131,8 +131,9 @@ 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") + # 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,7 +143,7 @@ async def handle_apexcharts_yaml(call: ServiceCall) -> dict[str, Any]: 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"while (data.length > 0 && data[data.length - 1]?.[1] === null) data.pop(); " f"return data;" ) # Only show extremas for HIGH and LOW levels (not NORMAL)