Convert alexa test fixtures to async (#142054)

This commit is contained in:
Erik Montnemery 2025-04-02 11:00:13 +02:00 committed by GitHub
parent 6b45b0f522
commit e02a6f2f19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 108 additions and 120 deletions

View File

@ -1,6 +1,5 @@
"""The tests for the Alexa component.""" """The tests for the Alexa component."""
from asyncio import AbstractEventLoop
import datetime import datetime
from http import HTTPStatus from http import HTTPStatus
@ -24,13 +23,11 @@ NPR_NEWS_MP3_URL = "https://pd.npr.org/anon.npr-mp3/npr/news/newscast.mp3"
@pytest.fixture @pytest.fixture
def alexa_client( async def alexa_client(
event_loop: AbstractEventLoop,
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
) -> TestClient: ) -> TestClient:
"""Initialize a Home Assistant server for testing this module.""" """Initialize a Home Assistant server for testing this module."""
loop = event_loop
@callback @callback
def mock_service(call): def mock_service(call):
@ -38,38 +35,36 @@ def alexa_client(
hass.services.async_register("test", "alexa", mock_service) hass.services.async_register("test", "alexa", mock_service)
assert loop.run_until_complete( assert await async_setup_component(
async_setup_component( hass,
hass, alexa.DOMAIN,
alexa.DOMAIN, {
{ # Key is here to verify we allow other keys in config too
# Key is here to verify we allow other keys in config too "homeassistant": {},
"homeassistant": {}, "alexa": {
"alexa": { "flash_briefings": {
"flash_briefings": { "password": "pass/abc",
"password": "pass/abc", "weather": [
"weather": [ {
{ "title": "Weekly forecast",
"title": "Weekly forecast", "text": "This week it will be sunny.",
"text": "This week it will be sunny.",
},
{
"title": "Current conditions",
"text": "Currently it is 80 degrees fahrenheit.",
},
],
"news_audio": {
"title": "NPR",
"audio": NPR_NEWS_MP3_URL,
"display_url": "https://npr.org",
"uid": "uuid",
}, },
} {
}, "title": "Current conditions",
"text": "Currently it is 80 degrees fahrenheit.",
},
],
"news_audio": {
"title": "NPR",
"audio": NPR_NEWS_MP3_URL,
"display_url": "https://npr.org",
"uid": "uuid",
},
}
}, },
) },
) )
return loop.run_until_complete(hass_client()) return await hass_client()
def _flash_briefing_req(client, briefing_id, password="pass%2Fabc"): def _flash_briefing_req(client, briefing_id, password="pass%2Fabc"):

View File

@ -1,6 +1,5 @@
"""The tests for the Alexa component.""" """The tests for the Alexa component."""
from asyncio import AbstractEventLoop
from http import HTTPStatus from http import HTTPStatus
import json import json
@ -30,13 +29,11 @@ NPR_NEWS_MP3_URL = "https://pd.npr.org/anon.npr-mp3/npr/news/newscast.mp3"
@pytest.fixture @pytest.fixture
def alexa_client( async def alexa_client(
event_loop: AbstractEventLoop,
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
) -> TestClient: ) -> TestClient:
"""Initialize a Home Assistant server for testing this module.""" """Initialize a Home Assistant server for testing this module."""
loop = event_loop
@callback @callback
def mock_service(call): def mock_service(call):
@ -44,96 +41,92 @@ def alexa_client(
hass.services.async_register("test", "alexa", mock_service) hass.services.async_register("test", "alexa", mock_service)
assert loop.run_until_complete( assert await async_setup_component(
async_setup_component( hass,
hass, alexa.DOMAIN,
alexa.DOMAIN, {
{ # Key is here to verify we allow other keys in config too
# Key is here to verify we allow other keys in config too "homeassistant": {},
"homeassistant": {}, "alexa": {},
"alexa": {}, },
},
)
) )
assert loop.run_until_complete( assert await async_setup_component(
async_setup_component( hass,
hass, "intent_script",
"intent_script", {
{ "intent_script": {
"intent_script": { "WhereAreWeIntent": {
"WhereAreWeIntent": { "speech": {
"speech": { "type": "plain",
"type": "plain", "text": """
"text": """ {%- if is_state("device_tracker.paulus", "home")
{%- if is_state("device_tracker.paulus", "home") and is_state("device_tracker.anne_therese",
and is_state("device_tracker.anne_therese", "home") -%}
"home") -%} You are both home, you silly
You are both home, you silly {%- else -%}
{%- else -%} Anne Therese is at {{
Anne Therese is at {{ states("device_tracker.anne_therese")
states("device_tracker.anne_therese") }} and Paulus is at {{
}} and Paulus is at {{ states("device_tracker.paulus")
states("device_tracker.paulus") }}
}} {% endif %}
{% endif %} """,
""", }
} },
"GetZodiacHoroscopeIntent": {
"speech": {
"type": "plain",
"text": "You told us your sign is {{ ZodiacSign }}.",
}
},
"GetZodiacHoroscopeIDIntent": {
"speech": {
"type": "plain",
"text": "You told us your sign is {{ ZodiacSign_Id }}.",
}
},
"AMAZON.PlaybackAction<object@MusicCreativeWork>": {
"speech": {
"type": "plain",
"text": "Playing {{ object_byArtist_name }}.",
}
},
"CallServiceIntent": {
"speech": {
"type": "plain",
"text": "Service called for {{ ZodiacSign }}",
}, },
"GetZodiacHoroscopeIntent": { "card": {
"speech": { "type": "simple",
"type": "plain", "title": "Card title for {{ ZodiacSign }}",
"text": "You told us your sign is {{ ZodiacSign }}.", "content": "Card content: {{ ZodiacSign }}",
}
}, },
"GetZodiacHoroscopeIDIntent": { "action": {
"speech": { "service": "test.alexa",
"type": "plain", "data_template": {"hello": "{{ ZodiacSign }}"},
"text": "You told us your sign is {{ ZodiacSign_Id }}.", "entity_id": "switch.test",
}
}, },
"AMAZON.PlaybackAction<object@MusicCreativeWork>": { },
"speech": { APPLICATION_ID: {
"type": "plain", "speech": {
"text": "Playing {{ object_byArtist_name }}.", "type": "plain",
} "text": "LaunchRequest has been received.",
}
},
APPLICATION_ID_SESSION_OPEN: {
"speech": {
"type": "plain",
"text": "LaunchRequest has been received.",
}, },
"CallServiceIntent": { "reprompt": {
"speech": { "type": "plain",
"type": "plain", "text": "LaunchRequest has been received.",
"text": "Service called for {{ ZodiacSign }}",
},
"card": {
"type": "simple",
"title": "Card title for {{ ZodiacSign }}",
"content": "Card content: {{ ZodiacSign }}",
},
"action": {
"service": "test.alexa",
"data_template": {"hello": "{{ ZodiacSign }}"},
"entity_id": "switch.test",
},
}, },
APPLICATION_ID: { },
"speech": { }
"type": "plain", },
"text": "LaunchRequest has been received.",
}
},
APPLICATION_ID_SESSION_OPEN: {
"speech": {
"type": "plain",
"text": "LaunchRequest has been received.",
},
"reprompt": {
"type": "plain",
"text": "LaunchRequest has been received.",
},
},
}
},
)
) )
return loop.run_until_complete(hass_client()) return await hass_client()
def _intent_req(client, data=None): def _intent_req(client, data=None):