diff --git a/README.md b/README.md new file mode 100644 index 0000000..caceb0f --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Iracing Result Card + +This card displays the recent race results provided by the iRacing integration. + +## iRacing integration + +[Iracing integration](https://github.com/cazeaux/ha-iracing) + +## Installation + +Create a `www/iracing` folder in your `config`. If you have created the `www` folder for the first time, Home Assistant must be restarted. + +Copy the `iracing-result-card.js` file into your `config/www/iracing` folder. + +Add the custom resource by following these steps: + +* Activate the advanced mode in your settings + +![advanced settings](advanced-settings.png) + +* Go to the **dashboards** page in the parameters, and click on the three dots on the top right, click on `Resources` + +![resources](resources.png) + +* Add the resource as follows: + +![Alt text](add-resource.png) + +Use the URL : `/local/iracing/iracing-result-card.js` + +## How to use + +```yaml +type: custom:iracing-result-card +entity: sensor.DRIVER_NAME_driver +``` \ No newline at end of file diff --git a/add-resource.png b/add-resource.png new file mode 100644 index 0000000..f7eabaa Binary files /dev/null and b/add-resource.png differ diff --git a/advanced-settings.png b/advanced-settings.png new file mode 100644 index 0000000..23ffc64 Binary files /dev/null and b/advanced-settings.png differ diff --git a/hacs.json b/hacs.json new file mode 100644 index 0000000..54677b9 --- /dev/null +++ b/hacs.json @@ -0,0 +1,3 @@ +{ + "name": "Iracing Result Card" +} \ No newline at end of file diff --git a/info.md b/info.md new file mode 100644 index 0000000..59c3c26 --- /dev/null +++ b/info.md @@ -0,0 +1,10 @@ +# Iracing Result Card + +This card displays the recent race results provided by the iRacing integration. + +## How to use + +```yaml +type: custom:iracing-result-card +entity: sensor.DRIVER_NAME_driver +``` \ No newline at end of file diff --git a/iracing-result-card.js b/iracing-result-card.js new file mode 100644 index 0000000..55f6bc9 --- /dev/null +++ b/iracing-result-card.js @@ -0,0 +1,126 @@ +class IracingResultCard extends HTMLElement { + // Whenever the state changes, a new `hass` object is set. Use this to + // update your content. + set hass(hass) { + // Initialize the content if it's not there yet. + const entityId = this.config.entity; + const state = hass.states[entityId]; + const stateStr = state ? state.state : "unavailable"; + const json_results = hass.states[entityId].attributes.recent_results; + const displayed_results_max = Math.min(json_results.length, 3); + window.cardSize = displayed_results_max; + + if (!this.content) { + const card = document.createElement("ha-card"); + card.header = state.state; + this.content = document.createElement("div"); + this.content.style.padding = "5px 10px"; + card.appendChild(this.content); + this.appendChild(card); + } + + + if (!json_results || !json_results.length) { + return; + } + this.content.innerHTML = ""; + const style = document.createElement("style"); + + for (let count = 0; count < displayed_results_max; count++) { + const item = (key) => json_results[count][key]; + const track = (key) => item('track')[key]; + console.log(item); + + const diff_ir = item("newi_rating") - item("oldi_rating"); + const diff_ir_prefix = (diff_ir >= 0 ? '+' : '') + const diff_sr = (item("new_sub_level") - item("old_sub_level")) / 100; + const diff_sr_prefix = (diff_sr >= 0 ? '+' : '') + const new_sr = (item("new_sub_level")) / 100; + let position_icon = '🏁'; + if (item("finish_position") == 1) { + position_icon = '🥇'; + } else if (item("finish_position") == 2) { + position_icon = '🥈'; + } else if (item("finish_position") == 3) { + position_icon = '🥉'; + } + + this.content.innerHTML += ` +