Electric Kiwi: bugfix connections empty not checked (#179119)

This commit is contained in:
Michael Arthur
2026-08-14 17:25:18 +02:00
committed by Bram Kragten
parent 148fdb527b
commit 82e30a000c
3 changed files with 62 additions and 4 deletions
@@ -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
@@ -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
}
+26 -2
View File
@@ -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()