Use TargetKey and the service argument enums in tts (#194000)

This commit is contained in:
epenet
2026-11-24 12:28:13 +00:00
committed by GitHub
parent b14f1fd10c
commit 36cc5919bf
3 changed files with 28 additions and 29 deletions
+2 -2
View File
@@ -13,7 +13,7 @@ from homeassistant.components.media_player import (
MediaPlayerServiceArgument,
MediaType,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, TargetKey
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.restore_state import RestoreEntity
@@ -141,7 +141,7 @@ class TextToSpeechEntity(RestoreEntity, cached_properties=CACHED_PROPERTIES_WITH
MP_DOMAIN,
MediaPlayerService.PLAY_MEDIA,
{
ATTR_ENTITY_ID: media_player_entity_id,
TargetKey.ENTITY_ID: media_player_entity_id,
MediaPlayerServiceArgument.MEDIA_CONTENT_ID: generate_media_source_id(
self.hass,
message=message,
+14 -16
View File
@@ -17,10 +17,11 @@ from homeassistant.components.media_player import (
)
from homeassistant.config import config_per_platform
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DESCRIPTION,
CONF_NAME,
CONF_PLATFORM,
CommonServiceArgument,
TargetKey,
)
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv, discovery
@@ -34,10 +35,6 @@ from homeassistant.setup import (
from homeassistant.util.yaml import load_yaml_dict
from .const import (
ATTR_CACHE,
ATTR_LANGUAGE,
ATTR_MESSAGE,
ATTR_OPTIONS,
CONF_CACHE,
CONF_CACHE_DIR,
CONF_FIELDS,
@@ -47,6 +44,7 @@ from .const import (
DEFAULT_CACHE_DIR,
DEFAULT_TIME_MEMORY,
DOMAIN,
TextToSpeechServiceArgument,
TtsAudioType,
)
from .media_source import generate_media_source_id
@@ -84,11 +82,11 @@ SERVICE_SAY = "say"
SCHEMA_SERVICE_SAY = probatio.Schema(
{
probatio.Required(ATTR_MESSAGE): cv.string,
probatio.Optional(ATTR_CACHE): cv.boolean,
probatio.Required(ATTR_ENTITY_ID): cv.comp_entity_ids,
probatio.Optional(ATTR_LANGUAGE): cv.string,
probatio.Optional(ATTR_OPTIONS): dict,
probatio.Required(TextToSpeechServiceArgument.MESSAGE): cv.string,
probatio.Optional(TextToSpeechServiceArgument.CACHE): cv.boolean,
probatio.Required(CommonServiceArgument.ENTITY_ID): cv.comp_entity_ids,
probatio.Optional(TextToSpeechServiceArgument.LANGUAGE): cv.string,
probatio.Optional(TextToSpeechServiceArgument.OPTIONS): dict,
}
)
@@ -146,20 +144,20 @@ async def async_setup_legacy(
async def async_say_handle(service: ServiceCall) -> None:
"""Service handle for say."""
entity_ids = service.data[ATTR_ENTITY_ID]
entity_ids = service.data[CommonServiceArgument.ENTITY_ID]
await hass.services.async_call(
MP_DOMAIN,
MediaPlayerService.PLAY_MEDIA,
{
ATTR_ENTITY_ID: entity_ids,
TargetKey.ENTITY_ID: entity_ids,
MediaPlayerServiceArgument.MEDIA_CONTENT_ID: generate_media_source_id(
hass,
engine=p_type,
message=service.data[ATTR_MESSAGE],
language=service.data.get(ATTR_LANGUAGE),
options=service.data.get(ATTR_OPTIONS),
cache=service.data.get(ATTR_CACHE),
message=service.data[TextToSpeechServiceArgument.MESSAGE],
language=service.data.get(TextToSpeechServiceArgument.LANGUAGE),
options=service.data.get(TextToSpeechServiceArgument.OPTIONS),
cache=service.data.get(TextToSpeechServiceArgument.CACHE),
),
MediaPlayerServiceArgument.MEDIA_CONTENT_TYPE: MediaType.MUSIC,
MediaPlayerServiceArgument.ANNOUNCE: True,
+12 -11
View File
@@ -9,17 +9,18 @@ from homeassistant.components.notify import (
PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
BaseNotificationService,
)
from homeassistant.const import ATTR_ENTITY_ID, CONF_ENTITY_ID, CONF_NAME
from homeassistant.const import (
CONF_ENTITY_ID,
CONF_LANGUAGE,
CONF_NAME,
CommonServiceArgument,
TargetKey,
)
from homeassistant.core import HomeAssistant, split_entity_id
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import (
ATTR_LANGUAGE,
DOMAIN,
TextToSpeechService,
TextToSpeechServiceArgument,
)
from .const import DOMAIN, TextToSpeechService, TextToSpeechServiceArgument
CONF_MEDIA_PLAYER = "media_player"
CONF_TTS_SERVICE = "tts_service"
@@ -39,7 +40,7 @@ PLATFORM_SCHEMA = probatio.All(
CONF_ENTITY_ID, ENTITY_LEGACY_PROVIDER_GROUP
): cv.entities_domain(DOMAIN),
probatio.Required(CONF_MEDIA_PLAYER): cv.entity_id,
probatio.Optional(ATTR_LANGUAGE): cv.string,
probatio.Optional(CONF_LANGUAGE): cv.string,
}
),
)
@@ -67,7 +68,7 @@ class TTSNotificationService(BaseNotificationService):
else:
_, self._tts_service = split_entity_id(config[CONF_TTS_SERVICE])
self._media_player = config[CONF_MEDIA_PLAYER]
self._language = config.get(ATTR_LANGUAGE)
self._language = config.get(CONF_LANGUAGE)
@override
async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
@@ -80,10 +81,10 @@ class TTSNotificationService(BaseNotificationService):
service_name = ""
if self._tts_service:
data[ATTR_ENTITY_ID] = self._media_player
data[CommonServiceArgument.ENTITY_ID] = self._media_player
service_name = self._tts_service
elif self._target:
data[ATTR_ENTITY_ID] = self._target
data[TargetKey.ENTITY_ID] = self._target
data[TextToSpeechServiceArgument.MEDIA_PLAYER_ENTITY_ID] = (
self._media_player
)