Fix tesla_fleet media player volume step calculation (#175813)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Brett Adams
2026-07-07 08:03:50 +02:00
committed by GitHub
co-authored by Copilot Autofix powered by AI
parent ea131806ba
commit 5833c5d01d
2 changed files with 35 additions and 7 deletions
@@ -76,14 +76,11 @@ class TeslaFleetMediaEntity(TeslaFleetVehicleEntity, MediaPlayerEntity):
self._attr_state = STATES.get(
self.get("vehicle_state_media_info_media_playback_status") or "Off",
)
# volume_level is audio_volume / audio_volume_max, so one notch as a
# fraction of range is the per-notch increment divided by the max.
self._attr_volume_step = (
1.0
/ self._volume_max
/ (
self.get("vehicle_state_media_info_audio_volume_increment")
or VOLUME_STEP
)
)
self.get("vehicle_state_media_info_audio_volume_increment") or VOLUME_STEP
) / self._volume_max
if volume := self.get("vehicle_state_media_info_audio_volume"):
self._attr_volume_level = volume / self._volume_max
@@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from tesla_fleet_api.exceptions import VehicleOffline
@@ -13,6 +14,7 @@ from homeassistant.components.media_player import (
SERVICE_MEDIA_PLAY,
SERVICE_MEDIA_PREVIOUS_TRACK,
SERVICE_VOLUME_SET,
SERVICE_VOLUME_UP,
MediaPlayerState,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
@@ -37,6 +39,35 @@ async def test_media_player(
assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot)
async def test_media_player_volume_step(
hass: HomeAssistant,
normal_config_entry: MockConfigEntry,
) -> None:
"""Test volume_up raises the level by exactly one Tesla notch."""
await setup_platform(hass, normal_config_entry, [Platform.MEDIA_PLAYER])
entity_id = "media_player.test_media_player"
with patch(
"tesla_fleet_api.tesla.VehicleFleet.adjust_volume",
return_value=COMMAND_OK,
):
await hass.services.async_call(
MEDIA_PLAYER_DOMAIN,
SERVICE_VOLUME_UP,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
# One notch up from the vehicle_data fixture's audio_volume of 1.6667 in a
# 10.333333 range: (1.6667 + 0.333333) / 10.333333.
state = hass.states.get(entity_id)
assert state.attributes[ATTR_MEDIA_VOLUME_LEVEL] == pytest.approx(
0.1935516, abs=1e-4
)
async def test_media_player_alt(
hass: HomeAssistant,
snapshot: SnapshotAssertion,