Update packages

This commit is contained in:
GermanBluefox
2026-03-31 17:00:15 +02:00
parent 7bf04e494a
commit 92140fe771
17 changed files with 13927 additions and 8128 deletions
+68 -56
View File
@@ -1,6 +1,6 @@
const util = require('util');
const EventEmitter = require('events').EventEmitter;
const WebSocket = require('ws');
const util = require('node:util');
const { EventEmitter } = require('node:events');
const WebSocket = require('node:ws');
function HASS(options, log) {
if (!(this instanceof HASS)) {
@@ -14,7 +14,7 @@ function HASS(options, log) {
const ERRORS = {
1: 'ERR_CANNOT_CONNECT',
2: 'ERR_INVALID_AUTH',
3: 'ERR_CONNECTION_LOST'
3: 'ERR_CONNECTION_LOST',
};
this.socket = null;
@@ -28,13 +28,15 @@ function HASS(options, log) {
function subscribeEvents(socket, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'subscribe_events', ts: Date.now(), cb: callback};
requests[id] = { type: 'subscribe_events', ts: Date.now(), cb: callback };
socket.send(JSON.stringify({
id: id,
type: 'subscribe_events'
/*event_type: 'state_changed'*/
}));
socket.send(
JSON.stringify({
id: id,
type: 'subscribe_events',
/*event_type: 'state_changed'*/
}),
);
} else {
callback && callback('not connected');
}
@@ -43,11 +45,13 @@ function HASS(options, log) {
function getConfig(socket, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'get_config', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
type: 'get_config'
}));
requests[id] = { type: 'get_config', cb: callback, ts: Date.now() };
socket.send(
JSON.stringify({
id: id,
type: 'get_config',
}),
);
} else {
callback && callback('not connected');
}
@@ -56,11 +60,13 @@ function HASS(options, log) {
function getStates(socket, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'get_states', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
type: 'get_states'
}));
requests[id] = { type: 'get_states', cb: callback, ts: Date.now() };
socket.send(
JSON.stringify({
id: id,
type: 'get_states',
}),
);
} else {
callback && callback('not connected');
}
@@ -69,11 +75,13 @@ function HASS(options, log) {
function getPanels(socket, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'get_panels', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
type: 'get_panels'
}));
requests[id] = { type: 'get_panels', cb: callback, ts: Date.now() };
socket.send(
JSON.stringify({
id: id,
type: 'get_panels',
}),
);
} else {
callback && callback('not connected');
}
@@ -82,11 +90,13 @@ function HASS(options, log) {
function getServices(socket, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'get_services', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
type: 'get_services'
}));
requests[id] = { type: 'get_services', cb: callback, ts: Date.now() };
socket.send(
JSON.stringify({
id: id,
type: 'get_services',
}),
);
} else {
callback && callback('not connected');
}
@@ -95,15 +105,17 @@ function HASS(options, log) {
function callService(socket, service, domain, serviceData, target, callback) {
if (socket && typeof socket.send === 'function') {
const id = currentId++;
requests[id] = {type: 'call_service', cb: callback, ts: Date.now()};
socket.send(JSON.stringify({
id: id,
type: 'call_service',
domain: domain || '',
service: service,
service_data: serviceData,
target
}));
requests[id] = { type: 'call_service', cb: callback, ts: Date.now() };
socket.send(
JSON.stringify({
id: id,
type: 'call_service',
domain: domain || '',
service: service,
service_data: serviceData,
target,
}),
);
} else {
callback && callback('not connected');
}
@@ -111,10 +123,12 @@ function HASS(options, log) {
function sendAuth(socket, pass) {
if (socket && typeof socket.send === 'function') {
socket.send(JSON.stringify({
type: 'auth',
access_token: pass
}));
socket.send(
JSON.stringify({
type: 'auth',
access_token: pass,
}),
);
}
}
@@ -127,37 +141,37 @@ function HASS(options, log) {
if (response.event.data && response.event.event_type === 'system_log_event') {
if (response.event.data.level === 'WARNING') {
log.warn('EVENT: ' + response.event.data.message);
} else
if (response.event.data.level === 'ERROR') {
} else if (response.event.data.level === 'ERROR') {
log.error('EVENT: ' + response.event.data.message);
} else {
} else {
log.debug('EVENT: ' + response.event.data.message);
}
} else if (response.event && response.event.event_type === 'state_changed') {
that.emit('state_changed', response.event.data.new_state);
}
} else
if (response.type === 'auth_required') {
} else if (response.type === 'auth_required') {
if (!options.password) {
that.emit('error', 'Password required. Connection closed');
socket.terminate();
} else {
setTimeout(() => sendAuth(socket, options.password), 50);
}
} else
if (response.type === 'auth_ok') {
} else if (response.type === 'auth_ok') {
setImmediate(() =>
subscribeEvents(socket, err => {
if (!err) {
connected = true;
that.emit('connected');
}
}));
}),
);
} else if (response.id === undefined) {
log.error(`Invalid answer: ${msg}`);
} else {
if (response.type === 'result' && requests[response.id]) {
log.debug(`got answer for ${requests[response.id].type} success = ${response.success}, result = ${JSON.stringify(response.result)}`);
log.debug(
`got answer for ${requests[response.id].type} success = ${response.success}, result = ${JSON.stringify(response.result)}`,
);
if (typeof requests[response.id].cb === 'function') {
requests[response.id].cb(!response.success, response.result);
delete requests[response.id];
@@ -176,7 +190,6 @@ function HASS(options, log) {
});
socket.on('open', () => {
if (!connected) {
}
});
socket.on('close', () => {
@@ -184,7 +197,6 @@ function HASS(options, log) {
if (connected) {
connected = false;
that.emit('disconnected');
}
if (!connectTimeout && !closed) {
setTimeout(() => {
@@ -244,7 +256,7 @@ function HASS(options, log) {
}
this.socket = new WebSocket(`ws${options.secure ? 's' : ''}://${options.host}:${options.port}/api/websocket`, {
perMessageDeflate: false
perMessageDeflate: false,
});
initSocket(this.socket);
@@ -259,7 +271,7 @@ function HASS(options, log) {
if (this.socket) {
this.socket.close();
}
}
};
return this;
}