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

Add option to filter events by location #416

Merged
merged 2 commits into from
Oct 4, 2024
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
8 changes: 8 additions & 0 deletions src/cards/trash-card/formSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ const getSchema = (customLocalize: LocalizeFunc, currentValues: TrashCardConfig,
}
}
]
},
{
name: 'location',
label: customLocalize(`editor.form.location.title`),
helper: customLocalize(`editor.form.location.helper`),
selector: {
text: {}
}
}
];

Expand Down
1 change: 1 addition & 0 deletions src/cards/trash-card/items/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ItemCard extends BaseItemElement {
id="info"
.primary=${with_label ? label : content}
.secondary=${with_label ? content : undefined}
.multiline=${true}
></ha-tile-info>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/cards/trash-card/trash-card-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const COLORMODES = [
interface TrashCardConfig {
entities: string[];
pattern?: ItemSettings[];
location?: string;
next_days?: number;
items_per_row?: number;
filter_events?: boolean;
Expand Down Expand Up @@ -74,6 +75,7 @@ const entityCardConfigStruct = assign(
object({
entities: optional(array(string())),
name: optional(string()),
location: optional(string()),
layout: optional(union([ literal(LAYOUTS[0]), literal(LAYOUTS[1]), literal(LAYOUTS[2]) ])),
fill_container: optional(boolean()),
filter_events: optional(boolean()),
Expand Down
4 changes: 4 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"refresh_rate": {
"title": "Update interval",
"helper": "Check for changes in the calendar every x minutes"
},
"location": {
"title": "Location",
"helper": "Filter events by given location, if none is given all are used."
}
},
"card": {
Expand Down
7 changes: 6 additions & 1 deletion src/utils/findActiveEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Config {

interface Options {
config: Config;
location?: string;
now: Date;
dropAfter: boolean;
filterFutureEventsDay: string;
Expand All @@ -32,12 +33,16 @@ const isNotPastWholeDayEvent = (item: CalendarEvent, now: Date, dropAfter: boole
(item.isWholeDayEvent && getDayFromDate(item.date.start) === getDayFromDate(now) && !dropAfter) ||
(item.isWholeDayEvent && getDayFromDate(item.date.start) !== getDayFromDate(now));

const findActiveEvents = (items: CalendarEvent[], { config, now, dropAfter, filterFutureEventsDay }: Options): CalendarEvent[] => {
const findActiveEvents = (items: CalendarEvent[], { config, now, dropAfter, filterFutureEventsDay, location }: Options): CalendarEvent[] => {
const dateString = `${filterFutureEventsDay}T00:00:00${getTimeZoneOffset()}`;
const dateMaxStart = new Date(dateString);

const activeItems = items.
filter((item): boolean => {
if (location && !item.content.location?.toLowerCase().includes(location.toLowerCase())) {
return false;
}

if (item.date.start > dateMaxStart) {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/getCalendarData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const getCalendarData = async (
debuggerInstance.log(`dropAfter`, dropAfter);
debuggerInstance.log(`now`, now);

if (config.location) {
debuggerInstance.log(`location filtering`, config.location);
}

const activeEvents = findActiveEvents(normalisedEvents, {
config: {
pattern: config.pattern!,
Expand All @@ -58,6 +62,7 @@ const getCalendarData = async (
},
dropAfter,
now,
location: config.location,
filterFutureEventsDay: end
});

Expand Down
Loading