Add test coverage for send_voice_assistant_event (#743)

This commit is contained in:
J. Nick Koston 2023-11-26 16:45:38 -06:00 committed by GitHub
parent 0fab7a6b99
commit 1a4c8bff17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 13 deletions

View File

@ -1303,20 +1303,15 @@ class APIClient:
def send_voice_assistant_event(
self, event_type: VoiceAssistantEventType, data: dict[str, str] | None
) -> None:
req = VoiceAssistantEventResponse()
req.event_type = event_type
data_args = []
req = VoiceAssistantEventResponse(event_type=event_type)
if data is not None:
for name, value in data.items():
arg = VoiceAssistantEventData()
arg.name = name
arg.value = value
data_args.append(arg)
# pylint: disable=no-member
req.data.extend(data_args)
# pylint: disable=no-member
req.data.extend(
[
VoiceAssistantEventData(name=name, value=value)
for name, value in data.items()
]
)
self._get_connection().send_message(req)
async def alarm_control_panel_command(

View File

@ -57,6 +57,8 @@ from aioesphomeapi.api_pb2 import (
SubscribeLogsResponse,
SwitchCommandRequest,
TextCommandRequest,
VoiceAssistantEventData,
VoiceAssistantEventResponse,
)
from aioesphomeapi.client import APIClient
from aioesphomeapi.connection import APIConnection
@ -94,6 +96,7 @@ from aioesphomeapi.model import (
UserServiceArg,
UserServiceArgType,
)
from aioesphomeapi.model import VoiceAssistantEventType as VoiceAssistantEventModelType
from aioesphomeapi.reconnect_logic import ReconnectLogic, ReconnectLogicState
from .common import (
@ -1666,3 +1669,33 @@ async def test_bluetooth_device_connect_cancelled(
)
# Make sure we do not leak message handlers
assert handlers_after == handlers_before
@pytest.mark.asyncio
async def test_send_voice_assistant_event(auth_client: APIClient) -> None:
send = patch_send(auth_client)
auth_client.send_voice_assistant_event(
VoiceAssistantEventModelType.VOICE_ASSISTANT_ERROR,
{"error": "error", "ok": "ok"},
)
send.assert_called_once_with(
VoiceAssistantEventResponse(
event_type=VoiceAssistantEventModelType.VOICE_ASSISTANT_ERROR.value,
data=[
VoiceAssistantEventData(name="error", value="error"),
VoiceAssistantEventData(name="ok", value="ok"),
],
)
)
send.reset_mock()
auth_client.send_voice_assistant_event(
VoiceAssistantEventModelType.VOICE_ASSISTANT_ERROR, None
)
send.assert_called_once_with(
VoiceAssistantEventResponse(
event_type=VoiceAssistantEventModelType.VOICE_ASSISTANT_ERROR.value,
data=[],
)
)