UNPKG

homebridge-solar-monitor-ivk

Version:

Solar production monitor for Homebridge with Pushover notifications

144 lines (119 loc) 4.3 kB
#!/usr/bin/env node /** * Test script for Homebridge Solar Monitor Plugin * Tests Pushover notifications and data parsing */ const https = require('https'); const querystring = require('querystring'); // Test configuration const config = { pushoverUserKey: 'ubacf6yrzqi39wv5758ogeorksmyg2', pushoverAppToken: 'apqypemqperophpagxtspat3tn7sfr', secondUserKey: 'us5sg9aqb2i7iqbjmmocr7p2df1ymp' }; // Test data const testData = { current_power: '10 kW', today_energy: '2.90 kWh', yesterday_energy: '27.92 kWh', monthly_energy: '0.00 kWh', timestamp: new Date().toISOString() }; // Send Pushover notification async function sendPushoverNotification(userKey, appToken, message) { const postData = querystring.stringify({ token: appToken, user: userKey, message: message, title: '⚡ Solar Production Update', sound: 'cosmic', priority: 0 }); const options = { hostname: 'api.pushover.net', port: 443, path: '/1/messages.json', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData) } }; return new Promise((resolve, reject) => { const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { if (res.statusCode === 200) { console.log(`✅ Pushover notification sent to user ${userKey}`); resolve(); } else { console.log(`❌ Pushover API error: ${res.statusCode} - ${data}`); reject(new Error(`Pushover API error: ${res.statusCode} - ${data}`)); } }); }); req.on('error', (error) => { console.log(`❌ Request error: ${error.message}`); reject(error); }); req.write(postData); req.end(); }); } // Format message function formatMessage(data) { return `🔋 Current Power: ${data.current_power} 📊 E Today: ${data.today_energy} 📈 E Yesterday: ${data.yesterday_energy} 📅 E This Month: ${data.monthly_energy}${new Date().toLocaleString()}`; } // Test solar hours function function isSolarHours() { const now = new Date(); const istTime = new Date(now.getTime() + (5.5 * 60 * 60 * 1000)); // IST is UTC+5:30 const hour = istTime.getHours(); const minute = istTime.getMinutes(); const currentTime = hour + minute / 60; // Solar hours: 6:00 AM to 6:30 PM IST return currentTime >= 6 && currentTime <= 18.5; } // Main test function async function runTests() { console.log('=== Homebridge Solar Monitor Plugin Test ===\n'); // Test 1: Solar hours check console.log('1. Testing solar hours check...'); const inSolarHours = isSolarHours(); console.log(` Solar hours active: ${inSolarHours}`); console.log(` Current IST time: ${new Date(new Date().getTime() + (5.5 * 60 * 60 * 1000)).toLocaleString()}\n`); // Test 2: Message formatting console.log('2. Testing message formatting...'); const message = formatMessage(testData); console.log(' Formatted message:'); console.log(message + '\n'); // Test 3: Pushover notifications console.log('3. Testing Pushover notifications...'); try { // Send to primary user await sendPushoverNotification(config.pushoverUserKey, config.pushoverAppToken, message); // Send to second user await sendPushoverNotification(config.secondUserKey, config.pushoverAppToken, message); console.log('✅ All notifications sent successfully!\n'); } catch (error) { console.log(`❌ Notification test failed: ${error.message}\n`); } // Test 4: Data parsing console.log('4. Testing data parsing...'); const powerMatch = testData.current_power.match(/(\d+(?:\.\d+)?)/); const todayMatch = testData.today_energy.match(/(\d+(?:\.\d+)?)/); const yesterdayMatch = testData.yesterday_energy.match(/(\d+(?:\.\d+)?)/); console.log(` Current Power: ${powerMatch ? powerMatch[1] + ' kW' : 'N/A'}`); console.log(` Today Energy: ${todayMatch ? todayMatch[1] + ' kWh' : 'N/A'}`); console.log(` Yesterday Energy: ${yesterdayMatch ? yesterdayMatch[1] + ' kWh' : 'N/A'}`); console.log('\n=== Test Complete ==='); } // Run tests runTests().catch(console.error);