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 option to countdown till any event #166

Merged
merged 5 commits into from
Mar 2, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ or added by clicking the "Add to lovelace" button on the HACS dashboard after in
| translations | dictionary | _[translations](#Translations)_ | Dictionary to override the default translation |
| actions | object | _[Actions](#actions)_ | The tap, double tap or hold actions set on the image of the countdown, last_result, results, qualifying_results and next-race cards |
| row_limit | number | | Limit the schedule, results, last_result, driver_standings and constructor_standings to this amount of row |
| countdown_type | string | 'race' | Set the event to countdown to (race,qualifying,practice1,practice2,practice3,sprint) |


### Actions
Expand Down
12 changes: 6 additions & 6 deletions formulaone-card.js

Large diffs are not rendered by default.

Binary file modified formulaone-card.js.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formulaone-card",
"version": "1.0.3",
"version": "1.1.0",
"description": "Frontend card for Home Assistant to display Formula One data",
"main": "index.js",
"scripts": {
Expand Down
45 changes: 39 additions & 6 deletions src/cards/countdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Race } from "../api/models";
import { ActionHandlerEvent, hasAction, HomeAssistant } from "custom-card-helpers";
import FormulaOneCard from "..";
import { actionHandler } from "../directives/action-handler-directive";
import { CountdownType } from "../types/formulaone-card-types";

export default class Countdown extends BaseCard {
hass: HomeAssistant;
Expand Down Expand Up @@ -81,17 +82,12 @@ export default class Countdown extends BaseCard {
return html`${getApiErrorMessage('next race')}`
}

const nextRace = response.filter(race => {
const raceDateTime = new Date(race.date + 'T' + race.time);
raceDateTime.setHours(raceDateTime.getHours() + 3);
return raceDateTime >= new Date();
})[0];
const { nextRace, raceDateTime } = this.getNextEvent(response);

if(!nextRace) {
return getEndOfSeasonMessage(this.translation('endofseason'));
}

const raceDateTime = new Date(nextRace.date + 'T' + nextRace.time);
const timer = this.countDownTillDate(raceDateTime);
const hasConfigAction = this.config.actions !== undefined;

Expand All @@ -117,4 +113,41 @@ export default class Countdown extends BaseCard {
html`${getApiLoadingMessage()}`
)}`;
}

getNextEvent(response: Race[]) {

const nextRace = response.filter(race => {
const raceDateTime = new Date(race.date + 'T' + race.time);
raceDateTime.setHours(raceDateTime.getHours() + 3);
return raceDateTime >= new Date();
})[0];

let raceDateTime = null;
if(nextRace) {
switch(this.config.countdown_type as CountdownType) {
case CountdownType.Practice1:
raceDateTime = new Date(nextRace.FirstPractice.date + 'T' + nextRace.FirstPractice.time);
break;
case CountdownType.Practice2:
raceDateTime = new Date(nextRace.SecondPractice.date + 'T' + nextRace.SecondPractice.time);
break;
case CountdownType.Practice3:
raceDateTime = new Date(nextRace.ThirdPractice.date + 'T' + nextRace.ThirdPractice.time);
break;
case CountdownType.Qualifying:
raceDateTime = new Date(nextRace.Qualifying.date + 'T' + nextRace.Qualifying.time);
break;
case CountdownType.Sprint:
if(nextRace.Sprint) {
raceDateTime = new Date(nextRace.Sprint.date + 'T' + nextRace.Sprint.time);
}
break;
default:
raceDateTime = new Date(nextRace.date + 'T' + nextRace.time);
break;
}
}

return { nextRace, raceDateTime };
}
}
10 changes: 10 additions & 0 deletions src/types/formulaone-card-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export interface FormulaOneCardConfig extends LovelaceCardConfig {
f1_font?: boolean;
row_limit?: number;
icons?: CustomIcons;
countdown_type?: CountdownType;
}

export enum CountdownType {
Race = "race",
Qualifying = "qualifying",
Practice1 = "practice1",
Practice2 = "practice2",
Practice3 = "practice3",
Sprint = "sprint"
}

export interface ActionOptions {
Expand Down
22 changes: 21 additions & 1 deletion tests/cards/countdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMock } from "ts-auto-mock";
import Countdown from "../../src/cards/countdown";
import { FormulaOneCardConfig } from "../../src/types/formulaone-card-types";
import { CountdownType, FormulaOneCardConfig } from "../../src/types/formulaone-card-types";
import { getRenderStringAsync } from "../utils";
import { MRData } from '../testdata/schedule.json'
import { MRData as resultData } from '../testdata/results.json'
Expand Down Expand Up @@ -235,6 +235,26 @@ describe('Testing countdown file', () => {
expect(customCardHelper.handleAction).toBeCalledTimes(3);

spy.mockClear();
}),
test.each`
countdown_type | current_date | expected
${CountdownType.Practice1}, ${new Date(2022, 3, 19)}, ${new Date("2022-04-22T11:30:00.000Z")}
${CountdownType.Practice2}, ${new Date(2022, 3, 19)}, ${new Date("2022-04-23T10:30:00.000Z")}
${CountdownType.Practice3}, ${new Date(2022, 2, 1)}, ${new Date("2022-03-19T12:00:00.000Z")}
${CountdownType.Qualifying}, ${new Date(2022, 3, 19)}, ${new Date("2022-04-22T15:00:00.000Z")}
${CountdownType.Race}, ${new Date(2022, 3, 19)}, ${new Date("2022-04-24T13:00:00.000Z")}
${CountdownType.Sprint}, ${new Date(2022, 3, 19)}, ${new Date("2022-04-23T14:30:00.000Z")}
`(`Calling render with countdown_type $countdown_type`, async ({ countdown_type, current_date, expected }) => {
config.countdown_type = countdown_type;
card.config = config;

jest.useFakeTimers();
jest.setSystemTime(current_date); // Weird bug in jest setting this to the last of the month

const result = card.getNextEvent(MRData.RaceTable.Races);
jest.useRealTimers();

expect(result.raceDateTime).toMatchObject(expected);
});
});

Expand Down