Fix IndexError on out-of-range heater levels in Tesla Fleet (#178049)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Seb Ruiz
2026-10-16 17:12:10 +00:00
committed by Franck Nijhof
co-authored by Joost Lekkerkerker Claude Opus 5.5
parent 6bcbfb10b2
commit e572506ca9
2 changed files with 51 additions and 7 deletions
+12 -6
View File
@@ -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:
+39 -1
View File
@@ -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,