MQTT publish to HA
This commit is contained in:
parent
30985747a6
commit
16eace739a
2 changed files with 128 additions and 0 deletions
48
app.js
48
app.js
|
|
@ -93,6 +93,54 @@ function renderAll() {
|
|||
renderWarnings();
|
||||
renderEntries();
|
||||
renderPlanner();
|
||||
scheduleHaPublish();
|
||||
}
|
||||
|
||||
function scheduleHaPublish() {
|
||||
clearTimeout(scheduleHaPublish.timer);
|
||||
scheduleHaPublish.timer = setTimeout(() => {
|
||||
publishHaState().catch((error) => console.warn('HA publish failed', error));
|
||||
}, 600);
|
||||
}
|
||||
|
||||
async function publishHaState() {
|
||||
if (!state.plan?.current) return;
|
||||
const current = state.plan.current;
|
||||
await post({
|
||||
action: 'publishHa',
|
||||
state: {
|
||||
feriefridage: Number(current.feriefridage || 0),
|
||||
vacation_days: Number(current.vacation || 0),
|
||||
flex_hours: Number(current.flex || 0),
|
||||
total_days: Number(current.totalDays || 0),
|
||||
upcoming_vacations: upcomingVacationsForHa(),
|
||||
updated_at: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function upcomingVacationsForHa() {
|
||||
const today = toIsoDate(new Date());
|
||||
return state.entries
|
||||
.filter((entry) => entry.end_date >= today)
|
||||
.sort((a, b) => a.start_date.localeCompare(b.start_date) || a.id - b.id)
|
||||
.map((entry) => {
|
||||
const summary = state.plan.entrySummaries[entry.id] || { workdays: 0, usedVacation: 0, usedFeriefridage: 0, usedFlex: 0, usedFlexDays: 0 };
|
||||
return {
|
||||
title: entry.title,
|
||||
start: entry.start_date,
|
||||
end: entry.end_date,
|
||||
start_display: formatDate(entry.start_date),
|
||||
end_display: formatDate(entry.end_date),
|
||||
status: entry.status,
|
||||
source: entry.source,
|
||||
workdays: Number(summary.workdays || 0),
|
||||
vacation_days: Number(summary.usedVacation || 0),
|
||||
feriefridage: Number(summary.usedFeriefridage || 0),
|
||||
flex_hours: Number(summary.usedFlex || 0),
|
||||
flex_days: Number(summary.usedFlexDays || 0),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function fillSettingsForm() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue