1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-17 15:44:52 +01:00

Tesla Fleet: fix malformed energy live response handling (#165101)

This commit is contained in:
Brett Adams
2026-03-18 00:04:35 +10:00
committed by GitHub
parent fa7a216afe
commit 57f0fd2ed2
2 changed files with 51 additions and 2 deletions

View File

@@ -195,9 +195,22 @@ class TeslaFleetEnergySiteLiveCoordinator(DataUpdateCoordinator[dict[str, Any]])
except TeslaFleetError as e:
raise UpdateFailed(e.message) from e
if not isinstance(data, dict):
LOGGER.debug(
"%s got unexpected live status response type: %s",
self.name,
type(data).__name__,
)
return self.data
# Convert Wall Connectors from array to dict
wall_connectors = data.get("wall_connectors")
if not isinstance(wall_connectors, list):
wall_connectors = []
data["wall_connectors"] = {
wc["din"]: wc for wc in (data.get("wall_connectors") or [])
wc["din"]: wc
for wc in wall_connectors
if isinstance(wc, dict) and "din" in wc
}
self.updated_once = True

View File

@@ -41,7 +41,7 @@ from homeassistant.helpers.config_entry_oauth2_flow import (
from . import setup_platform
from .conftest import create_config_entry
from .const import VEHICLE_ASLEEP, VEHICLE_DATA_ALT
from .const import LIVE_STATUS, VEHICLE_ASLEEP, VEHICLE_DATA_ALT
from tests.common import MockConfigEntry, async_fire_time_changed
@@ -352,6 +352,42 @@ async def test_energy_live_refresh_error(
assert normal_config_entry.state is state
async def test_energy_live_refresh_bad_response(
hass: HomeAssistant,
normal_config_entry: MockConfigEntry,
mock_live_status: AsyncMock,
) -> None:
"""Test coordinator refresh with malformed live status payload."""
bad_live_status = deepcopy(LIVE_STATUS)
bad_live_status["response"] = "site data is unavailable"
mock_live_status.side_effect = None
mock_live_status.return_value = bad_live_status
await setup_platform(hass, normal_config_entry)
assert normal_config_entry.state is ConfigEntryState.LOADED
assert (state := hass.states.get("sensor.test_battery_level"))
assert state.state != "unavailable"
async def test_energy_live_refresh_bad_wall_connectors(
hass: HomeAssistant,
normal_config_entry: MockConfigEntry,
mock_live_status: AsyncMock,
) -> None:
"""Test coordinator refresh with malformed wall connector payload."""
bad_live_status = deepcopy(LIVE_STATUS)
bad_live_status["response"]["wall_connectors"] = "site data is unavailable"
mock_live_status.side_effect = None
mock_live_status.return_value = bad_live_status
await setup_platform(hass, normal_config_entry)
assert normal_config_entry.state is ConfigEntryState.LOADED
assert (state := hass.states.get("sensor.test_battery_level"))
assert state.state != "unavailable"
# Test Energy Site Coordinator
@pytest.mark.parametrize(("side_effect", "state"), ERRORS)
async def test_energy_site_refresh_error(