fix home assistant lights

This commit is contained in:
legop3
2025-11-21 01:45:29 -05:00
parent e2a3d78bc0
commit 6473b076d5
4 changed files with 34 additions and 9 deletions
+7
View File
@@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"bcrypt": "^6.0.0", "bcrypt": "^6.0.0",
"express": "^4.19.2", "express": "^4.19.2",
"home-assistant-js-websocket": "^3.1.2",
"js-yaml": "^4.1.1", "js-yaml": "^4.1.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"socket.io": "^4.7.5", "socket.io": "^4.7.5",
@@ -695,6 +696,12 @@
"node": ">= 0.4" "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": { "node_modules/http-errors": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz",
+1 -1
View File
@@ -10,7 +10,7 @@
"dependencies": { "dependencies": {
"bcrypt": "^6.0.0", "bcrypt": "^6.0.0",
"express": "^4.19.2", "express": "^4.19.2",
"home-assistant-js-websocket": "3.1.2", "home-assistant-js-websocket": "^3.1.2",
"js-yaml": "^4.1.1", "js-yaml": "^4.1.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"socket.io": "^4.7.5", "socket.io": "^4.7.5",
+4
View File
@@ -5,4 +5,8 @@ const io = new SocketIOServer(httpServer, {
cors: { origin: '*' }, cors: { origin: '*' },
}); });
// Allow more service listeners without warnings.
io.sockets.setMaxListeners(30);
io.of('/').setMaxListeners(30);
module.exports = io; module.exports = io;
+22 -8
View File
@@ -1,11 +1,6 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const WebSocket = require('ws'); const WebSocket = require('ws');
const { const { createConnection, subscribeEntities, callService, Auth } = require('home-assistant-js-websocket');
createConnection,
createLongLivedTokenAuth,
subscribeEntities,
callService,
} = require('home-assistant-js-websocket');
const io = require('../globals/io'); const io = require('../globals/io');
const logger = require('../globals/logger').child('homeAssistantService'); const logger = require('../globals/logger').child('homeAssistantService');
const { loadConfig } = require('../helpers/configLoader'); const { loadConfig } = require('../helpers/configLoader');
@@ -29,6 +24,25 @@ let connected = false;
const enabled = Boolean(haConfig?.url && haConfig?.token); 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) { function inferType(entityId, explicitType) {
if (explicitType === 'light' || explicitType === 'switch') { if (explicitType === 'light' || explicitType === 'switch') {
return explicitType; return explicitType;
@@ -161,8 +175,8 @@ async function connect() {
return; return;
} }
try { try {
const auth = await createLongLivedTokenAuth(haConfig.url, haConfig.token); const auth = buildAuth();
connection = await createConnection({ auth }); connection = await createConnection({ auth, setupRetry: 0 });
connected = true; connected = true;
emitStatus(); emitStatus();
logger.info('Connected to Home Assistant'); logger.info('Connected to Home Assistant');