mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-15 17:12:59 -04:00
fix home assistant lights
This commit is contained in:
Generated
+7
@@ -10,6 +10,7 @@
|
||||
"dependencies": {
|
||||
"bcrypt": "^6.0.0",
|
||||
"express": "^4.19.2",
|
||||
"home-assistant-js-websocket": "^3.1.2",
|
||||
"js-yaml": "^4.1.1",
|
||||
"morgan": "^1.10.0",
|
||||
"socket.io": "^4.7.5",
|
||||
@@ -695,6 +696,12 @@
|
||||
"node": ">= 0.4"
|
||||
}
|
||||
},
|
||||
"node_modules/home-assistant-js-websocket": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/home-assistant-js-websocket/-/home-assistant-js-websocket-3.1.2.tgz",
|
||||
"integrity": "sha512-HzaJtuhufBkppfXerla5cJHSP8uXzLoSRV+XPCcYlOttiKzqLQHD3sPm2T9UHTHPPppy3jRZtaA86ISfH3GK/g==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/http-errors": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz",
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
"dependencies": {
|
||||
"bcrypt": "^6.0.0",
|
||||
"express": "^4.19.2",
|
||||
"home-assistant-js-websocket": "3.1.2",
|
||||
"home-assistant-js-websocket": "^3.1.2",
|
||||
"js-yaml": "^4.1.1",
|
||||
"morgan": "^1.10.0",
|
||||
"socket.io": "^4.7.5",
|
||||
|
||||
@@ -5,4 +5,8 @@ const io = new SocketIOServer(httpServer, {
|
||||
cors: { origin: '*' },
|
||||
});
|
||||
|
||||
// Allow more service listeners without warnings.
|
||||
io.sockets.setMaxListeners(30);
|
||||
io.of('/').setMaxListeners(30);
|
||||
|
||||
module.exports = io;
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
const EventEmitter = require('events');
|
||||
const WebSocket = require('ws');
|
||||
const {
|
||||
createConnection,
|
||||
createLongLivedTokenAuth,
|
||||
subscribeEntities,
|
||||
callService,
|
||||
} = require('home-assistant-js-websocket');
|
||||
const { createConnection, subscribeEntities, callService, Auth } = require('home-assistant-js-websocket');
|
||||
const io = require('../globals/io');
|
||||
const logger = require('../globals/logger').child('homeAssistantService');
|
||||
const { loadConfig } = require('../helpers/configLoader');
|
||||
@@ -29,6 +24,25 @@ let connected = false;
|
||||
|
||||
const enabled = Boolean(haConfig?.url && haConfig?.token);
|
||||
|
||||
function buildAuth() {
|
||||
const token = haConfig?.token?.trim();
|
||||
const url = haConfig?.url?.trim();
|
||||
if (!token || !url) {
|
||||
throw new Error('Home Assistant url/token missing');
|
||||
}
|
||||
// Long-lived tokens last 10 years; set a far future expiry to avoid refresh attempts.
|
||||
return new Auth(
|
||||
{
|
||||
hassUrl: url.replace(/\/+$/, ''),
|
||||
access_token: token,
|
||||
expires: Date.now() + 1000 * 60 * 60 * 24 * 365 * 10,
|
||||
refresh_token: null,
|
||||
clientId: 'multiroomba-rover',
|
||||
},
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
function inferType(entityId, explicitType) {
|
||||
if (explicitType === 'light' || explicitType === 'switch') {
|
||||
return explicitType;
|
||||
@@ -161,8 +175,8 @@ async function connect() {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const auth = await createLongLivedTokenAuth(haConfig.url, haConfig.token);
|
||||
connection = await createConnection({ auth });
|
||||
const auth = buildAuth();
|
||||
connection = await createConnection({ auth, setupRetry: 0 });
|
||||
connected = true;
|
||||
emitStatus();
|
||||
logger.info('Connected to Home Assistant');
|
||||
|
||||
Reference in New Issue
Block a user