diff --git a/homeassistant/components/tesla_fleet/media_player.py b/homeassistant/components/tesla_fleet/media_player.py index 0df38ebf15b5..3b2ebac8d4e2 100644 --- a/homeassistant/components/tesla_fleet/media_player.py +++ b/homeassistant/components/tesla_fleet/media_player.py @@ -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 diff --git a/tests/components/tesla_fleet/test_media_player.py b/tests/components/tesla_fleet/test_media_player.py index 3233246b8b5d..a164d2cbc08c 100644 --- a/tests/components/tesla_fleet/test_media_player.py +++ b/tests/components/tesla_fleet/test_media_player.py @@ -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,