-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
130 lines (99 loc) · 3.4 KB
/
main.js
File metadata and controls
130 lines (99 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { app, Tray, Menu, BrowserWindow, ipcMain } = require('electron');
const { generateSensorIcon } = require('./iconGenerator');
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const sensorIcons = {
'sensor.living_room_temperature': path.join(__dirname, 'assets/temp.png'),
//'sensor.humidity': path.join(__dirname, 'assets/humidity.png'),
// Add more mappings as needed
};
let tray, configWindow;
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
let currentSensorIndex = 0;
function saveConfig() {
fs.writeFileSync('config.json', JSON.stringify(config, null, 2));
}
async function fetchSensors() {
try {
const res = await axios.get(`${config.haUrl}/api/states`, {
headers: { Authorization: `Bearer ${config.token}` },
});
return res.data
.filter(s => config.sensors.includes(s.entity_id))
.map(s => {
const name = s.attributes.friendly_name || s.entity_id;
const value = `${s.state} ${s.attributes.unit_of_measurement || ''}`;
return {
label: `${name}: ${value}`,
//icon: sensorIcons[s.entity_id] || undefined,
enabled: false
};
});
} catch {
return ['Error fetching data'];
}
}
function createTray() {
tray = new Tray('assets/ha-taskbar.png');
tray.setToolTip('Home Assistant Tray');
updateTray();
tray.setContextMenu(Menu.buildFromTemplate([
{ label: 'Configure', click: createConfigWindow },
{ type: 'separator' },
{ label: 'Quit', role: 'quit' }
]));
}
async function updateTray() {
const sensorData = await fetchSensors();
if (sensorData.length === 0) {
tray.setImage(generateSensorIcon({ text: 'ERR', color: '#FF0000', bg: '#000000' }));
return;
}
const { label, icon } = sensorData[currentSensorIndex];
const parts = label.split(': ');
const displayValue = parts[1] || parts[0];
// Optional: color-code by sensor
const colorMap = ['#007bff', '#28a745', '#ffc107', '#dc3545'];
const bgColor = '#ffffff';
const textColor = colorMap[currentSensorIndex % colorMap.length];
tray.setImage(generateSensorIcon({ text: displayValue, color: textColor, bg: bgColor }));
tray.setContextMenu(Menu.buildFromTemplate([
...sensorData,
{ type: 'separator' },
{ label: 'Configure', click: createConfigWindow },
{ label: 'Quit', role: 'quit' }
]));
tray.setToolTip(sensorData.map(s => s.label).join('\n'));
// Next sensor in rotation
currentSensorIndex = (currentSensorIndex + 1) % sensorData.length;
// Rotate every 10 seconds
setTimeout(updateTray, 10000);
}
function createConfigWindow() {
if (configWindow) return;
configWindow = new BrowserWindow({
width: 500,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
configWindow.loadFile('renderer.html');
configWindow.on('closed', () => { configWindow = null; });
}
ipcMain.handle('get-config', () => config);
ipcMain.handle('get-all-sensors', async () => {
const res = await axios.get(`${config.haUrl}/api/states`, {
headers: { Authorization: `Bearer ${config.token}` },
});
return res.data
.filter(s => s.entity_id.startsWith('sensor.'))
.map(s => ({ id: s.entity_id, name: s.attributes.friendly_name || s.entity_id }));
});
ipcMain.on('save-config', (event, newConfig) => {
config = newConfig;
saveConfig();
updateTray();
});
app.whenReady().then(createTray);