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

Support hour mode #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
64 changes: 44 additions & 20 deletions dist/meteo-france-weather-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,37 @@ function hasConfigOrEntityChanged(element, changedProps) {
return true;
}

function processForecast(lang, forecast) {
if (forecast === undefined || forecast.length == 0) {
return [];
} else {
let processedForecast = [];
for (let i=0; i<forecast.length; i++) {
const first = (i == 0);
const notLast = (i + 1 < forecast.length);
const date = new Date(forecast[i].datetime);
const millisecondsInOnHour = 3600 * 1000;
let hourMode = false;
if (first && notLast) {
const timediff = new Date(forecast[i+1].datetime) - date;
hourMode = (timediff == millisecondsInOnHour);
} else if (!first) {
const timediff = date - new Date(forecast[i-1].datetime);
hourMode = (timediff == millisecondsInOnHour);
}

processedForecast.push({
formattedDate: hourMode ? date.toLocaleTimeString(lang, {hour: "2-digit"}) : date.toLocaleDateString(lang, {weekday: "short"}),
condition: forecast[i].condition.toLowerCase(),
temperature: forecast[i].temperature,
templow : forecast[i].templow,
precipitation : forecast[i].precipitation
});
}
return processedForecast;
}
}

class WeatherCard extends LitElement {
static get properties() {
return {
Expand Down Expand Up @@ -194,6 +225,7 @@ class WeatherCard extends LitElement {
}

const lang = this.hass.selectedLanguage || this.hass.language;
const processedForecast = processForecast(lang, stateObj.attributes.forecast);

const next_rising = new Date(
this.hass.states["sun.sun"].attributes.next_rising
Expand Down Expand Up @@ -404,51 +436,43 @@ class WeatherCard extends LitElement {
: ""
}
${
stateObj.attributes.forecast &&
stateObj.attributes.forecast.length > 0
processedForecast.length > 0
? html`
<div class="forecast clear">
${
stateObj.attributes.forecast.slice(0, 5).map(
daily => html`
processedForecast.slice(0, 5).map(
element => html`
<div class="day">
<span class="dayname"
>${
new Date(daily.datetime).toLocaleDateString(
lang,
{
weekday: "short"
}
)
}</span
>
<span class="dayname">
${element.formattedDate}
</span>
<br /><i
class="icon"
style="background: none, url(${
this.getWeatherIcon(daily.condition.toLowerCase())
this.getWeatherIcon(element.condition)
}) no-repeat; background-size: contain;"
></i>
<br /><span class="highTemp"
>${daily.temperature}${
>${element.temperature}${
this.getUnit("temperature")
}</span
>
${
typeof daily.templow !== 'undefined'
typeof element.templow !== 'undefined'
? html`
<br /><span class="lowTemp"
>${daily.templow}${
>${element.templow}${
this.getUnit("temperature")
}</span
>
`
: ""
}
${
typeof daily.precipitation !== 'undefined'
typeof element.precipitation !== 'undefined'
? html`
<br /><span class="rainForcast"
>${daily.precipitation}${
>${element.precipitation}${
this.getUnit("precipitation")
}</span
>
Expand Down