Skip to content

Commit

Permalink
Wait for idle only on time change
Browse files Browse the repository at this point in the history
  • Loading branch information
amitfin committed May 22, 2023
1 parent 63be8b4 commit d4a0a01
Showing 1 changed file with 58 additions and 49 deletions.
107 changes: 58 additions & 49 deletions daily-schedule-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ class DailyScheduleCard extends HTMLElement {
content._value_element = value_element;
this._setCardRowValue(content, this._getStateSchedule(entity));
content.appendChild(value_element);
content.onclick = function () {
content.onclick = () => {
this._dialog._entity = entity;
this._dialog._title.innerText = name;
this._dialog._message.innerText = "";
this._dialog._plus._button.disabled = false;
this._dialog._schedule = [...this._getStateSchedule(entity)];
this._createDialogRows();
this._dialog.show();
}.bind(this);
};
return content;
}

Expand Down Expand Up @@ -145,14 +145,14 @@ class DailyScheduleCard extends HTMLElement {
const text = document.createElement("P");
text.innerText = "Add Range";
plus.appendChild(text);
plus.onclick = function () {
plus.onclick = () => {
if (button.disabled === true) {
return;
}
this._dialog._schedule.push({ from: null, to: null });
this._createDialogRows();
this._saveBackendEntity();
}.bind(this);
};
this._dialog._plus = plus;
const message = document.createElement("P");
message.style.display = "flex";
Expand Down Expand Up @@ -182,9 +182,9 @@ class DailyScheduleCard extends HTMLElement {
close.icon = "mdi:close";
close.style.marginLeft = "-4px";
close.style.cursor = "pointer";
close.onclick = function () {
close.onclick = () => {
this._dialog.close();
}.bind(this);
};
header.appendChild(close);
const title = document.createElement("P");
title.style.margin = "1px 0 0 0";
Expand All @@ -194,7 +194,7 @@ class DailyScheduleCard extends HTMLElement {
more_info.icon = "mdi:information-outline";
more_info.style.marginLeft = "auto";
more_info.style.cursor = "pointer";
more_info.onclick = function () {
more_info.onclick = () => {
this._dialog.close();
const event = new Event("hass-more-info", {
bubbles: true,
Expand All @@ -203,7 +203,7 @@ class DailyScheduleCard extends HTMLElement {
});
event.detail = {entityId: this._dialog._entity};
this.dispatchEvent(event);
}.bind(this);
};
header.appendChild(more_info);
return header;
}
Expand All @@ -226,16 +226,25 @@ class DailyScheduleCard extends HTMLElement {
const time = range.from.split(":");
from_input.value = time[0] + ":" + time[1];
}
from_input.onchange = function () {
from_input.onchange = () => {
if (from_input.value === "") {
return;
}
const time = from_input.value + ":00";
if (range.from !== time) {
range.from = time;
this._saveBackendEntity();
}
}.bind(this);
setTimeout(
(counter) => {
if (counter < this._dialog._save_counter) {
return;
}
const time = from_input.value + ":00";
if (range.from !== time) {
range.from = time;
this._saveBackendEntity();
}
},
1000,
++(this._dialog._save_counter),
);
};
row.appendChild(from_input);

const arrow = document.createElement("ha-icon");
Expand All @@ -250,16 +259,25 @@ class DailyScheduleCard extends HTMLElement {
const time = range.to.split(":");
to_input.value = time[0] + ":" + time[1];
}
to_input.onchange = function () {
to_input.onchange = () => {
if (to_input.value === "") {
return;
}
const time = to_input.value + ":00";
if (range.to !== time) {
range.to = time;
this._saveBackendEntity();
}
}.bind(this);
setTimeout(
(counter) => {
if (counter < this._dialog._save_counter) {
return;
}
const time = to_input.value + ":00";
if (range.to !== time) {
range.to = time;
this._saveBackendEntity();
}
},
1000,
++(this._dialog._save_counter),
);
};
row.appendChild(to_input);

const toggle = document.createElement("ha-switch");
Expand All @@ -274,11 +292,11 @@ class DailyScheduleCard extends HTMLElement {
const remove = document.createElement("ha-icon");
remove.icon = "mdi:delete-outline";
remove.style.cursor = "pointer";
remove.onclick = function () {
remove.onclick = () => {
this._dialog._schedule = this._dialog._schedule.filter((_, i) => i !== index);
this._createDialogRows();
this._saveBackendEntity();
}.bind(this);
};
row.appendChild(remove);

return row;
Expand All @@ -296,32 +314,23 @@ class DailyScheduleCard extends HTMLElement {
}
}

setTimeout(
function (counter) {
if (counter < this._dialog._save_counter) {
return;
this._hass
.callService("daily_schedule", "set", {
entity_id: this._dialog._entity,
schedule: this._dialog._schedule,
})
.then(() => {
if (this._dialog._message.innerText.length > 0) {
this._dialog._message.innerText = "";
}
this._hass
.callService("daily_schedule", "set", {
entity_id: this._dialog._entity,
schedule: this._dialog._schedule,
})
.then(() => {
if (this._dialog._message.innerText.length > 0) {
this._dialog._message.innerText = "";
}
this._dialog._plus._button.disabled = false;
})
.catch((error) => {
if (this._dialog._message.innerText !== error.message) {
this._dialog._message.innerText = error.message;
}
return Promise.reject(error);
});
}.bind(this),
500,
++(this._dialog._save_counter),
);
this._dialog._plus._button.disabled = false;
})
.catch((error) => {
if (this._dialog._message.innerText !== error.message) {
this._dialog._message.innerText = error.message;
}
return Promise.reject(error);
});
}
}

Expand Down

0 comments on commit d4a0a01

Please sign in to comment.