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

use locale.time_format for displaying time #163

Open
wants to merge 1 commit 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
122 changes: 62 additions & 60 deletions dist/weather-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function hasConfigOrEntityChanged(element, changedProps) {
if (oldHass) {
return (
oldHass.states[element._config.entity] !==
element.hass.states[element._config.entity] ||
element.hass.states[element._config.entity] ||
oldHass.states["sun.sun"] !== element.hass.states["sun.sun"]
);
}
Expand Down Expand Up @@ -128,6 +128,7 @@ class WeatherCard extends LitElement {
this.numberElements = 0;

const lang = this.hass.selectedLanguage || this.hass.language;
const time_format = this.hass.locale.time_format;
const stateObj = this.hass.states[this._config.entity];

if (!stateObj) {
Expand All @@ -150,10 +151,10 @@ class WeatherCard extends LitElement {
return html`
<ha-card @click="${this._handleClick}">
${this._config.current !== false ? this.renderCurrent(stateObj) : ""}
${this._config.details !== false ? this.renderDetails(stateObj, lang) : ""}
${this._config.details !== false ? this.renderDetails(stateObj, lang, time_format) : ""}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass hass.locale here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can also work, but in HA there is a time format setting that can override system locale . If it's set, i think it should use time_formwt, then fallback to locale, and then to hass.lang.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice if this can be merged. I use an english OS, dutch locale. Logbook and history shows 24hr timeformat, unfortunately this card does not and gives me AM/PM times. It gives me headaches 🤣

${this._config.forecast !== false
? this.renderForecast(stateObj.attributes.forecast, lang)
: ""}
? this.renderForecast(stateObj.attributes.forecast, lang, time_format)
: ""}
</ha-card>
`;
}
Expand All @@ -166,38 +167,40 @@ class WeatherCard extends LitElement {
<span
class="icon bigger"
style="background: none, url('${this.getWeatherIcon(
stateObj.state.toLowerCase(),
this.hass.states["sun.sun"]
)}') no-repeat; background-size: contain;"
stateObj.state.toLowerCase(),
this.hass.states["sun.sun"]
)}') no-repeat; background-size: contain;"
>${stateObj.state}
</span>
${this._config.name
? html` <span class="title"> ${this._config.name} </span> `
: ""}
? html` <span class="title"> ${this._config.name} </span> `
: ""}
<span class="temp"
>${this.getUnit("temperature") == "°F"
? Math.round(stateObj.attributes.temperature)
: stateObj.attributes.temperature}</span
? Math.round(stateObj.attributes.temperature)
: stateObj.attributes.temperature}</span
>
<span class="tempc"> ${this.getUnit("temperature")}</span>
</div>
`;
}

renderDetails(stateObj, lang) {
renderDetails(stateObj, lang, time_format) {
const sun = this.hass.states["sun.sun"];
let next_rising;
let next_setting;

if (sun) {
next_rising = new Date(sun.attributes.next_rising).toLocaleTimeString(lang, {
hour: "2-digit",
minute: "2-digit",
});
hour12: !time_format ? undefined : time_format == "12",
hour: "2-digit",
minute: "2-digit",
});
next_setting = new Date(sun.attributes.next_setting).toLocaleTimeString(lang, {
hour: "2-digit",
minute: "2-digit",
});
hour12: !time_format ? undefined : time_format == "12",
hour: "2-digit",
minute: "2-digit",
});
}

this.numberElements++;
Expand All @@ -210,8 +213,8 @@ class WeatherCard extends LitElement {
</li>
<li>
<ha-icon icon="mdi:weather-windy"></ha-icon> ${windDirections[
parseInt((stateObj.attributes.wind_bearing + 11.25) / 22.5)
]}
parseInt((stateObj.attributes.wind_bearing + 11.25) / 22.5)
]}
${stateObj.attributes.wind_speed}<span class="unit">
${this.getUnit("length")}/h
</span>
Expand All @@ -225,31 +228,31 @@ class WeatherCard extends LitElement {
</li>
<li>
<ha-icon icon="mdi:weather-fog"></ha-icon> ${stateObj.attributes
.visibility}<span class="unit">
.visibility}<span class="unit">
${this.getUnit("length")}
</span>
</li>
${next_rising
? html`
? html`
<li>
<ha-icon icon="mdi:weather-sunset-up"></ha-icon>
${next_rising}
</li>
`
: ""}
: ""}
${next_setting
? html`
? html`
<li>
<ha-icon icon="mdi:weather-sunset-down"></ha-icon>
${next_setting}
</li>
`
: ""}
: ""}
</ul>
`;
}

renderForecast(forecast, lang) {
renderForecast(forecast, lang, time_format) {
if (!forecast || forecast.length === 0) {
return html``;
}
Expand All @@ -258,76 +261,75 @@ class WeatherCard extends LitElement {
return html`
<div class="forecast clear ${this.numberElements > 1 ? "spacer" : ""}">
${forecast
.slice(
0,
this._config.number_of_forecasts
? this._config.number_of_forecasts
: 5
)
.map(
(daily) => html`
.slice(
0,
this._config.number_of_forecasts
? this._config.number_of_forecasts
: 5
)
.map(
(daily) => html`
<div class="day">
<div class="dayname">
${this._config.hourly_forecast
? new Date(daily.datetime).toLocaleTimeString(lang, {
hour: "2-digit",
minute: "2-digit",
})
: new Date(daily.datetime).toLocaleDateString(lang, {
weekday: "short",
})}
? new Date(daily.datetime).toLocaleTimeString(lang, {
hour12: !time_format ? undefined : time_format == "12",
hour: "2-digit",
minute: "2-digit",
})
: new Date(daily.datetime).toLocaleDateString(lang, {
weekday: "short",
})}
</div>
<i
class="icon"
style="background: none, url('${this.getWeatherIcon(
daily.condition.toLowerCase()
)}') no-repeat; background-size: contain"
daily.condition.toLowerCase()
)}') no-repeat; background-size: contain"
></i>
<div class="highTemp">
${daily.temperature}${this.getUnit("temperature")}
</div>
${daily.templow !== undefined
? html`
? html`
<div class="lowTemp">
${daily.templow}${this.getUnit("temperature")}
</div>
`
: ""}
: ""}
${!this._config.hide_precipitation &&
daily.precipitation !== undefined &&
daily.precipitation !== null
? html`
daily.precipitation !== undefined &&
daily.precipitation !== null
? html`
<div class="precipitation">
${Math.round(daily.precipitation*10)/10} ${this.getUnit("precipitation")}
${Math.round(daily.precipitation * 10) / 10} ${this.getUnit("precipitation")}
</div>
`
: ""}
: ""}
${!this._config.hide_precipitation &&
daily.precipitation_probability !== undefined &&
daily.precipitation_probability !== null
? html`
daily.precipitation_probability !== undefined &&
daily.precipitation_probability !== null
? html`
<div class="precipitation_probability">
${Math.round(daily.precipitation_probability)} ${this.getUnit("precipitation_probability")}
</div>
`
: ""}
: ""}
</div>
`
)}
)}
</div>
`;
}

getWeatherIcon(condition, sun) {
return `${
this._config.icons
return `${this._config.icons
? this._config.icons
: "https://cdn.jsdelivr.net/gh/bramkragten/weather-card/dist/icons/"
}${
sun && sun.state == "below_horizon"
}${sun && sun.state == "below_horizon"
? weatherIconsNight[condition]
: weatherIconsDay[condition]
}.svg`;
}.svg`;
}

getUnit(measure) {
Expand Down