From 42a29863156aa3d558fc929187d88dffe36707fe Mon Sep 17 00:00:00 2001 From: Michael Arthur Date: Fri, 14 Aug 2026 23:32:51 +1200 Subject: [PATCH] Electric Kiwi: bugfix connections empty not checked (#179119) --- .../components/electric_kiwi/sensor.py | 4 +-- .../account_summary_no_connections.json | 34 +++++++++++++++++++ tests/components/electric_kiwi/test_sensor.py | 28 +++++++++++++-- 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/components/electric_kiwi/fixtures/account_summary_no_connections.json diff --git a/homeassistant/components/electric_kiwi/sensor.py b/homeassistant/components/electric_kiwi/sensor.py index e8cad1c284b2..bac4d8487edc 100644 --- a/homeassistant/components/electric_kiwi/sensor.py +++ b/homeassistant/components/electric_kiwi/sensor.py @@ -46,8 +46,8 @@ class ElectricKiwiAccountSensorEntityDescription(SensorEntityDescription): def _get_hop_percentage(account_balance: AccountSummary) -> float: """Return the hop percentage from account summary.""" if power := account_balance.services.get("power"): - if connection := power.connections[0]: - return float(connection.hop_percentage) + if connections := power.connections: + return float(connections[0].hop_percentage) return 0.0 diff --git a/tests/components/electric_kiwi/fixtures/account_summary_no_connections.json b/tests/components/electric_kiwi/fixtures/account_summary_no_connections.json new file mode 100644 index 000000000000..a11ae1067dc3 --- /dev/null +++ b/tests/components/electric_kiwi/fixtures/account_summary_no_connections.json @@ -0,0 +1,34 @@ +{ + "data": { + "type": "account_summary", + "total_running_balance": "184.09", + "total_account_balance": "-102.22", + "total_billing_days": 31, + "next_billing_date": "2025-02-19", + "service_names": [], + "services": { + "power": { + "connections": [] + } + }, + "date_to_pay": "", + "invoice_id": "", + "total_invoiced_charges": "", + "default_to_pay": "", + "invoice_exists": 1, + "display_date": "2025-01-19", + "last_billed_date": "2025-01-18", + "last_billed_amount": "-21.02", + "summary": { + "electricity_used": "12.98", + "other_charges": "0.00", + "payments": "0.00", + "credits": "0.00", + "mobile_charges": "0.00", + "broadband_charges": "0.00", + "addon_unbilled_charges": {} + }, + "is_prepay": "N" + }, + "status": 1 +} diff --git a/tests/components/electric_kiwi/test_sensor.py b/tests/components/electric_kiwi/test_sensor.py index 3e58b33a998e..600a27b4b2e4 100644 --- a/tests/components/electric_kiwi/test_sensor.py +++ b/tests/components/electric_kiwi/test_sensor.py @@ -4,10 +4,11 @@ from datetime import UTC, datetime from unittest.mock import AsyncMock, Mock import zoneinfo +from electrickiwi_api.model import AccountSummary from freezegun import freeze_time import pytest -from homeassistant.components.electric_kiwi.const import ATTRIBUTION +from homeassistant.components.electric_kiwi.const import ATTRIBUTION, DOMAIN from homeassistant.components.electric_kiwi.sensor import _check_and_move_time from homeassistant.components.sensor import ( ATTR_STATE_CLASS, @@ -22,7 +23,7 @@ from homeassistant.util import dt as dt_util from . import init_integration -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, load_json_value_fixture DEFAULT_TIME_ZONE = dt_util.get_default_time_zone() TEST_TZ_NAME = "Pacific/Auckland" @@ -130,6 +131,29 @@ async def test_account_sensors( assert state.attributes.get(ATTR_STATE_CLASS) == state_class +async def test_hop_percentage_without_power_connections( + hass: HomeAssistant, + config_entry: MockConfigEntry, + electrickiwi_api: AsyncMock, + ek_auth: AsyncMock, +) -> None: + """Test the hour of power savings sensor when the account has no connections. + + An account that migrated to another provider keeps returning account data, + but without any power connections, so the sensor falls back to 0. + """ + electrickiwi_api.get_account_summary.return_value = AccountSummary.from_dict( + load_json_value_fixture("account_summary_no_connections.json", DOMAIN) + ) + + await init_integration(hass, config_entry) + assert config_entry.state is ConfigEntryState.LOADED + + state = hass.states.get("sensor.hour_of_power_savings") + assert state + assert state.state == "0.0" + + async def test_check_and_move_time(electrickiwi_api: AsyncMock) -> None: """Test correct time is returned depending on time of day.""" hop = await electrickiwi_api.get_hop()