Fix IndexError on out-of-range heater levels in Tesla Fleet by sebr · Pull Request #178049 · home-assistant/core · GitHub
Skip to content

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

Merged
silamon merged 4 commits into
home-assistant:devfrom
sebr:tesla_fleet-clamp-heater-levels
Oct 14, 2026
Merged

silamon merged 4 commits into
home-assistant:devfrom
sebr:tesla_fleet-clamp-heater-levels

Conversation

@sebr

@sebr sebr commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Proposed change

Tesla can report a seat or steering wheel heat level higher than the options the select entity models. Both select entities indexed their option list with that raw level, so a steering wheel level of 3 (three options modelled) raised IndexError inside _async_update_attrs. That runs in the coordinator callback, so the update for that listener died:

File "homeassistant/components/tesla_fleet/select.py", line 190, in _async_update_attrs
    self._attr_current_option = self._attr_options[value]
IndexError: list index out of range

This clamps the reported level into the modelled range instead, guarded by isinstance, so an unknown level maps to the nearest known option and None still maps to unknown. The seat heater got the same guard — it models four levels, so it is less exposed, but it fails the same way on any level >= 4.

Teslemetry had the same defect and took the same fix in #175815; the two integrations now read the same.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Tesla can report a seat or steering wheel heat level higher than the
options the select entity models. Indexing the option list with that raw
level raised IndexError inside the coordinator update, so the entity
stopped updating.

Clamp the level into the modeled range instead, matching the fix already
made in Teslemetry.

Fixes home-assistant#178048
Copilot AI balanced review requested due to automatic review settings August 3, 2026 03:46
@home-assistant

home-assistant Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents Tesla Fleet heater select entities from crashing on out-of-range levels.

Changes:

  • Clamps seat and steering-wheel heater levels to valid options.
  • Adds regression tests for both entity types.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
homeassistant/components/tesla_fleet/select.py Safely maps reported heater levels to modeled options.
tests/components/tesla_fleet/test_select.py Tests upper out-of-range heater levels.

@sebr
sebr marked this pull request as ready for review August 3, 2026 03:57
Copilot AI review requested due to automatic review settings August 4, 2026 06:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comment on lines +140 to +144
# Defensive clamp: Tesla could report a level outside the modeled
# range, so map it to the nearest known option rather than erroring.
if isinstance(value, int):
options = self._attr_options
self._attr_current_option = options[max(0, min(value, len(options) - 1))]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so 2 things:

  1. Do we expect the value to not be an int? Or is this defensive?
  2. I think we should rewrite the comment, as it now reads like "it could happen", but according to your report it happens. AI really likes to be "save" and defensive, producing very verbose code as it tries to check everything, so I always try to cut down on that. So I think instead we should be more concrete, it can happen and we guard that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joostlek this is the identical fix ported from the Teslemetry integration. It was put up by @Bre77 and approved by yourself last month: #175815

I don't mind if it changes but perhaps this could be considered more holistically so that it is consistent across the code base? Perhaps better for @Bre77 to take over if you both agree?

@Bre77 Bre77 Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything I know is less certain here because this integration uses a different path. It is more possible to not be an int here (compared to Teslemetry), but I do know its value can be SNA or 4 so this fix should be right.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SNA?

In any case, I think the comment should be updated to indicate that this happens, instead of sounding like something imaginary

@home-assistant

home-assistant Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@home-assistant
home-assistant Bot marked this pull request as draft August 4, 2026 15:40
@sebr
sebr marked this pull request as ready for review August 5, 2026 00:09
@home-assistant
home-assistant Bot requested a review from joostlek August 5, 2026 00:09
Copilot AI balanced review requested due to automatic review settings October 14, 2026 16:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix matches the established Teslemetry implementation and includes adequate regression coverage.

0 open findings

🧠 Review effort: Balanced

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 16:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The fix matches the established Teslemetry behavior and covers both affected entity types.

0 open findings

🧠 Review effort: Balanced

@silamon
silamon merged commit 4e1caca into home-assistant:dev Oct 14, 2026
35 of 36 checks passed
@joostlek joostlek added this to the 2026.10.2 milestone Oct 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tesla Fleet: steering wheel heater select raises IndexError on out-of-range heat level

6 participants