Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added configuration value to set the time from when a current day event should be hidden #137

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cards/trash-card/trash-card-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ EntityWithOutIcon & {
// eslint-disable-next-line @typescript-eslint/naming-convention
full_size?: boolean;
// eslint-disable-next-line @typescript-eslint/naming-convention
drop_todayevents_from?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
use_summary?: boolean;
};

Expand All @@ -41,6 +43,9 @@ export const entityCardConfigStruct = assign(
use_summary: optional(boolean()),
// eslint-disable-next-line @typescript-eslint/naming-convention
next_days: optional(integer()),
// eslint-disable-next-line @typescript-eslint/naming-convention
drop_todayevents_from: optional(string()),

settings: optional(
object({
organic: optional(
Expand Down
18 changes: 16 additions & 2 deletions src/cards/trash-card/trash-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const OTHER_LABELS = new Set([
'next_days',
'filter_events',
'full_size',
'drop_todayevents_from',
'use_summary'
]);

Expand Down Expand Up @@ -147,6 +148,13 @@ const SCHEMA: HaFormSchema[] = [
{ name: 'fill_container', selector: { boolean: {}}},
{ name: 'full_size', selector: { boolean: {}}},
{ name: 'filter_events', selector: { boolean: {}}},
{ name: 'drop_todayevents_from',
default: {
hours: 11,
minutes: 0,
seconds: 0
},
selector: { time: {}}},
{ name: 'use_summary', selector: { boolean: {}}},
{ name: 'next_days',
selector: { number: {
Expand Down Expand Up @@ -180,9 +188,15 @@ export class TrashCardEditor extends LitElement implements LovelaceCardEditor {
void loadHaComponents();
}

public setConfig (config: TrashCardConfig): void {
public setConfig (config: Partial<TrashCardConfig>): void {
assert(config, entityCardConfigStruct);
this.config = config;
this.config = {
// eslint-disable-next-line @typescript-eslint/naming-convention
drop_todayevents_from: '10:00:00',
// eslint-disable-next-line @typescript-eslint/naming-convention
next_days: 2,
...config
};
}

protected updated (changedProps: PropertyValues): void {
Expand Down
4 changes: 4 additions & 0 deletions src/cards/trash-card/trash-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { eventToItem } from '../../utils/eventToItem';
import { findActiveEvent } from '../../utils/findActiveEvent';
import { getDayFromDate } from '../../utils/getDayFromDate';
import type { HassEntity } from 'home-assistant-js-websocket';
import { isTodayAfter } from '../../utils/isTodayAfter';
import { loadHaComponents } from 'lovelace-mushroom/src/utils/loader';
import { normaliseEvents } from '../../utils/normaliseEvents';
import type { RawCalendarEvent } from '../../utils/calendarEvents';
Expand Down Expand Up @@ -102,6 +103,8 @@ export class TrashCard extends LitElement implements LovelaceCard {

const uri = `calendars/${this.config?.entity}?start=${start}&end=${end}`;

const dropAfter = isTodayAfter(new Date(), this.config!.drop_todayevents_from ?? '10:00:00');

// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.hass.
callApi<RawCalendarEvent[]>('GET', uri).
Expand All @@ -113,6 +116,7 @@ export class TrashCard extends LitElement implements LovelaceCard {
// eslint-disable-next-line @typescript-eslint/naming-convention
filter_events: this.config!.filter_events
},
dropAfter,
now: new Date() }
),
{ settings: this.config!.settings!, useSummary: Boolean(this.config!.use_summary) }
Expand Down
1 change: 1 addition & 0 deletions src/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"fill_container": "Container ausfüllen",
"next_days": "Tage in der Zukunft",
"filter_events": "Ereignisse nach Mustern filtern",
"drop_todayevents_from": "Ab wieviel Uhr Ganztages Ereignis ausblenden",
"full_size": "Karte ohne Seitenrand",
"use_summary": "Ereignisse Bezeichnung anstelle des Lables verwenden"
},
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"fill_container": "Fill container",
"next_days": "Days in the future",
"filter_events": "Filter events by patterns",
"drop_todayevents_from": "From what time to hide all-day event",
"full_size": "Card without margin",
"use_summary": "Event summary instead of label"
},
Expand Down
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"fill_container": "Remplir le conteneur",
"next_days": "Jours dans le futur",
"filter_events": "Filtrer les événements par motifs",
"drop_todayevents_from": "A partir de quelle heure masquer l'événement de toute la journée ?",
"full_size": "Carte sans marge",
"use_summary": "Résumé de l'événement au lieu de l'étiquette"
},
Expand Down
3 changes: 2 additions & 1 deletion src/translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"fill_container": "Naplňte nádobu",
"next_days": "Dni v budúcnosti",
"filter_events": "Filtrovanie udalostí podľa vzorov",
"drop_todayevents_from": "d akého času sa má skryť celodenná udalosť",
"full_size": "Karta bez marže",
"use_summary": "Zhrnutie udalosti namiesto označenia"
},
Expand Down Expand Up @@ -67,4 +68,4 @@
"day_from_till": "<DAY>\nmedzi <START> a <END>"
}
}
}
}
38 changes: 32 additions & 6 deletions src/utils/findActiveEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import calendarEvents from '../../mocks/calendarData.json';
import { findActiveEvent } from './findActiveEvent';
import { getTimeZoneOffset } from './getTimeZoneOffset';
import { isTodayAfter } from './isTodayAfter';
import { normaliseEvents } from './normaliseEvents';
import type { RawCalendarEvent } from './calendarEvents';

Expand All @@ -10,14 +11,19 @@ describe('findActiveEvent', (): void => {
const emptyConfig = {
settings: {},
filter_events: undefined

};

test('the whole day event today cause its before 10 o`clock', async () => {
const events = normaliseEvents(calendarEvents as RawCalendarEvent[]);

const now = new Date(`2023-12-10T09:59:59${offset}`);
const dropAfter = isTodayAfter(now, '10:00:00');

const result = findActiveEvent(events, {
config: emptyConfig,
now: new Date(`2023-12-10T09:59:59${offset}`)
now,
dropAfter
});

expect(result).toEqual(expect.objectContaining({
Expand All @@ -33,9 +39,13 @@ describe('findActiveEvent', (): void => {
test('the next event because it is after 10 o`clock', async () => {
const events = normaliseEvents(calendarEvents as RawCalendarEvent[]);

const now = new Date(`2023-12-10T10:01:59${offset}`);
const dropAfter = isTodayAfter(now, '10:00:00');

const result = findActiveEvent(events, {
config: emptyConfig,
now: new Date(`2023-12-10T10:01:59${offset}`)
now,
dropAfter
});

expect(result).toEqual(expect.objectContaining({
Expand All @@ -51,9 +61,13 @@ describe('findActiveEvent', (): void => {
test('the first event, it`s today and has started but not ended', async () => {
const events = normaliseEvents(calendarEvents as RawCalendarEvent[]);

const now = new Date(`2023-12-14T13:45:00+01:00`);
const dropAfter = isTodayAfter(now, '10:00:00');

const result = findActiveEvent(events, {
config: emptyConfig,
now: new Date(`2023-12-14T13:45:00+01:00`)
now,
dropAfter
});

expect(result).toEqual(expect.objectContaining({
Expand All @@ -69,9 +83,13 @@ describe('findActiveEvent', (): void => {
test('the second event, event 2 is today but today in the past', async () => {
const events = normaliseEvents(calendarEvents as RawCalendarEvent[]);

const now = new Date(`2023-12-14T14:15:00+01:00`);
const dropAfter = isTodayAfter(now, '10:00:00');

const result = findActiveEvent(events, {
config: emptyConfig,
now: new Date(`2023-12-14T14:15:00+01:00`)
now,
dropAfter
});

expect(result).toEqual(expect.objectContaining({
Expand All @@ -87,6 +105,9 @@ describe('findActiveEvent', (): void => {
test('the last event, event 3 is matching the pattern', async () => {
const events = normaliseEvents(calendarEvents as RawCalendarEvent[]);

const now = new Date(`2023-12-14T14:15:00+01:00`);
const dropAfter = isTodayAfter(now, '10:00:00');

const result = findActiveEvent(events, {
config: {
settings: {
Expand All @@ -96,7 +117,8 @@ describe('findActiveEvent', (): void => {
},
filter_events: true
},
now: new Date(`2023-12-14T14:15:00+01:00`)
now,
dropAfter
});

expect(result).toEqual(expect.objectContaining({
Expand All @@ -112,6 +134,9 @@ describe('findActiveEvent', (): void => {
test('the second event, event 2 is today but today in the past and filtering is off', async () => {
const events = normaliseEvents(calendarEvents as RawCalendarEvent[]);

const now = new Date(`2023-12-14T14:15:00+01:00`);
const dropAfter = isTodayAfter(now, '10:00:00');

const result = findActiveEvent(events, {
config: {
settings: {
Expand All @@ -121,7 +146,8 @@ describe('findActiveEvent', (): void => {
},
filter_events: false
},
now: new Date(`2023-12-14T14:15:00+01:00`)
now,
dropAfter
});

expect(result).toEqual(expect.objectContaining({
Expand Down
9 changes: 5 additions & 4 deletions src/utils/findActiveEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Config {
interface Options {
config: Config;
now: Date;
dropAfter: boolean;
}

const isMatchingAnyPatterns = (item: CalendarEvent, config: Config) => {
Expand All @@ -24,11 +25,11 @@ const isMatchingAnyPatterns = (item: CalendarEvent, config: Config) => {
return patterns.length === 0 || patterns.find(pattern => item.content.summary.includes(pattern));
};

const isNotPastWholeDayEvent = (item: CalendarEvent, now: Date): boolean =>
(item.isWholeDayEvent && getDayFromDate(item.date.start) === getDayFromDate(now) && now.getHours() < 10) ||
const isNotPastWholeDayEvent = (item: CalendarEvent, now: Date, dropAfter: boolean): boolean =>
(item.isWholeDayEvent && getDayFromDate(item.date.start) === getDayFromDate(now) && !dropAfter) ||
(item.isWholeDayEvent && getDayFromDate(item.date.start) !== getDayFromDate(now));

const findActiveEvent = (items: CalendarEvent[], { config, now }: Options): CalendarEvent | undefined => {
const findActiveEvent = (items: CalendarEvent[], { config, now, dropAfter }: Options): CalendarEvent | undefined => {
const activeItems = items.
filter((item): boolean => {
if (item.isWholeDayEvent) {
Expand All @@ -46,7 +47,7 @@ const findActiveEvent = (items: CalendarEvent[], { config, now }: Options): Cale
return activeItems.
find((item): boolean =>
isMatchingAnyPatterns(item, config) &&
(isNotPastWholeDayEvent(item, now) ||
(isNotPastWholeDayEvent(item, now, dropAfter) ||
!item.isWholeDayEvent));
};

Expand Down
23 changes: 23 additions & 0 deletions src/utils/isTodayAfter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { getTimeZoneOffset } from './getTimeZoneOffset';
import { isTodayAfter } from './isTodayAfter';

describe('isTodayAfter', (): void => {
const offset = getTimeZoneOffset();

test('11:10:00 is after 11:00:00', async () => {
const date = new Date(`1970-01-01T11:10:00.000${offset}`);

const result = isTodayAfter(date, '11:00:00');

expect(result).toEqual(true);
});

test('11:10:00 is before 11:10:01', async () => {
const date = new Date(`1970-01-01T11:10:00.000${offset}`);

const result = isTodayAfter(date, '11:10:01');

expect(result).toEqual(false);
});
});
15 changes: 15 additions & 0 deletions src/utils/isTodayAfter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getDayFromDate } from './getDayFromDate';
import { getTimeZoneOffset } from './getTimeZoneOffset';

const isTodayAfter = (now: Date, dropAfter: string): boolean => {
const [ hours, minutes, seconds ] = dropAfter.split(':');

const dropAfterDateString = `${getDayFromDate(now)}T${hours}:${minutes}:${seconds}.000${getTimeZoneOffset()}`;
const dropAfterDate = new Date(dropAfterDateString);

return now > dropAfterDate;
};

export {
isTodayAfter
};
Loading