Skip to content

Commit

Permalink
Bang & Olufsen add overlay/announce play_media functionality (home-as…
Browse files Browse the repository at this point in the history
…sistant#113434)

* Add overlay service

* Convert custom service to play_media announce

* Remove debugging
  • Loading branch information
mj23000 authored Jun 4, 2024
1 parent 0aac4b2 commit 67e9e90
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions homeassistant/components/bang_olufsen/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class BangOlufsenMediaType(StrEnum):
DEEZER = "deezer"
RADIO = "radio"
TTS = "provider"
OVERLAY_TTS = "overlay_tts"


class BangOlufsenModel(StrEnum):
Expand Down Expand Up @@ -117,6 +118,7 @@ class WebsocketNotification(StrEnum):
BangOlufsenMediaType.DEEZER,
BangOlufsenMediaType.RADIO,
BangOlufsenMediaType.TTS,
BangOlufsenMediaType.OVERLAY_TTS,
MediaType.MUSIC,
MediaType.URL,
MediaType.CHANNEL,
Expand Down
41 changes: 39 additions & 2 deletions homeassistant/components/bang_olufsen/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Action,
Art,
OverlayPlayRequest,
OverlayPlayRequestTextToSpeechTextToSpeech,
PlaybackContentMetadata,
PlaybackError,
PlaybackProgress,
Expand Down Expand Up @@ -69,6 +70,7 @@
BANG_OLUFSEN_FEATURES = (
MediaPlayerEntityFeature.BROWSE_MEDIA
| MediaPlayerEntityFeature.CLEAR_PLAYLIST
| MediaPlayerEntityFeature.MEDIA_ANNOUNCE
| MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.PLAY
Expand Down Expand Up @@ -547,10 +549,10 @@ async def async_play_media(
self,
media_type: MediaType | str,
media_id: str,
announce: bool | None = None,
**kwargs: Any,
) -> None:
"""Play from: netradio station id, URI, favourite or Deezer."""

# Convert audio/mpeg, audio/aac etc. to MediaType.MUSIC
if media_type.startswith("audio/"):
media_type = MediaType.MUSIC
Expand All @@ -574,7 +576,42 @@ async def async_play_media(
if media_id.endswith(".m3u"):
media_id = media_id.replace(".m3u", "")

if media_type in (MediaType.URL, MediaType.MUSIC):
if announce:
extra = kwargs.get(ATTR_MEDIA_EXTRA, {})

absolute_volume = extra.get("overlay_absolute_volume", None)
offset_volume = extra.get("overlay_offset_volume", None)
tts_language = extra.get("overlay_tts_language", "en-us")

# Construct request
overlay_play_request = OverlayPlayRequest()

# Define volume level
if absolute_volume:
overlay_play_request.volume_absolute = absolute_volume

elif offset_volume:
# Ensure that the volume is not above 100
if not self._volume.level or not self._volume.level.level:
_LOGGER.warning("Error setting volume")
else:
overlay_play_request.volume_absolute = min(
self._volume.level.level + offset_volume, 100
)

if media_type == BangOlufsenMediaType.OVERLAY_TTS:
# Bang & Olufsen cloud TTS
overlay_play_request.text_to_speech = (
OverlayPlayRequestTextToSpeechTextToSpeech(
lang=tts_language, text=media_id
)
)
else:
overlay_play_request.uri = Uri(location=media_id)

await self._client.post_overlay_play(overlay_play_request)

elif media_type in (MediaType.URL, MediaType.MUSIC):
await self._client.post_uri_source(uri=Uri(location=media_id))

# The "provider" media_type may not be suitable for overlay all the time.
Expand Down

0 comments on commit 67e9e90

Please sign in to comment.