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

End of season #27

Merged
merged 4 commits into from
Nov 22, 2022
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
14 changes: 7 additions & 7 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": "0.1.4",
"version": "0.1.6",
"description": "Frontend card for hass-formulaoneapi",
"main": "index.js",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion src/cards/driver-standings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default class DriverStandings extends BaseCard {
}

getCountryFlag = (nationality: string) => {
console.log(nationality, countries.filter(x => x.Nationality === nationality).length);
const country = countries.filter(x => x.Nationality === nationality)[0].Country;
const countryDashed = country.replace(" ","-");

Expand Down
8 changes: 8 additions & 0 deletions src/cards/next-race.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ export default class NextRace extends BaseCard {
return html`<h2><img height="25" src="${getCountryFlagUrl(countryDashed)}">&nbsp; ${this.next_race.round} : ${this.next_race.raceName}</h2>${imageWithLinkHtml}<br> `
}

renderSeasonEnded(): HTMLTemplateResult {
return html`<table><tr><td class="text-center"><strong>Season is over. See you next year!</strong></td></tr></table>`;
}

render() : HTMLTemplateResult {

if(!this.sensor_entity_id.endsWith('_races') || this.next_race === undefined) {
throw new Error('Please pass the correct sensor (races)')
}

if(!this.next_race) {
return this.renderSeasonEnded();
}

const raceDate = new Date(this.next_race.date + 'T' + this.next_race.time);
const freePractice1 = formatDateTimeRaceInfo(new Date(this.next_race.FirstPractice.date + 'T' + this.next_race.FirstPractice.time), this.hass.locale);
const freePractice2 = formatDateTimeRaceInfo(new Date(this.next_race.SecondPractice.date + 'T' + this.next_race.SecondPractice.time), this.hass.locale);
Expand Down
14 changes: 14 additions & 0 deletions src/cards/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import { BaseCard } from "./base-card";

export default class Schedule extends BaseCard {

next_race: Race;

constructor(sensor: string, hass: HomeAssistant, config: FormulaOneCardConfig) {
super(sensor, hass, config);

const sensorEntity = this.hass.states[this.sensor_entity_id];

this.next_race = sensorEntity.attributes['next_race'] as Race;
}

renderSeasonEnded(): HTMLTemplateResult {
return html`<table><tr><td class="text-center"><strong>Season is over. See you next year!</strong></td></tr></table>`;
}

renderLocation(circuit: Circuit) {
const locationConcatted = `${circuit.Location.locality}, ${circuit.Location.country}`;
return this.config.location_clickable ? html`<a href="${circuit.url}" target="_blank">${locationConcatted}</a>` : locationConcatted;
Expand All @@ -35,6 +45,10 @@ export default class Schedule extends BaseCard {
if(!this.sensor_entity_id.endsWith('_races') || data === undefined) {
throw new Error('Please pass the correct sensor (races)')
}

if(!this.next_race) {
return this.renderSeasonEnded();
}

return html`
<table>
Expand Down
12 changes: 12 additions & 0 deletions tests/cards/next-race.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,17 @@ describe('Testing next-race file', () => {
const htmlResult = getRenderString(result);

expect(htmlResult).toMatch('<table> <tbody> <tr> <td colspan="5"><h2><img height="25" src="https://www.countries-ofthe-world.com/flags-normal/flag-of-Bahrain.png">&nbsp; 1 : Bahrain Grand Prix</h2><a target="_new" href="http://en.wikipedia.org/wiki/Bahrain_International_Circuit"><img width="100%" src="https://www.formula1.com/content/dam/fom-website/2018-redesign-assets/Circuit%20maps%2016x9/Bahrain_Circuit.png.transform/7col/image.png"></a><br> </td> </tr> <tr><td>Date</td><td>20-03-22</td><td>&nbsp;</td><td>Practice 1</td><td align="right">vr 13:00</td></tr> <tr><td>Race</td><td>1</td><td>&nbsp;</td><td>Practice 2</td><td align="right">vr 16:00</td></tr> <tr><td>Race name</td><td>Bahrain Grand Prix</td><td>&nbsp;</td><td>Practice 3</td><td align="right">-</td></tr> <tr><td>Circuit name</td><td>Bahrain International Circuit</td><td>&nbsp;</td><td>Qualifying</td><td align="right">-</td></tr> <tr><td>Location</td><td>Bahrain</td><td>&nbsp;</td><td>Sprint</td><td align="right">zo 10:00</td></tr> <tr><td>City</td><td>Sakhir</td><td>&nbsp;</td><td>Race</td><td align="right">zo 16:00</td></tr> </tbody> </table>');
}),
test('Calling render with hass and sensor when season ended', () => {
hassEntity.attributes['next_race'] = null;
hass.states = {
'sensor.test_sensor_races': hassEntity
};

const card = new NextRace('sensor.test_sensor_races', hass, config);
const result = card.render();
const htmlResult = getRenderString(result);

expect(htmlResult).toMatch('<table><tr><td class="text-center"><strong>Season is over. See you next year!</strong></td></tr></table>');
})
});
17 changes: 16 additions & 1 deletion tests/cards/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Testing schedule file', () => {
expect(() => card.render()).toThrowError('Please pass the correct sensor (races)');
}),
test('Calling render with hass and sensor', () => {
hassEntity.attributes['next_race'] = data[0] as Race;
hassEntity.attributes['data'] = data as Race[];
hass.states = {
'sensor.test_sensor_races': hassEntity
Expand All @@ -49,7 +50,8 @@ describe('Testing schedule file', () => {

expect(htmlResult).toMatch('<table> <thead> <tr> <th>&nbsp;</th> <th>Race</th> <th>Location</th> <th class="text-center">Date</th> <th class="text-center">Time</th> </tr> </thead> <tbody> <tr class=""> <td class="width-50 text-center">1</td> <td>Bahrain International Circuit</td> <td>Sakhir, Bahrain</td> <td class="width-60 text-center">20-03</td> <td class="width-50 text-center">16:00</td> </tr> <tr class=""> <td class="width-50 text-center">2</td> <td>Jeddah Corniche Circuit</td> <td>Jeddah, Saudi Arabia</td> <td class="width-60 text-center">27-03</td> <td class="width-50 text-center">19:00</td> </tr> <tr class=""> <td class="width-50 text-center">3</td> <td>Albert Park Grand Prix Circuit</td> <td>Melbourne, Australia</td> <td class="width-60 text-center">10-04</td> <td class="width-50 text-center">7:00</td> </tr> <tr class=""> <td class="width-50 text-center">4</td> <td>Autodromo Enzo e Dino Ferrari</td> <td>Imola, Italy</td> <td class="width-60 text-center">24-04</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">5</td> <td>Miami International Autodrome</td> <td>Miami, USA</td> <td class="width-60 text-center">08-05</td> <td class="width-50 text-center">21:30</td> </tr> <tr class=""> <td class="width-50 text-center">6</td> <td>Circuit de Barcelona-Catalunya</td> <td>Montmeló, Spain</td> <td class="width-60 text-center">22-05</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">7</td> <td>Circuit de Monaco</td> <td>Monte-Carlo, Monaco</td> <td class="width-60 text-center">29-05</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">8</td> <td>Baku City Circuit</td> <td>Baku, Azerbaijan</td> <td class="width-60 text-center">12-06</td> <td class="width-50 text-center">13:00</td> </tr> <tr class=""> <td class="width-50 text-center">9</td> <td>Circuit Gilles Villeneuve</td> <td>Montreal, Canada</td> <td class="width-60 text-center">19-06</td> <td class="width-50 text-center">20:00</td> </tr> <tr class=""> <td class="width-50 text-center">10</td> <td>Silverstone Circuit</td> <td>Silverstone, UK</td> <td class="width-60 text-center">03-07</td> <td class="width-50 text-center">16:00</td> </tr> <tr class=""> <td class="width-50 text-center">11</td> <td>Red Bull Ring</td> <td>Spielberg, Austria</td> <td class="width-60 text-center">10-07</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">12</td> <td>Circuit Paul Ricard</td> <td>Le Castellet, France</td> <td class="width-60 text-center">24-07</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">13</td> <td>Hungaroring</td> <td>Budapest, Hungary</td> <td class="width-60 text-center">31-07</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">14</td> <td>Circuit de Spa-Francorchamps</td> <td>Spa, Belgium</td> <td class="width-60 text-center">28-08</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">15</td> <td>Circuit Park Zandvoort</td> <td>Zandvoort, Netherlands</td> <td class="width-60 text-center">04-09</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">16</td> <td>Autodromo Nazionale di Monza</td> <td>Monza, Italy</td> <td class="width-60 text-center">11-09</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">17</td> <td>Marina Bay Street Circuit</td> <td>Marina Bay, Singapore</td> <td class="width-60 text-center">02-10</td> <td class="width-50 text-center">14:00</td> </tr> <tr class=""> <td class="width-50 text-center">18</td> <td>Suzuka Circuit</td> <td>Suzuka, Japan</td> <td class="width-60 text-center">09-10</td> <td class="width-50 text-center">7:00</td> </tr> <tr class=""> <td class="width-50 text-center">19</td> <td>Circuit of the Americas</td> <td>Austin, USA</td> <td class="width-60 text-center">23-10</td> <td class="width-50 text-center">21:00</td> </tr> <tr class=""> <td class="width-50 text-center">20</td> <td>Autódromo Hermanos Rodríguez</td> <td>Mexico City, Mexico</td> <td class="width-60 text-center">30-10</td> <td class="width-50 text-center">21:00</td> </tr> <tr class=""> <td class="width-50 text-center">21</td> <td>Autódromo José Carlos Pace</td> <td>São Paulo, Brazil</td> <td class="width-60 text-center">13-11</td> <td class="width-50 text-center">19:00</td> </tr> <tr class=""> <td class="width-50 text-center">22</td> <td>Yas Marina Circuit</td> <td>Abu Dhabi, UAE</td> <td class="width-60 text-center">20-11</td> <td class="width-50 text-center">14:00</td> </tr> </tbody> </table>');
}),
test('Calling render with hass and sensor and location clickable', () => {
test('Calling render with hass and sensor and location clickable', () => {
hassEntity.attributes['next_race'] = data[0] as Race;
hassEntity.attributes['data'] = data as Race[];
hass.states = {
'sensor.test_sensor_races': hassEntity
Expand Down Expand Up @@ -84,5 +86,18 @@ describe('Testing schedule file', () => {

jest.useRealTimers();
expect(htmlResult).toMatch(`<table> <thead> <tr> <th>&nbsp;</th> <th>Race</th> <th>Location</th> <th class="text-center">Date</th> <th class="text-center">Time</th> </tr> </thead> <tbody> <tr class="${previous_race}"> <td class="width-50 text-center">1</td> <td>Bahrain International Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Bahrain_International_Circuit" target="_blank">Sakhir, Bahrain</a></td> <td class="width-60 text-center">20-03</td> <td class="width-50 text-center">16:00</td> </tr> <tr class="${previous_race}"> <td class="width-50 text-center">2</td> <td>Jeddah Corniche Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Jeddah_Street_Circuit" target="_blank">Jeddah, Saudi Arabia</a></td> <td class="width-60 text-center">27-03</td> <td class="width-50 text-center">19:00</td> </tr> <tr class=""> <td class="width-50 text-center">3</td> <td>Albert Park Grand Prix Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Melbourne_Grand_Prix_Circuit" target="_blank">Melbourne, Australia</a></td> <td class="width-60 text-center">10-04</td> <td class="width-50 text-center">7:00</td> </tr> <tr class=""> <td class="width-50 text-center">4</td> <td>Autodromo Enzo e Dino Ferrari</td> <td><a href="http://en.wikipedia.org/wiki/Autodromo_Enzo_e_Dino_Ferrari" target="_blank">Imola, Italy</a></td> <td class="width-60 text-center">24-04</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">5</td> <td>Miami International Autodrome</td> <td><a href="http://en.wikipedia.org/wiki/Miami_International_Autodrome" target="_blank">Miami, USA</a></td> <td class="width-60 text-center">08-05</td> <td class="width-50 text-center">21:30</td> </tr> <tr class=""> <td class="width-50 text-center">6</td> <td>Circuit de Barcelona-Catalunya</td> <td><a href="http://en.wikipedia.org/wiki/Circuit_de_Barcelona-Catalunya" target="_blank">Montmeló, Spain</a></td> <td class="width-60 text-center">22-05</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">7</td> <td>Circuit de Monaco</td> <td><a href="http://en.wikipedia.org/wiki/Circuit_de_Monaco" target="_blank">Monte-Carlo, Monaco</a></td> <td class="width-60 text-center">29-05</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">8</td> <td>Baku City Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Baku_City_Circuit" target="_blank">Baku, Azerbaijan</a></td> <td class="width-60 text-center">12-06</td> <td class="width-50 text-center">13:00</td> </tr> <tr class=""> <td class="width-50 text-center">9</td> <td>Circuit Gilles Villeneuve</td> <td><a href="http://en.wikipedia.org/wiki/Circuit_Gilles_Villeneuve" target="_blank">Montreal, Canada</a></td> <td class="width-60 text-center">19-06</td> <td class="width-50 text-center">20:00</td> </tr> <tr class=""> <td class="width-50 text-center">10</td> <td>Silverstone Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Silverstone_Circuit" target="_blank">Silverstone, UK</a></td> <td class="width-60 text-center">03-07</td> <td class="width-50 text-center">16:00</td> </tr> <tr class=""> <td class="width-50 text-center">11</td> <td>Red Bull Ring</td> <td><a href="http://en.wikipedia.org/wiki/Red_Bull_Ring" target="_blank">Spielberg, Austria</a></td> <td class="width-60 text-center">10-07</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">12</td> <td>Circuit Paul Ricard</td> <td><a href="http://en.wikipedia.org/wiki/Paul_Ricard_Circuit" target="_blank">Le Castellet, France</a></td> <td class="width-60 text-center">24-07</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">13</td> <td>Hungaroring</td> <td><a href="http://en.wikipedia.org/wiki/Hungaroring" target="_blank">Budapest, Hungary</a></td> <td class="width-60 text-center">31-07</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">14</td> <td>Circuit de Spa-Francorchamps</td> <td><a href="http://en.wikipedia.org/wiki/Circuit_de_Spa-Francorchamps" target="_blank">Spa, Belgium</a></td> <td class="width-60 text-center">28-08</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">15</td> <td>Circuit Park Zandvoort</td> <td><a href="http://en.wikipedia.org/wiki/Circuit_Zandvoort" target="_blank">Zandvoort, Netherlands</a></td> <td class="width-60 text-center">04-09</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">16</td> <td>Autodromo Nazionale di Monza</td> <td><a href="http://en.wikipedia.org/wiki/Autodromo_Nazionale_Monza" target="_blank">Monza, Italy</a></td> <td class="width-60 text-center">11-09</td> <td class="width-50 text-center">15:00</td> </tr> <tr class=""> <td class="width-50 text-center">17</td> <td>Marina Bay Street Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Marina_Bay_Street_Circuit" target="_blank">Marina Bay, Singapore</a></td> <td class="width-60 text-center">02-10</td> <td class="width-50 text-center">14:00</td> </tr> <tr class=""> <td class="width-50 text-center">18</td> <td>Suzuka Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Suzuka_Circuit" target="_blank">Suzuka, Japan</a></td> <td class="width-60 text-center">09-10</td> <td class="width-50 text-center">7:00</td> </tr> <tr class=""> <td class="width-50 text-center">19</td> <td>Circuit of the Americas</td> <td><a href="http://en.wikipedia.org/wiki/Circuit_of_the_Americas" target="_blank">Austin, USA</a></td> <td class="width-60 text-center">23-10</td> <td class="width-50 text-center">21:00</td> </tr> <tr class=""> <td class="width-50 text-center">20</td> <td>Autódromo Hermanos Rodríguez</td> <td><a href="http://en.wikipedia.org/wiki/Aut%C3%B3dromo_Hermanos_Rodr%C3%ADguez" target="_blank">Mexico City, Mexico</a></td> <td class="width-60 text-center">30-10</td> <td class="width-50 text-center">21:00</td> </tr> <tr class=""> <td class="width-50 text-center">21</td> <td>Autódromo José Carlos Pace</td> <td><a href="http://en.wikipedia.org/wiki/Aut%C3%B3dromo_Jos%C3%A9_Carlos_Pace" target="_blank">São Paulo, Brazil</a></td> <td class="width-60 text-center">13-11</td> <td class="width-50 text-center">19:00</td> </tr> <tr class=""> <td class="width-50 text-center">22</td> <td>Yas Marina Circuit</td> <td><a href="http://en.wikipedia.org/wiki/Yas_Marina_Circuit" target="_blank">Abu Dhabi, UAE</a></td> <td class="width-60 text-center">20-11</td> <td class="width-50 text-center">14:00</td> </tr> </tbody> </table>`);
}),
test('Calling render with hass and sensor season ended', () => {
hassEntity.attributes['next_race'] = null;
hassEntity.attributes['data'] = data as Race[];
hass.states = {
'sensor.test_sensor_races': hassEntity
};

const card = new Schedule('sensor.test_sensor_races', hass, config);
const result = card.render();
const htmlResult = getRenderString(result);

expect(htmlResult).toMatch('<table><tr><td class="text-center"><strong>Season is over. See you next year!</strong></td></tr></table>');
})
});