hass-core/tests/components/tts/test_legacy.py
J. Nick Koston 4f18f0d902
Fix setup timings when config entry platform loads are not awaited ()
* Move setup time logging into the context manager

We were fetching the time twice but since the context
manager already has the timing, move it there

* remove log setup assertions from integration test

* tweak logging to give us better data for tracking issues

* redundant

* adjust

* preen

* fixes

* adjust

* make api change internal so nobody uses it

* coverage

* fix test

* fix more tests

* coverage

* more tests assuming internal calls

* fix more

* adjust

* adjust

* fix axis tests

* fix broadlink -- it does not call async_forward_entry_setup

* missed some

* remove useless patch

* rename, detect it both ways

* clear

* debug

* try to fix

* handle phase finishing out while paused

* where its set does not need to know its late as that is an implemenation detail of setup

* where its set does not need to know its late as that is an implemenation detail of setup

* tweak

* simplify

* reduce complexity

* revert order change as it makes review harder

* revert naming changes as it makes review harder

* improve comment

* improve debug

* late dispatch test

* test the other way as well

* Update setup.py

* Update setup.py

* Update setup.py

* simplify

* reduce
2024-03-23 15:26:38 -04:00

166 lines
4.9 KiB
Python

"""Test the legacy tts setup."""
from __future__ import annotations
import pytest
from homeassistant.components.media_player import (
DOMAIN as DOMAIN_MP,
SERVICE_PLAY_MEDIA,
)
from homeassistant.components.tts import ATTR_MESSAGE, DOMAIN, Provider
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_setup_component
from .common import SUPPORT_LANGUAGES, MockProvider, MockTTS
from tests.common import (
MockModule,
assert_setup_component,
async_mock_service,
mock_integration,
mock_platform,
)
class DefaultProvider(Provider):
"""Test provider."""
@property
def supported_languages(self) -> list[str]:
"""Return a list of supported languages."""
return SUPPORT_LANGUAGES
async def test_default_provider_attributes() -> None:
"""Test default provider attributes."""
provider = DefaultProvider()
assert provider.hass is None
assert provider.name is None
assert provider.default_language is None
assert provider.supported_languages == SUPPORT_LANGUAGES
assert provider.supported_options is None
assert provider.default_options is None
assert provider.async_get_supported_voices("test") is None
async def test_deprecated_platform(hass: HomeAssistant) -> None:
"""Test deprecated google platform."""
with assert_setup_component(0, DOMAIN):
assert await async_setup_component(
hass, DOMAIN, {DOMAIN: {"platform": "google"}}
)
async def test_invalid_platform(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test platform setup with an invalid platform."""
await async_load_platform(
hass,
"tts",
"bad_tts",
{"tts": [{"platform": "bad_tts"}]},
hass_config={"tts": [{"platform": "bad_tts"}]},
)
await hass.async_block_till_done()
assert "Unknown text-to-speech platform specified" in caplog.text
async def test_platform_setup_without_provider(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_provider: MockProvider
) -> None:
"""Test platform setup without provider returned."""
class BadPlatform(MockTTS):
"""A mock TTS platform without provider."""
async def async_get_engine(
self,
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> Provider | None:
"""Raise exception during platform setup."""
return None
mock_integration(hass, MockModule(domain="bad_tts"))
mock_platform(hass, "bad_tts.tts", BadPlatform(mock_provider))
await async_load_platform(
hass,
"tts",
"bad_tts",
{},
hass_config={"tts": [{"platform": "bad_tts"}]},
)
await hass.async_block_till_done()
assert "Error setting up platform: bad_tts" in caplog.text
async def test_platform_setup_with_error(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_provider: MockProvider,
) -> None:
"""Test platform setup with an error during setup."""
class BadPlatform(MockTTS):
"""A mock TTS platform with a setup error."""
async def async_get_engine(
self,
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> Provider:
"""Raise exception during platform setup."""
raise Exception("Setup error") # pylint: disable=broad-exception-raised
mock_integration(hass, MockModule(domain="bad_tts"))
mock_platform(hass, "bad_tts.tts", BadPlatform(mock_provider))
await async_load_platform(
hass,
"tts",
"bad_tts",
{},
hass_config={"tts": [{"platform": "bad_tts"}]},
)
await hass.async_block_till_done()
assert "Error setting up platform: bad_tts" in caplog.text
async def test_service_without_cache_config(
hass: HomeAssistant, mock_tts_cache_dir, mock_tts
) -> None:
"""Set up a TTS platform without cache."""
calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
config = {DOMAIN: {"platform": "test", "cache": False}}
with assert_setup_component(1, DOMAIN):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
await hass.services.async_call(
DOMAIN,
"test_say",
{
ATTR_ENTITY_ID: "media_player.something",
ATTR_MESSAGE: "There is someone at the door.",
},
blocking=True,
)
assert len(calls) == 1
await hass.async_block_till_done()
assert not (
mock_tts_cache_dir / "42f18378fd4393d18c8dd11d03fa9563c1e54491_en-us_-_test.mp3"
).is_file()