mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 12:21:04 -04:00
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:
co-authored by
Joost Lekkerkerker
Claude Opus 5.5
parent
6bcbfb10b2
commit
e572506ca9
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user