homebridge-sense-energy-monitor
Version:
Enhanced Homebridge plugin for Sense Home Energy Monitor with comprehensive API integration and real-time monitoring
41 lines • 1.62 kB
JavaScript
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
export function normalizeConfig(raw) {
const errors = [];
if (!raw) {
return { config: null, errors: ['No configuration provided'] };
}
if (!raw.username || typeof raw.username !== 'string') {
errors.push('username is required and must be a string');
}
if (!raw.password || typeof raw.password !== 'string') {
errors.push('password is required and must be a string');
}
if (raw.mfaEnabled && (!raw.mfaSecret || typeof raw.mfaSecret !== 'string')) {
errors.push('mfaSecret is required when mfaEnabled is true');
}
if (errors.length > 0) {
return { config: null, errors };
}
const pollingIntervalSec = clamp(raw.pollingInterval ?? 60, 30, 3600);
const deviceLoggingMin = clamp(raw.deviceLoggingInterval ?? 2, 1, 60);
return {
config: {
name: raw.name ?? 'Sense Energy Monitor',
username: raw.username,
password: raw.password,
mfaEnabled: raw.mfaEnabled === true,
mfaSecret: raw.mfaSecret ?? null,
monitorId: raw.monitor_id ?? null,
pollingIntervalMs: pollingIntervalSec * 1000,
deviceLoggingIntervalMs: deviceLoggingMin * 60 * 1000,
useWebSocket: raw.useWebSocket !== false,
devicePowerThreshold: clamp(raw.devicePowerThreshold ?? 10, 1, 100),
enableHistory: raw.enableHistory !== false,
verbose: raw.verbose === true,
},
errors: [],
};
}
//# sourceMappingURL=config.js.map