Skip to content

Commit

Permalink
feat: Enable message coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Dec 25, 2022
1 parent c405f07 commit d235c35
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/homeAssistant/createSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ export default function createSocket({
socket.off('close', onClose);
socket.off('error', onClose);
socket.haVersion = message.ha_version;
// enable coalesce messages if supported
if (atLeastHaVersion(socket.haVersion, 2022, 9)) {
socket.send(
JSON.stringify({
type: 'supported_features',
id: 1,
features: { coalesce_messages: 1 },
})
);
}
promResolve(socket);
break;

Expand Down Expand Up @@ -118,3 +128,25 @@ export default function createSocket({
);
});
}

// https://github.com/home-assistant/home-assistant-js-websocket/blob/95f166b29a09fc1841bd0c1f312391ceb2812520/lib/util.ts#L45
function atLeastHaVersion(
version: string,
major: number,
minor: number,
patch?: number
): boolean {
const [haMajor, haMinor, haPatch] = version.split('.', 3);

return (
Number(haMajor) > major ||
(Number(haMajor) === major &&
(patch === undefined
? Number(haMinor) >= minor
: Number(haMinor) > minor)) ||
(patch !== undefined &&
Number(haMajor) === major &&
Number(haMinor) === minor &&
Number(haPatch) >= patch)
);
}

0 comments on commit d235c35

Please sign in to comment.