Skip to content

Commit

Permalink
Add announcements support (#2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt authored Mar 17, 2024
1 parent ace1a3f commit 6461e5d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
8 changes: 8 additions & 0 deletions custom_components/mass/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CONF_ASSIST_AUTO_EXPOSE_PLAYERS,
CONF_INTEGRATION_CREATED_ADDON,
CONF_OPENAI_AGENT_ID,
CONF_PRE_ANNOUNCE_TTS,
CONF_USE_ADDON,
DOMAIN,
LOGGER,
Expand Down Expand Up @@ -264,6 +265,9 @@ async def async_step_zeroconf(
This flow is triggered by the Zeroconf component. It will check if the
host is already configured and delegate to the import step if not.
"""
# abort if discovery info is not what we expect
if "server_id" not in discovery_info.properties:
return None
# abort if we already have exactly this server_id
# reload the integration if the host got updated
server_id = discovery_info.properties["server_id"]
Expand Down Expand Up @@ -435,6 +439,10 @@ def mass_config_option_schema(
else False
),
): bool,
vol.Optional(
CONF_PRE_ANNOUNCE_TTS,
default=True,
): bool,
}


Expand Down
1 change: 1 addition & 0 deletions custom_components/mass/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
CONF_USE_ADDON = "use_addon"
CONF_OPENAI_AGENT_ID = "openai_agent_id"
CONF_ASSIST_AUTO_EXPOSE_PLAYERS = "expose_players_assist"
CONF_PRE_ANNOUNCE_TTS = "pre_announce_tts"

LOGGER = logging.getLogger(__package__)
2 changes: 1 addition & 1 deletion custom_components/mass/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"music_assistant"
],
"requirements": [
"music-assistant==2.0.0b110"
"music-assistant==2.0.0b112"
],
"version": "0.0.0",
"zeroconf": [
Expand Down
21 changes: 11 additions & 10 deletions custom_components/mass/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
ATTR_QUEUE_INDEX,
ATTR_QUEUE_ITEMS,
ATTR_STREAM_TITLE,
CONF_PRE_ANNOUNCE_TTS,
DOMAIN,
)
from .entity import MassBaseEntity
Expand Down Expand Up @@ -471,6 +472,16 @@ async def _async_play_media_advanced(
) -> None:
"""Send the play_media command to the media player."""
# pylint: disable=too-many-arguments
# announce/alert support
if announce:
conf_entry = self.hass.config_entries.async_get_entry(
self.registry_entry.config_entry_id
)
use_pre_announce = conf_entry.options.get(CONF_PRE_ANNOUNCE_TTS, True)
await self.mass.players.play_announcement(
self.player_id, media_id[0], use_pre_announce
)
return
media_uris: list[str] = []
# work out (all) uri(s) to play
for media_id_str in media_id:
Expand Down Expand Up @@ -503,16 +514,6 @@ async def _async_play_media_advanced(
else:
queue_id = self.player_id

# announce/alert support (WIP)
if announce and radio_mode:
radio_mode = None
if announce is None and "/api/tts_proxy" in media_id:
announce = True
if announce:
raise NotImplementedError(
"Music Assistant does not yet support announcements"
)

await self.mass.players.play_media(
queue_id, media=media_uris, option=enqueue, radio_mode=radio_mode
)
Expand Down
7 changes: 4 additions & 3 deletions custom_components/mass/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"error": {
"cannot_connect": "Failed to connect",
"unknown": "Unexpected error"
},
},
"abort": {
"addon_get_discovery_info_failed": "Failed to get Music Assistant Server add-on discovery info.",
"addon_info_failed": "Failed to get Music Assistant Server add-on info.",
Expand All @@ -64,7 +64,7 @@
"not_matter_addon": "Discovered add-on is not the official Music Assistant Server add-on.",
"reconfiguration_successful": "Successfully reconfigured the Music Assistant integration.",
"cannot_connect": "Failed to connect"
},
},
"progress": {
"install_addon": "Please wait while the Music Assistant Server add-on installation finishes. This can take several minutes.",
"start_addon": "Please wait while the Music Assistant Server add-on starts. This add-on is what powers Music Assistant in Home Assistant. This may take some seconds."
Expand Down Expand Up @@ -150,7 +150,8 @@
"data": {
"url": "URL of the Music Assistant server",
"use_addon": "Use the official Music Assistant Server add-on",
"expose_players_assist": "Expose players to Assist"
"expose_players_assist": "Expose players to Assist",
"pre_announce_tts": "Prepend notification sound to TTS announcements"
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions custom_components/mass/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"error": {
"cannot_connect": "Failed to connect",
"unknown": "Unexpected error"
},
},
"abort": {
"addon_get_discovery_info_failed": "Failed to get Music Assistant Server add-on discovery info.",
"addon_info_failed": "Failed to get Music Assistant Server add-on info.",
Expand All @@ -64,7 +64,7 @@
"not_matter_addon": "Discovered add-on is not the official Music Assistant Server add-on.",
"reconfiguration_successful": "Successfully reconfigured the Music Assistant integration.",
"cannot_connect": "Failed to connect"
},
},
"progress": {
"install_addon": "Please wait while the Music Assistant Server add-on installation finishes. This can take several minutes.",
"start_addon": "Please wait while the Music Assistant Server add-on starts. This add-on is what powers Music Assistant in Home Assistant. This may take some seconds."
Expand Down Expand Up @@ -150,7 +150,8 @@
"data": {
"url": "URL of the Music Assistant server",
"use_addon": "Use the official Music Assistant Server add-on",
"expose_players_assist": "Expose players to Assist"
"expose_players_assist": "Expose players to Assist",
"pre_announce_tts": "Prepend notification sound to TTS announcements"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ classifiers = [
dependencies = [
"async_timeout~=4.0.3",
"homeassistant~=2024.3.1",
"music-assistant~=2.0.0b93",
"music-assistant~=2.0.0b112",
]
description = "Music Assistant (BETA) Home Assistant Integration"
license = {text = "Apache-2.0"}
Expand All @@ -31,7 +31,7 @@ test = [
"safety==3.0.1",
"tomli==2.0.1",
"pytest-homeassistant-custom-component==0.13.108",
"zeroconf"
"zeroconf",
]

[tool.codespell]
Expand Down

0 comments on commit 6461e5d

Please sign in to comment.