This commit is contained in:
Mateusz Gruszczyński
2026-09-14 23:10:17 +02:00
parent 4bb9c8621a
commit 0a2fb8c6c7
47 changed files with 1337 additions and 372 deletions
+45 -19
View File
@@ -200,40 +200,66 @@ async function handleHistoryAction(button) {
async function loadEnergyHistory() {
const energyDevices = app.devices.filter(device => device.capabilities?.energy_meter === true || !!device.ha_energy_entity_id);
if (!energyDevices.length) {
app.historyEnergy = null;
const targets = normalizeEnergyHistoryTargets();
if (!targets.length || !app.historyEnergyTargets.length) {
app.historyEnergy = [];
return;
}
if (!energyDevices.some(device => device.id === app.historyEnergyDevice)) app.historyEnergyDevice = energyDevices[0].id;
const hours = Number($('#historyHours')?.value || 24);
const days = Math.max(1, Math.ceil(hours / 24));
app.historyEnergy = await api(`/api/history/energy?device_id=${encodeURIComponent(app.historyEnergyDevice)}&interval=${encodeURIComponent(app.historyEnergyInterval)}&days=${days}&limit=100000`);
const selected = app.historyEnergyTargets.slice(0, 8);
app.historyEnergy = await Promise.all(selected.map(targetId => api(`/api/history/energy?target_id=${encodeURIComponent(targetId)}&interval=${encodeURIComponent(app.historyEnergyInterval)}&days=${days}&limit=100000&compare=${encodeURIComponent(app.historyEnergyCompare)}`)));
}
function renderEnergyHistorySummary() {
const host = $('#historySummary'); if (!host) return;
const data = app.historyEnergy;
if (!data || data.source === 'none') { host.innerHTML = ''; return; }
const summary = data.summary || {};
const rows = [
[tr('energy.today'), summary.today],
[tr('energy.yesterday'), summary.yesterday],
[tr('energy.currentMonth'), summary.current_month],
[tr('energy.previousMonth'), summary.previous_month],
[tr('energy.periodTotal'), summary.period_total],
];
host.innerHTML = rows.map(([label, value]) => `<div class="history-stat"><small>${esc(label)}</small><strong>${Number(value || 0).toLocaleString(locale(), { maximumFractionDigits: 3 })} kWh</strong><span>${esc(data.source === 'gree_cloud' ? 'GREE Cloud' : 'Home Assistant')}</span></div>`).join('');
const data = (app.historyEnergy || []).filter(item => item && item.source !== 'none');
if (!data.length) { host.innerHTML = ''; return; }
if (data.length === 1) {
const item = data[0];
const summary = item.summary || {};
const rows = [
[tr('energy.today'), summary.today],
[tr('energy.yesterday'), summary.yesterday],
[tr('energy.currentMonth'), summary.current_month],
[tr('energy.previousMonth'), summary.previous_month],
[tr('energy.periodTotal'), summary.period_total],
];
if (item.comparison) rows.push([tr('energy.comparisonPeriod'), item.comparison.period_total]);
host.innerHTML = rows.map(([label, value]) => `<div class="history-stat"><small>${esc(label)}</small><strong>${Number(value || 0).toLocaleString(locale(), { maximumFractionDigits: 3 })} kWh</strong><span>${esc(item.target_name || '')} · ${esc(item.source === 'gree_cloud' ? 'GREE Cloud' : 'Home Assistant')}</span></div>`).join('');
return;
}
host.innerHTML = data.map(item => `<div class="history-stat"><small>${esc(item.target_name || item.target_id)}</small><strong>${Number(item.summary?.period_total || 0).toLocaleString(locale(), { maximumFractionDigits: 3 })} kWh</strong><span>${esc(item.source === 'gree_cloud' ? 'GREE Cloud' : 'Home Assistant')}${item.comparison ? ` · ${esc(tr('energy.comparisonPeriod'))}: ${Number(item.comparison.period_total || 0).toLocaleString(locale(), { maximumFractionDigits: 3 })} kWh` : ''}</span></div>`).join('');
}
function renderEnergyHistory() {
const host = $('#historyCharts'); if (!host) return;
const data = app.historyEnergy;
const data = (app.historyEnergy || []).filter(item => item && item.source !== 'none');
renderEnergyHistorySummary();
if (!data || data.source === 'none') {
if (!data.length) {
host.innerHTML = `<div class="empty"><strong>${esc(tr('energy.noData'))}</strong></div>`;
return;
}
const series = [];
data.forEach((item, index) => {
const color = historySeriesColor(index);
series.push({
key: `${item.target_id}:current`,
label: data.length === 1 ? `${item.target_name} · ${tr('energy.currentPeriod')}` : item.target_name,
color,
buckets: item.buckets || [],
});
if (app.historyEnergyCompare !== 'none' && item.comparison?.buckets?.length) {
series.push({
key: `${item.target_id}:comparison`,
label: `${item.target_name} · ${tr('energy.comparisonPeriod')}`,
color,
comparison: true,
buckets: item.comparison.buckets,
});
}
});
host.innerHTML = historyChartMarkup('energyConsumptionChart', tr('energy.title'), tr('energy.chartHint'));
drawEnergyBarChart($('#energyConsumptionChart'), data.buckets || [], { height: 360 });
drawEnergyBarChart($('#energyConsumptionChart'), series, { height: 360, interval: app.historyEnergyInterval });
}