vicfergar / HassClient

A Home Assistant client using Web Sockect API
GNU General Public License v3.0
18 stars 10 forks source link

WebSocket and Assist Pipeline events #10

Open ReikanYsora opened 1 year ago

ReikanYsora commented 1 year ago

Hello, !

First of all, congratulations on the work done on this DLL. I've been using it for a while for a personal project and it works incredibly well. It covers all my needs, except for one detail.

I'm trying to use Home Assistant's Assist Pipeline and I'm encountering just one difficulty for which I'd like your opinion.

With this code, I'm able to communicate a pipeline execution request and it works correctly, except that I can't catch the pipeline assist events specified in this documentation Assist Pipeline Dev Documentation

RawCommandResult result = await hassWSApi.SendRawCommandWithResultAsync(new RawCommandMessage("assist_pipeline/run", new { start_stage = "intent", end_stage = "tts", input = new { text = "Turn on kitchen light's" } }));

I'm trying to catch the assist pipeline events with this code :

await hassWSApi.AddEventHandlerSubscriptionAsync(TestEventHandler_Event, "run-start");

or

await hassWSApi.AddEventHandlerSubscriptionAsync(TestEventHandler_Event, "tts-end");

Unfortunately, I can't catch anything, so I can't parse and retrieve the feedback from the assist pipeline, which should give me, in this example, the link to play the TextToSpeech.

In the debug console of Assist Pipeline directly in Home Assistant, my request is mark as done and the action is properly handled (in this case, i speak french to Home Assistant, because i'm french :p)

image

Can you tell me what I'm doing wrong and what method I should use to catch these events?

Thanks you so much

Reikan

vicfergar commented 1 year ago

Hi @ReikanYsora,

Glad to hear that you are finding this library so useful! 😄

I took a look at your issue, seems that the sending message has the right format. But this pipeline implementation seems to be slightly different from existing ones.

The WS server will respond with an event using the id from the run command. I'm sharing an example here:

image

I will try to take a look for a simple way of handling these result events.

vicfergar commented 1 year ago

I created a branch named feature/run_pipeline that you can use to test this feature. It's still WIP because STT seems to be slily more difficult to implement as it needs to send binary data over the socket.

This can be used as follows: IEnumerable<PipelineEventResultInfo> result = await hassWSApi.RunIntentPipeline(StageTypes.TTS, "Turn on kitchen light's");

ReikanYsora commented 1 year ago

Thank you very much for the speed of your reply.

I've just tested it, and "intent" and "tts" work perfectly! However, I had to add this line to manually define the pipeline id in my request : RawCommandResult pipelineInfos = await hassWSApi.SendRawCommandWithResultAsync(new RawCommandMessage("assist_pipeline/pipeline/list"));

By default, if the value of the "pipeline" field is null, the operation fails and asks for it to be defined.

image

By specifying the pipeline ID returned by the '"assist_pipeline/pipeline/list"' command, everything works correctly :) Maybe I should add a GetPipelines() method to get this information too ?

I understand that SST is more complex, and I'm very interested in this integration.

Anyway, thanks again for everything!

ReikanYsora commented 1 year ago

If you want, i have a commit on my 'feature/run_pipeline' forkked branch with this methods and models added

vicfergar commented 1 year ago

Good catch @ReikanYsora, I added a fix for that issue.

Feel free to open a PR with the changes from your commit :)

ReikanYsora commented 1 year ago

Done. :)