Skip to content

Commit

Permalink
fix(time): Improve handling of cronjobs creation
Browse files Browse the repository at this point in the history
Fixes #1490
  • Loading branch information
zachowj committed Oct 1, 2024
1 parent c76105b commit 680227a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/nodes/time/TimeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DEFAULT_PROPERTY = 'state';
const ExposeAsController = ExposeAsMixin(OutputController<TimeNode>);
export default class TimeController extends ExposeAsController {
#cronjob: CronosTask | null = null;
#isAlreadyRunning = false;

#createCronjob(crontab: string) {
this.node.debug(`Creating cronjob: ${crontab}`);
Expand Down Expand Up @@ -169,6 +170,8 @@ export default class TimeController extends ExposeAsController {
}

public async onStateChanged() {
if (this.#isAlreadyRunning) return;

const property = this.node.config.property || DEFAULT_PROPERTY;
const entity = this.#getEntity();
const dateString = selectn(property, entity);
Expand All @@ -185,6 +188,7 @@ export default class TimeController extends ExposeAsController {
// Doesn't match time format 00:00:00
if (!digits) {
if (!isValidDate(dateString)) {
this.#isAlreadyRunning = false;
throw new ConfigError(
['ha-time.error.invalid_date', { date: dateString }],
'ha-time.status.invalid_date',
Expand Down Expand Up @@ -224,12 +228,15 @@ export default class TimeController extends ExposeAsController {
);
}
this.status.setFailed('ha-time.status.in_the_past');
this.#isAlreadyRunning = false;
return;
}

this.#createCronjob(crontab);

const nextTime = this.#formatDate(this.#cronjob?.nextRun);
this.status.setText(RED._('ha-time.status.next_at', { nextTime }));

this.#isAlreadyRunning = false;
}
}

0 comments on commit 680227a

Please sign in to comment.