mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +00:00
Fix energy history in Teslemetry (#147646)
This commit is contained in:
parent
e3ba1f34ca
commit
19d89c8952
@ -194,14 +194,14 @@ class TeslemetryEnergyHistoryCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
except TeslaFleetError as e:
|
||||
raise UpdateFailed(e.message) from e
|
||||
|
||||
if not data or not isinstance(data.get("time_series"), list):
|
||||
raise UpdateFailed("Received invalid data")
|
||||
|
||||
# Add all time periods together
|
||||
output = dict.fromkeys(ENERGY_HISTORY_FIELDS, None)
|
||||
for period in data.get("time_series", []):
|
||||
output = dict.fromkeys(ENERGY_HISTORY_FIELDS, 0)
|
||||
for period in data["time_series"]:
|
||||
for key in ENERGY_HISTORY_FIELDS:
|
||||
if key in period:
|
||||
if output[key] is None:
|
||||
output[key] = period[key]
|
||||
else:
|
||||
output[key] += period[key]
|
||||
|
||||
return output
|
||||
|
@ -20,6 +20,7 @@ VEHICLE_DATA_ALT = load_json_object_fixture("vehicle_data_alt.json", DOMAIN)
|
||||
LIVE_STATUS = load_json_object_fixture("live_status.json", DOMAIN)
|
||||
SITE_INFO = load_json_object_fixture("site_info.json", DOMAIN)
|
||||
ENERGY_HISTORY = load_json_object_fixture("energy_history.json", DOMAIN)
|
||||
ENERGY_HISTORY_EMPTY = load_json_object_fixture("energy_history_empty.json", DOMAIN)
|
||||
|
||||
COMMAND_OK = {"response": {"result": True, "reason": ""}}
|
||||
COMMAND_REASON = {"response": {"result": False, "reason": "already closed"}}
|
||||
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"response": {
|
||||
"serial_number": "xxxxxx",
|
||||
"period": "day",
|
||||
"installation_time_zone": "Australia/Brisbane",
|
||||
"time_series": null
|
||||
}
|
||||
}
|
@ -8,12 +8,13 @@ from syrupy.assertion import SnapshotAssertion
|
||||
from teslemetry_stream import Signal
|
||||
|
||||
from homeassistant.components.teslemetry.coordinator import VEHICLE_INTERVAL
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import STATE_UNAVAILABLE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import assert_entities, assert_entities_alt, setup_platform
|
||||
from .const import VEHICLE_DATA_ALT
|
||||
from .const import ENERGY_HISTORY_EMPTY, VEHICLE_DATA_ALT
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
@ -101,3 +102,28 @@ async def test_sensors_streaming(
|
||||
):
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == snapshot(name=f"{entity_id}-state")
|
||||
|
||||
|
||||
async def test_energy_history_no_time_series(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
mock_energy_history: AsyncMock,
|
||||
) -> None:
|
||||
"""Test energy history coordinator when time_series is not a list."""
|
||||
# Mock energy history to return data without time_series as a list
|
||||
|
||||
entry = await setup_platform(hass, [Platform.SENSOR])
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_id = "sensor.energy_site_battery_discharged"
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "0.036"
|
||||
|
||||
mock_energy_history.return_value = ENERGY_HISTORY_EMPTY
|
||||
|
||||
freezer.tick(VEHICLE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
Loading…
x
Reference in New Issue
Block a user