Allow to play a LinkPlay preset (#125204)

* Allow to play a linkplay preset

* Make it an entity service

* Fixes

* PR feedback

* Rename more
This commit is contained in:
Simon Lamon 2024-09-11 12:54:12 +02:00 committed by GitHub
parent b8ce687ec2
commit b1698bc0d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,7 @@
{
"services": {
"play_preset": {
"service": "mdi:play-box-outline"
}
}
}

View File

@ -9,6 +9,7 @@ from typing import Any, Concatenate
from linkplay.bridge import LinkPlayBridge
from linkplay.consts import EqualizerMode, LoopMode, PlayingMode, PlayingStatus
from linkplay.exceptions import LinkPlayException, LinkPlayRequestException
import voluptuous as vol
from homeassistant.components import media_source
from homeassistant.components.media_player import (
@ -25,7 +26,11 @@ from homeassistant.components.media_player.browse_media import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_platform,
)
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utcnow
@ -109,6 +114,15 @@ SEEKABLE_FEATURES: MediaPlayerEntityFeature = (
| MediaPlayerEntityFeature.SEEK
)
SERVICE_PLAY_PRESET = "play_preset"
ATTR_PRESET_NUMBER = "preset_number"
SERVICE_PLAY_PRESET_SCHEMA = cv.make_entity_service_schema(
{
vol.Required(ATTR_PRESET_NUMBER): cv.positive_int,
}
)
async def async_setup_entry(
hass: HomeAssistant,
@ -117,6 +131,13 @@ async def async_setup_entry(
) -> None:
"""Set up a media player from a config entry."""
# register services
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
SERVICE_PLAY_PRESET, SERVICE_PLAY_PRESET_SCHEMA, "async_play_preset"
)
# add entities
async_add_entities([LinkPlayMediaPlayerEntity(entry.runtime_data.bridge)])
@ -262,6 +283,11 @@ class LinkPlayMediaPlayerEntity(MediaPlayerEntity):
url = async_process_play_media_url(self.hass, media_id)
await self._bridge.player.play(url)
@exception_wrap
async def async_play_preset(self, preset_number: int) -> None:
"""Play preset number."""
await self._bridge.player.play_preset(preset_number)
def _update_properties(self) -> None:
"""Update the properties of the media player."""
self._attr_available = True

View File

@ -0,0 +1,15 @@
play_preset:
target:
entity:
integration: linkplay
domain: media_player
fields:
preset_number:
example: 1
required: true
default: 1
selector:
number:
min: 1
max: 10
mode: box

View File

@ -22,5 +22,17 @@
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
}
},
"services": {
"play_preset": {
"name": "Play preset",
"description": "Play the preset number on the device.",
"fields": {
"preset_number": {
"name": "Preset number",
"description": "The preset number on the device to play."
}
}
}
}
}