Add media_player add off on capability to esphome (#147990)

This commit is contained in:
rwrozelle
2025-08-14 13:07:01 -04:00
committed by GitHub
parent 6e98446523
commit 1ea740d81c
2 changed files with 49 additions and 2 deletions

View File

@@ -51,6 +51,8 @@ _STATES: EsphomeEnumMapper[EspMediaPlayerState, MediaPlayerState] = EsphomeEnumM
EspMediaPlayerState.IDLE: MediaPlayerState.IDLE,
EspMediaPlayerState.PLAYING: MediaPlayerState.PLAYING,
EspMediaPlayerState.PAUSED: MediaPlayerState.PAUSED,
EspMediaPlayerState.OFF: MediaPlayerState.OFF,
EspMediaPlayerState.ON: MediaPlayerState.ON,
}
)
@@ -279,6 +281,24 @@ class EsphomeMediaPlayer(
device_id=self._static_info.device_id,
)
@convert_api_error_ha_error
async def async_turn_on(self) -> None:
"""Send turn on command."""
self._client.media_player_command(
self._key,
command=MediaPlayerCommand.TURN_ON,
device_id=self._static_info.device_id,
)
@convert_api_error_ha_error
async def async_turn_off(self) -> None:
"""Send turn off command."""
self._client.media_player_command(
self._key,
command=MediaPlayerCommand.TURN_OFF,
device_id=self._static_info.device_id,
)
def _is_url(url: str) -> bool:
"""Validate the URL can be parsed and at least has scheme + netloc."""

View File

@@ -27,6 +27,8 @@ from homeassistant.components.media_player import (
SERVICE_MEDIA_PLAY,
SERVICE_MEDIA_STOP,
SERVICE_PLAY_MEDIA,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
SERVICE_VOLUME_MUTE,
SERVICE_VOLUME_SET,
STATE_PLAYING,
@@ -57,8 +59,8 @@ async def test_media_player_entity(
key=1,
name="my media_player",
supports_pause=True,
# PLAY_MEDIA,BROWSE_MEDIA,STOP,VOLUME_SET,VOLUME_MUTE,MEDIA_ANNOUNCE,PAUSE,PLAY
feature_flags=1200653,
# PLAY_MEDIA,BROWSE_MEDIA,STOP,VOLUME_SET,VOLUME_MUTE,MEDIA_ANNOUNCE,PAUSE,PLAY,TURN_OFF,TURN_ON
feature_flags=1201037,
)
]
states = [
@@ -158,6 +160,31 @@ async def test_media_player_entity(
)
mock_client.media_player_command.reset_mock()
await hass.services.async_call(
MEDIA_PLAYER_DOMAIN,
SERVICE_TURN_OFF,
{
ATTR_ENTITY_ID: "media_player.test_my_media_player",
},
blocking=True,
)
mock_client.media_player_command.assert_has_calls(
[call(1, command=MediaPlayerCommand.TURN_OFF, device_id=0)]
)
await hass.services.async_call(
MEDIA_PLAYER_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "media_player.test_my_media_player",
},
blocking=True,
)
mock_client.media_player_command.assert_has_calls(
[call(1, command=MediaPlayerCommand.TURN_ON, device_id=0)]
)
mock_client.media_player_command.reset_mock()
async def test_media_player_entity_with_undefined_flags(
hass: HomeAssistant,