home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

custom message with response

paulius-dev opened this issue · comments

I am trying to integrate home-assistant-js-websocket with jquery.
I use: https://cdn.jsdelivr.net/npm/home-assistant-js-websocket@8.0.1/dist/haws.umd.js compiled script.

authorization is successful, I receive all entities, I also send commands successfully.

Question. How can I send a custom message to HA and print a response?

I tried:

var service_data = {
entity_ids: ["sensor.temperature"],
minimal_response:true,
no_attributes: true,
significant_changes_only:true,
start_time:"2023-05-09T23:28:21.482Z",
type:"history/stream",
};
wsconn.sendMessagePromise(service_data).then(response => {
// Handle the response from Home Assistant
console.log("Received response:", response);
})
.catch(error => {
// Handle any errors that occurred
console.error("An error occurred:", error);
});

in Developer tools->Network tab (websocket) messages I see my request (type:"history/stream"....) and I get a response with historical data from HA successfully.

but in console i get
Received response: null

.then works good with:
HAWS.getUser(wsconn).then(response => {
// Handle the response from Home Assistant
console.log("Received response:", response);
})
.catch(error => {
// Handle any errors that occurred
console.error("An error occurred:", error);
});

Thanks for advice!