|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Voice assistant entity integration example. Bare minimum of an integration driver.""" |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from asyncio import sleep |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import ucapi |
| 9 | +from ucapi import AssistantEvent, AssistantEventType, VoiceAssistant |
| 10 | +from ucapi.api_definitions import AssistantTextResponse, AssistantSttResponse |
| 11 | +from ucapi.voice_assistant import ( |
| 12 | + Commands as VACommands, |
| 13 | + Features as VAFeatures, |
| 14 | + Attributes as VAAttr, |
| 15 | + VoiceAssistantEntityOptions, |
| 16 | + AudioConfiguration, |
| 17 | + SampleFormat, |
| 18 | +) |
| 19 | + |
| 20 | +loop = asyncio.new_event_loop() |
| 21 | +api = ucapi.IntegrationAPI(loop) |
| 22 | + |
| 23 | +session_id = 0 |
| 24 | + |
| 25 | + |
| 26 | +@api.listens_to(ucapi.Events.CONNECT) |
| 27 | +async def on_connect() -> None: |
| 28 | + # When the remote connects, we just set the device state. We are ready all the time! |
| 29 | + await api.set_device_state(ucapi.DeviceStates.CONNECTED) |
| 30 | + |
| 31 | + |
| 32 | +@api.listens_to(ucapi.Events.SUBSCRIBE_ENTITIES) |
| 33 | +async def on_subscribe_entities(entity_ids: list[str]) -> None: |
| 34 | + for entity_id in entity_ids: |
| 35 | + api.configured_entities.update_attributes(entity_id, {VAAttr.STATE: "ON"}) |
| 36 | + |
| 37 | + |
| 38 | +async def on_voice_cmd( |
| 39 | + entity: ucapi.VoiceAssistant, cmd_id: str, params: dict[str, Any] | None |
| 40 | +) -> ucapi.StatusCodes: |
| 41 | + """ |
| 42 | + Voice assistant command handler. |
| 43 | +
|
| 44 | + Called by the integration-API if a command is sent to a configured voice_assistant-entity. |
| 45 | +
|
| 46 | + :param entity: voice assistant entity |
| 47 | + :param cmd_id: command |
| 48 | + :param params: optional command parameters |
| 49 | + :return: status of the command |
| 50 | + """ |
| 51 | + # HACK until core is fixed |
| 52 | + global session_id |
| 53 | + |
| 54 | + print(f"Got {entity.id} command request: {cmd_id}") |
| 55 | + if params is None: |
| 56 | + return ucapi.StatusCodes.BAD_REQUEST |
| 57 | + |
| 58 | + session_id = params.get("session_id", 0) |
| 59 | + if session_id <= 0: |
| 60 | + return ucapi.StatusCodes.BAD_REQUEST |
| 61 | + |
| 62 | + if cmd_id == VACommands.VOICE_START: |
| 63 | + ready_evt = AssistantEvent( |
| 64 | + type=AssistantEventType.READY, |
| 65 | + entity_id=entity.id, |
| 66 | + session_id=session_id, |
| 67 | + ) |
| 68 | + await api.broadcast_assistant_event(ready_evt) |
| 69 | + |
| 70 | + # Acknowledge start; binary audio will arrive on the WS binary channel |
| 71 | + return ucapi.StatusCodes.OK |
| 72 | + return ucapi.StatusCodes.NOT_IMPLEMENTED |
| 73 | + |
| 74 | + |
| 75 | +async def on_voice_session(session): |
| 76 | + print( |
| 77 | + f"Voice stream started: session={session.session_id}, " |
| 78 | + f"{session.config.channels}ch @ {session.config.sample_rate} Hz" |
| 79 | + ) |
| 80 | + # HACK until core is fixed |
| 81 | + global session_id |
| 82 | + |
| 83 | + total = 0 |
| 84 | + async for frame in session: # frame is bytes |
| 85 | + total += len(frame) |
| 86 | + # feed frame into your voice assistant / LLM here |
| 87 | + print(f"Got {len(frame)} bytes of audio data") |
| 88 | + print(f"Voice stream ended: session={session.session_id}, bytes={total}") |
| 89 | + |
| 90 | + event = AssistantEvent( |
| 91 | + type=AssistantEventType.STT_RESPONSE, |
| 92 | + entity_id="va_main", |
| 93 | + session_id=session_id, |
| 94 | + data=AssistantSttResponse( |
| 95 | + text="I'm just a demo and I don't know what you said." |
| 96 | + ), |
| 97 | + ) |
| 98 | + await api.broadcast_assistant_event(event) |
| 99 | + |
| 100 | + await sleep(1) |
| 101 | + event = AssistantEvent( |
| 102 | + type=AssistantEventType.TEXT_RESPONSE, |
| 103 | + entity_id="va_main", |
| 104 | + session_id=session_id, |
| 105 | + data=AssistantTextResponse( |
| 106 | + success=True, text=f"You have sent {total} bytes of audio data" |
| 107 | + ), |
| 108 | + ) |
| 109 | + await api.broadcast_assistant_event(event) |
| 110 | + |
| 111 | + await sleep(1) |
| 112 | + event = AssistantEvent( |
| 113 | + type=AssistantEventType.FINISHED, |
| 114 | + entity_id="va_main", |
| 115 | + session_id=session_id, |
| 116 | + ) |
| 117 | + await api.broadcast_assistant_event(event) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + logging.basicConfig() |
| 122 | + |
| 123 | + entity = VoiceAssistant( |
| 124 | + identifier="va_main", |
| 125 | + name={"en": "Demo Voice Assistant"}, |
| 126 | + features=[VAFeatures.TRANSCRIPTION, VAFeatures.RESPONSE_TEXT], |
| 127 | + attributes={VAAttr.STATE.value: "ON"}, |
| 128 | + options=VoiceAssistantEntityOptions( |
| 129 | + audio_cfg=AudioConfiguration( |
| 130 | + channels=1, sample_rate=16000, sample_format=SampleFormat.I16 |
| 131 | + ), |
| 132 | + ), |
| 133 | + cmd_handler=on_voice_cmd, |
| 134 | + ) |
| 135 | + |
| 136 | + api.available_entities.add(entity) |
| 137 | + api.set_voice_stream_handler(on_voice_session) |
| 138 | + |
| 139 | + loop.run_until_complete(api.init("voice.json")) |
| 140 | + loop.run_forever() |
0 commit comments