feat: méthode WebSocket HA pour Lovelace + vue lumières créée
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Node 22 a WebSocket natif
|
||||
const HA_WS = process.env.HA_URL.replace('http://', 'ws://').replace('https://', 'wss://') + '/api/websocket';
|
||||
const TOKEN = process.env.HA_TOKEN;
|
||||
|
||||
let msgId = 1;
|
||||
const pending = {};
|
||||
const ws = new WebSocket(HA_WS);
|
||||
|
||||
function send(msg) {
|
||||
return new Promise((resolve) => {
|
||||
const id = msgId++;
|
||||
msg.id = id;
|
||||
pending[id] = resolve;
|
||||
ws.send(JSON.stringify(msg));
|
||||
});
|
||||
}
|
||||
|
||||
ws.addEventListener('message', ({ data }) => {
|
||||
const msg = JSON.parse(data);
|
||||
if (msg.type === 'auth_required') {
|
||||
ws.send(JSON.stringify({ type: 'auth', access_token: TOKEN }));
|
||||
} else if (msg.type === 'auth_ok') {
|
||||
console.log('✅ Auth OK, HA:', msg.ha_version);
|
||||
main().catch(console.error);
|
||||
} else if (msg.id && pending[msg.id]) {
|
||||
pending[msg.id](msg);
|
||||
delete pending[msg.id];
|
||||
}
|
||||
});
|
||||
|
||||
async function main() {
|
||||
// Lister les dashboards
|
||||
const dashboards = await send({ type: 'lovelace/dashboards/list' });
|
||||
console.log('\n📋 Dashboards:', JSON.stringify(dashboards.result || dashboards.error, null, 2));
|
||||
|
||||
// Config dashboard par défaut
|
||||
const config = await send({ type: 'lovelace/config' });
|
||||
if (config.result) {
|
||||
console.log('\n📄 Mode: storage');
|
||||
console.log('Views:', config.result.views?.map(v => v.title || v.path));
|
||||
} else {
|
||||
console.log('\n📄 Config défaut:', JSON.stringify(config, null, 2).substring(0, 600));
|
||||
}
|
||||
|
||||
ws.close();
|
||||
}
|
||||
|
||||
ws.addEventListener('error', (e) => { console.error('WS Error:', e.message); process.exit(1); });
|
||||
setTimeout(() => { console.error('Timeout'); process.exit(1); }, 10000);
|
||||
Reference in New Issue
Block a user