Home Assistant Unofficial Reference 2024.12.1
connection_test.py
Go to the documentation of this file.
1 """Assist satellite connection test."""
2 
3 import logging
4 from pathlib import Path
5 
6 from aiohttp import web
7 
8 from homeassistant.components.http import KEY_HASS, HomeAssistantView
9 
10 from .const import CONNECTION_TEST_DATA
11 
12 _LOGGER = logging.getLogger(__name__)
13 
14 CONNECTION_TEST_CONTENT_TYPE = "audio/mpeg"
15 CONNECTION_TEST_FILENAME = "connection_test.mp3"
16 CONNECTION_TEST_URL_BASE = "/api/assist_satellite/connection_test"
17 
18 
19 class ConnectionTestView(HomeAssistantView):
20  """View to serve an audio sample for connection test."""
21 
22  requires_auth = False
23  url = f"{CONNECTION_TEST_URL_BASE}/{{connection_id}}"
24  name = "api:assist_satellite_connection_test"
25 
26  async def get(self, request: web.Request, connection_id: str) -> web.Response:
27  """Start a get request."""
28  _LOGGER.debug("Request for connection test with id %s", connection_id)
29 
30  hass = request.app[KEY_HASS]
31  connection_test_data = hass.data[CONNECTION_TEST_DATA]
32 
33  connection_test_event = connection_test_data.pop(connection_id, None)
34 
35  if connection_test_event is None:
36  return web.Response(status=404)
37 
38  connection_test_event.set()
39 
40  audio_path = Path(__file__).parent / CONNECTION_TEST_FILENAME
41  audio_data = await hass.async_add_executor_job(audio_path.read_bytes)
42 
43  return web.Response(body=audio_data, content_type=CONNECTION_TEST_CONTENT_TYPE)
web.Response get(self, web.Request request, str connection_id)