Migrate demo tts platform to config entry (#191738)

This commit is contained in:
epenet
2026-11-10 08:03:59 +01:00
committed by GitHub
parent fd48e95426
commit aa5c391cb8
5 changed files with 98 additions and 71 deletions
+1 -1
View File
@@ -47,6 +47,7 @@ COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM = [
Platform.SWITCH,
Platform.TEXT,
Platform.TIME,
Platform.TTS,
Platform.UPDATE,
Platform.VACUUM,
Platform.VALVE,
@@ -55,7 +56,6 @@ COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM = [
]
COMPONENTS_WITH_DEMO_PLATFORM = [
Platform.TTS,
Platform.IMAGE_PROCESSING,
Platform.DEVICE_TRACKER,
]
+15 -43
View File
@@ -3,60 +3,32 @@
import os
from typing import Any, override
import probatio
from homeassistant.components.tts import (
CONF_LANG,
PLATFORM_SCHEMA as TTS_PLATFORM_SCHEMA,
Provider,
TtsAudioType,
)
from homeassistant.components.tts import TextToSpeechEntity, TtsAudioType
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
SUPPORT_LANGUAGES = ["en", "de"]
DEFAULT_LANG = "en"
PLATFORM_SCHEMA = TTS_PLATFORM_SCHEMA.extend(
{probatio.Optional(CONF_LANG, default=DEFAULT_LANG): probatio.In(SUPPORT_LANGUAGES)}
)
def get_engine(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> Provider:
"""Set up Demo speech component."""
return DemoProvider(config.get(CONF_LANG, DEFAULT_LANG))
config_entry: ConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Demo speech platform via config entry."""
async_add_entities([DemoProviderEntity()])
class DemoProvider(Provider):
"""Demo speech API provider."""
class DemoProviderEntity(TextToSpeechEntity):
"""Demo speech API provider entity."""
def __init__(self, lang: str) -> None:
"""Initialize demo provider."""
self._lang = lang
self.name = "Demo"
@property
@override
def default_language(self) -> str:
"""Return the default language."""
return self._lang
@property
@override
def supported_languages(self) -> list[str]:
"""Return list of supported languages."""
return SUPPORT_LANGUAGES
@property
@override
def supported_options(self) -> list[str]:
"""Return list of supported options like voice, emotions."""
return ["voice", "age"]
_attr_name = "Demo TTS"
_attr_default_language = DEFAULT_LANG
_attr_supported_languages = SUPPORT_LANGUAGES
_attr_supported_options = ["voice", "age"]
@override
def get_tts_audio(
+3 -17
View File
@@ -14,7 +14,6 @@ from pychromecast.const import CAST_TYPE_CHROMECAST, CAST_TYPE_GROUP
import pytest
from pytest_homeassistant.common import (
MockConfigEntry,
assert_setup_component,
async_load_fixture,
mock_platform,
)
@@ -2100,12 +2099,7 @@ async def test_failed_cast_other_url(
) -> None:
"""Test warning when casting from internal_url fails."""
await async_setup_component(hass, "homeassistant", {})
with assert_setup_component(1, tts.DOMAIN):
assert await async_setup_component(
hass,
tts.DOMAIN,
{tts.DOMAIN: {"platform": "demo"}},
)
assert await async_setup_component(hass, tts.DOMAIN, {})
info = get_fake_chromecast_info()
chromecast, _ = await async_setup_media_player_cast(hass, info)
@@ -2129,10 +2123,7 @@ async def test_failed_cast_internal_url(
hass,
{"internal_url": "http://example.local:8123"},
)
with assert_setup_component(1, tts.DOMAIN):
assert await async_setup_component(
hass, tts.DOMAIN, {tts.DOMAIN: {"platform": "demo"}}
)
assert await async_setup_component(hass, tts.DOMAIN, {})
info = get_fake_chromecast_info()
chromecast, _ = await async_setup_media_player_cast(hass, info)
@@ -2159,12 +2150,7 @@ async def test_failed_cast_external_url(
hass,
{"external_url": "http://example.com:8123"},
)
with assert_setup_component(1, tts.DOMAIN):
assert await async_setup_component(
hass,
tts.DOMAIN,
{tts.DOMAIN: {"platform": "demo"}},
)
assert await async_setup_component(hass, tts.DOMAIN, {})
info = get_fake_chromecast_info()
chromecast, _ = await async_setup_media_player_cast(hass, info)
+70
View File
@@ -0,0 +1,70 @@
"""The tests for the demo tts component."""
from collections.abc import Generator
from pathlib import Path
from unittest.mock import patch
import pytest
from pytest_homeassistant.common import MockConfigEntry, async_mock_service
from homeassistant.components.demo import DOMAIN
from homeassistant.components.media_player import (
DOMAIN as MEDIA_PLAYER_DOMAIN,
SERVICE_PLAY_MEDIA,
)
from homeassistant.components.tts import (
DOMAIN as TTS_DOMAIN,
TextToSpeechService,
TextToSpeechServiceArgument,
async_get_media_source_audio,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
ENTITY_ID = "tts.demo_tts"
@pytest.fixture
def tts_only() -> Generator[None]:
"""Enable only the tts platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.TTS],
):
yield
@pytest.fixture(autouse=True)
async def setup_config_entry(
hass: HomeAssistant, tts_only: None, mock_tts_cache_dir: Path
) -> None:
"""Set up demo component from config entry."""
assert await async_setup_component(hass, "homeassistant", {})
config_entry = MockConfigEntry(domain=DOMAIN)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
async def test_speak(hass: HomeAssistant) -> None:
"""Test the demo entity returns the demo audio."""
calls = async_mock_service(hass, MEDIA_PLAYER_DOMAIN, SERVICE_PLAY_MEDIA)
await hass.services.async_call(
TTS_DOMAIN,
TextToSpeechService.SPEAK,
{
ATTR_ENTITY_ID: ENTITY_ID,
TextToSpeechServiceArgument.MEDIA_PLAYER_ENTITY_ID: "media_player.something",
TextToSpeechServiceArgument.MESSAGE: "There is someone at the door.",
},
blocking=True,
)
assert len(calls) == 1
extension, data = await async_get_media_source_audio(
hass, calls[0].data["media_content_id"]
)
assert extension == "mp3"
assert data
+9 -10
View File
@@ -14,7 +14,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.core_config import async_process_ha_core_config
from homeassistant.setup import async_setup_component
from .common import MockTTSEntity, mock_config_entry_setup
from .common import MockTTSEntity, MockTTSProvider, mock_config_entry_setup, mock_setup
@pytest.fixture(autouse=True)
@@ -42,7 +42,7 @@ async def test_setup_legacy_platform(hass: HomeAssistant) -> None:
notify.DOMAIN: {
"platform": "tts",
"name": "tts_test",
"tts_service": "tts.demo_say",
"tts_service": "tts.test_say",
"media_player": "media_player.demo",
}
}
@@ -86,25 +86,24 @@ async def test_setup_platform_missing_key(hass: HomeAssistant) -> None:
assert not hass.services.has_service(notify.DOMAIN, "tts_test")
async def test_setup_legacy_service(hass: HomeAssistant) -> None:
"""Set up the demo platform and call service."""
async def test_setup_legacy_service(
hass: HomeAssistant, mock_provider: MockTTSProvider
) -> None:
"""Set up the legacy platform and call service."""
calls = async_mock_service(hass, MP_DOMAIN, SERVICE_PLAY_MEDIA)
config = {
tts.DOMAIN: {"platform": "demo"},
notify.DOMAIN: {
"platform": "tts",
"name": "tts_test",
"tts_service": "tts.demo_say",
"tts_service": "tts.test_say",
"media_player": "media_player.demo",
"language": "en",
"language": "en_US",
},
}
await async_setup_component(hass, "homeassistant", {})
with assert_setup_component(1, tts.DOMAIN):
assert await async_setup_component(hass, tts.DOMAIN, config)
await mock_setup(hass, mock_provider)
with assert_setup_component(1, notify.DOMAIN):
assert await async_setup_component(hass, notify.DOMAIN, config)