Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

home-assistant-js-websocket

Package Overview
Dependencies
Maintainers
2
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

home-assistant-js-websocket - npm Package Compare versions

Comparing version 6.0.1 to 6.1.0

2

dist/connection.d.ts

@@ -72,3 +72,3 @@ import { ERR_CONNECTION_LOST } from "./errors.js";

* @param callback the callback to be called when a new item arrives
* @param [options.resubscribe] re-established a subscription after a reconnect
* @param [options.resubscribe] re-established a subscription after a reconnect. Defaults to true.
* @returns promise that resolves to an unsubscribe function

@@ -75,0 +75,0 @@ */

@@ -279,3 +279,3 @@ /**

* @param callback the callback to be called when a new item arrives
* @param [options.resubscribe] re-established a subscription after a reconnect
* @param [options.resubscribe] re-established a subscription after a reconnect. Defaults to true.
* @returns promise that resolves to an unsubscribe function

@@ -282,0 +282,0 @@ */

import { HassEntities, UnsubscribeFunc } from "./types.js";
import { Connection } from "./connection.js";
export declare const entitiesColl: (conn: Connection) => import("./collection.js").Collection<HassEntities>;
export declare const entitiesColl: (conn: Connection) => import("./collection.js").Collection<{}>;
export declare const subscribeEntities: (conn: Connection, onChange: (state: HassEntities) => void) => UnsubscribeFunc;
import { getCollection } from "./collection.js";
import { getStates } from "./commands.js";
function processEvent(store, event) {
import { atLeastHaVersion } from "./util.js";
function processEvent(store, updates) {
const state = Object.assign({}, store.state);
if (updates.a) {
for (const entityId in updates.a) {
const newState = updates.a[entityId];
let last_changed = new Date(newState.lc * 1000).toISOString();
state[entityId] = {
entity_id: entityId,
state: newState.s,
attributes: newState.a,
context: typeof newState.c === "string"
? { id: newState.c, parent_id: null, user_id: null }
: newState.c,
last_changed: last_changed,
last_updated: newState.lu
? new Date(newState.lu * 1000).toISOString()
: last_changed,
};
}
}
if (updates.r) {
for (const entityId of updates.r) {
delete state[entityId];
}
}
if (updates.c) {
for (const entityId in updates.c) {
let entityState = state[entityId];
if (!entityState) {
console.warn("Received state update for unknown entity", entityId);
continue;
}
entityState = Object.assign({}, entityState);
const { "+": toAdd, "-": toRemove } = updates.c[entityId];
if (toAdd) {
if (toAdd.s) {
entityState.state = toAdd.s;
}
if (toAdd.c) {
if (typeof toAdd.c === "string") {
entityState.context = Object.assign(Object.assign({}, entityState.context), { id: toAdd.c });
}
else {
entityState.context = Object.assign(Object.assign({}, entityState.context), toAdd.c);
}
}
if (toAdd.lc) {
entityState.last_updated = entityState.last_changed = new Date(toAdd.lc * 1000).toISOString();
}
else if (toAdd.lu) {
entityState.last_updated = new Date(toAdd.lu * 1000).toISOString();
}
if (toAdd.a) {
entityState.attributes = Object.assign(Object.assign({}, entityState.attributes), toAdd.a);
}
}
if (toRemove) {
const attributes = Object.assign({}, entityState.attributes);
for (const key in toRemove.a) {
delete attributes[key];
}
entityState.attributes = attributes;
}
state[entityId] = entityState;
}
}
store.setState(state, true);
}
const subscribeUpdates = (conn, store) => conn.subscribeMessage((ev) => processEvent(store, ev), {
type: "subscribe_entities",
});
function legacyProcessEvent(store, event) {
const state = store.state;

@@ -17,3 +89,3 @@ if (state === undefined)

}
async function fetchEntities(conn) {
async function legacyFetchEntities(conn) {
const states = await getStates(conn);

@@ -27,4 +99,6 @@ const entities = {};

}
const subscribeUpdates = (conn, store) => conn.subscribeEvents((ev) => processEvent(store, ev), "state_changed");
export const entitiesColl = (conn) => getCollection(conn, "_ent", fetchEntities, subscribeUpdates);
const legacySubscribeUpdates = (conn, store) => conn.subscribeEvents((ev) => legacyProcessEvent(store, ev), "state_changed");
export const entitiesColl = (conn) => atLeastHaVersion(conn.haVersion, 2022, 4, 0)
? getCollection(conn, "_ent", () => Promise.resolve({}), subscribeUpdates)
: getCollection(conn, "_ent", legacyFetchEntities, legacySubscribeUpdates);
export const subscribeEntities = (conn, onChange) => entitiesColl(conn).subscribe(onChange);

@@ -426,3 +426,3 @@ (function (global, factory) {

* @param callback the callback to be called when a new item arrives
* @param [options.resubscribe] re-established a subscription after a reconnect
* @param [options.resubscribe] re-established a subscription after a reconnect. Defaults to true.
* @returns promise that resolves to an unsubscribe function

@@ -510,2 +510,14 @@ */

};
const atLeastHaVersion = (version, major, minor, patch) => {
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));
};

@@ -888,3 +900,74 @@ const genClientId = () => `${location.protocol}//${location.host}/`;

function processEvent(store, event) {
function processEvent(store, updates) {
const state = Object.assign({}, store.state);
if (updates.a) {
for (const entityId in updates.a) {
const newState = updates.a[entityId];
let last_changed = new Date(newState.lc * 1000).toISOString();
state[entityId] = {
entity_id: entityId,
state: newState.s,
attributes: newState.a,
context: typeof newState.c === "string"
? { id: newState.c, parent_id: null, user_id: null }
: newState.c,
last_changed: last_changed,
last_updated: newState.lu
? new Date(newState.lu * 1000).toISOString()
: last_changed,
};
}
}
if (updates.r) {
for (const entityId of updates.r) {
delete state[entityId];
}
}
if (updates.c) {
for (const entityId in updates.c) {
let entityState = state[entityId];
if (!entityState) {
console.warn("Received state update for unknown entity", entityId);
continue;
}
entityState = Object.assign({}, entityState);
const { "+": toAdd, "-": toRemove } = updates.c[entityId];
if (toAdd) {
if (toAdd.s) {
entityState.state = toAdd.s;
}
if (toAdd.c) {
if (typeof toAdd.c === "string") {
entityState.context = Object.assign(Object.assign({}, entityState.context), { id: toAdd.c });
}
else {
entityState.context = Object.assign(Object.assign({}, entityState.context), toAdd.c);
}
}
if (toAdd.lc) {
entityState.last_updated = entityState.last_changed = new Date(toAdd.lc * 1000).toISOString();
}
else if (toAdd.lu) {
entityState.last_updated = new Date(toAdd.lu * 1000).toISOString();
}
if (toAdd.a) {
entityState.attributes = Object.assign(Object.assign({}, entityState.attributes), toAdd.a);
}
}
if (toRemove) {
const attributes = Object.assign({}, entityState.attributes);
for (const key in toRemove.a) {
delete attributes[key];
}
entityState.attributes = attributes;
}
state[entityId] = entityState;
}
}
store.setState(state, true);
}
const subscribeUpdates = (conn, store) => conn.subscribeMessage((ev) => processEvent(store, ev), {
type: "subscribe_entities",
});
function legacyProcessEvent(store, event) {
const state = store.state;

@@ -903,3 +986,3 @@ if (state === undefined)

}
async function fetchEntities(conn) {
async function legacyFetchEntities(conn) {
const states = await getStates(conn);

@@ -913,4 +996,6 @@ const entities = {};

}
const subscribeUpdates = (conn, store) => conn.subscribeEvents((ev) => processEvent(store, ev), "state_changed");
const entitiesColl = (conn) => getCollection(conn, "_ent", fetchEntities, subscribeUpdates);
const legacySubscribeUpdates = (conn, store) => conn.subscribeEvents((ev) => legacyProcessEvent(store, ev), "state_changed");
const entitiesColl = (conn) => atLeastHaVersion(conn.haVersion, 2022, 4, 0)
? getCollection(conn, "_ent", () => Promise.resolve({}), subscribeUpdates)
: getCollection(conn, "_ent", legacyFetchEntities, legacySubscribeUpdates);
const subscribeEntities = (conn, onChange) => entitiesColl(conn).subscribe(onChange);

@@ -917,0 +1002,0 @@

@@ -8,9 +8,11 @@ export declare type Error = 1 | 2 | 3 | 4;

};
export declare type Context = {
id: string;
user_id: string | null;
parent_id: string | null;
};
export declare type HassEventBase = {
origin: string;
time_fired: string;
context: {
id: string;
user_id: string;
};
context: Context;
};

@@ -64,6 +66,3 @@ export declare type HassEvent = HassEventBase & {

attributes: HassEntityAttributeBase;
context: {
id: string;
user_id: string | null;
};
context: Context;
};

@@ -70,0 +69,0 @@ export declare type HassEntityAttributeBase = {

export declare function parseQuery<T>(queryString: string): T;
export declare const debounce: <T extends (...args: any[]) => unknown>(func: T, wait: number, immediate?: boolean) => T;
export declare const atLeastHaVersion: (version: string, major: number, minor: number, patch?: number | undefined) => boolean;

@@ -38,1 +38,13 @@ export function parseQuery(queryString) {

};
export const atLeastHaVersion = (version, major, minor, patch) => {
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));
};

@@ -5,3 +5,3 @@ {

"sideEffects": false,
"version": "6.0.1",
"version": "6.1.0",
"description": "Home Assistant websocket client",

@@ -8,0 +8,0 @@ "source": "lib/index.ts",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc