From e572506ca9424c385ff93e3eaa1a33e6ef578cd5 Mon Sep 17 00:00:00 2001 From: Seb Ruiz Date: Thu, 15 Oct 2026 03:47:17 +1100 Subject: [PATCH] Fix IndexError on out-of-range heater levels in Tesla Fleet (#178049) Co-authored-by: Joost Lekkerkerker Co-authored-by: Claude Opus 5.5 --- .../components/tesla_fleet/select.py | 18 ++++++--- tests/components/tesla_fleet/test_select.py | 40 ++++++++++++++++++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/tesla_fleet/select.py b/homeassistant/components/tesla_fleet/select.py index e5ad9238f84a..432410294d8c 100644 --- a/homeassistant/components/tesla_fleet/select.py +++ b/homeassistant/components/tesla_fleet/select.py @@ -137,10 +137,13 @@ class TeslaFleetSeatHeaterSelectEntity(TeslaFleetVehicleEntity, SelectEntity): """Handle updated data from the coordinator.""" self._attr_available = self.entity_description.available_fn(self) value = self._value - if value is None: - self._attr_current_option = None + # Tesla reports levels above the highest option (for example 4) and + # "SNA" (signal not available) instead of a level. + if isinstance(value, int): + options = self._attr_options + self._attr_current_option = options[max(0, min(value, len(options) - 1))] else: - self._attr_current_option = self._attr_options[value] + self._attr_current_option = None @override async def async_select_option(self, option: str) -> None: @@ -184,10 +187,13 @@ class TeslaFleetWheelHeaterSelectEntity(TeslaFleetVehicleEntity, SelectEntity): """Handle updated data from the coordinator.""" value = self._value - if value is None: - self._attr_current_option = None + # Tesla reports levels above the highest option (for example 4) and + # "SNA" (signal not available) instead of a level. + if isinstance(value, int): + options = self._attr_options + self._attr_current_option = options[max(0, min(value, len(options) - 1))] else: - self._attr_current_option = self._attr_options[value] + self._attr_current_option = None @override async def async_select_option(self, option: str) -> None: diff --git a/tests/components/tesla_fleet/test_select.py b/tests/components/tesla_fleet/test_select.py index 5aa05ab7976b..4cd9f337db5d 100644 --- a/tests/components/tesla_fleet/test_select.py +++ b/tests/components/tesla_fleet/test_select.py @@ -1,5 +1,6 @@ """Test the Tesla Fleet select platform.""" +from copy import deepcopy from unittest.mock import AsyncMock, patch import pytest @@ -12,7 +13,7 @@ from homeassistant.components.select import ( DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) -from homeassistant.components.tesla_fleet.select import LOW +from homeassistant.components.tesla_fleet.select import HIGH, LOW from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -49,6 +50,43 @@ async def test_select_offline( assert state.state == STATE_UNKNOWN +@pytest.mark.parametrize( + ("field", "entity_id", "value"), + [ + pytest.param( + "steering_wheel_heat_level", + "select.test_steering_wheel_heater", + 3, + id="steering_wheel_heater", + ), + pytest.param( + "seat_heater_left", + "select.test_seat_heater_front_left", + 4, + id="seat_heater", + ), + ], +) +async def test_select_heat_level_out_of_range( + hass: HomeAssistant, + mock_vehicle_data: AsyncMock, + normal_config_entry: MockConfigEntry, + field: str, + entity_id: str, + value: int, +) -> None: + """Tests that a heat level beyond the last modeled option clamps to it.""" + + data = deepcopy(VEHICLE_DATA_ALT) + data["response"]["climate_state"][field] = value + mock_vehicle_data.return_value = data + + await setup_platform(hass, normal_config_entry, [Platform.SELECT]) + + state = hass.states.get(entity_id) + assert state.state == HIGH + + async def test_select_services( hass: HomeAssistant, mock_vehicle_data: AsyncMock,