Ensure forecast temperatures are properly positioned + show em-dash when n/a (#9066)

Co-authored-by: Zack Barett <zackbarett@hey.com>
This commit is contained in:
Philip Allgaier 2022-01-24 17:10:44 +01:00 committed by GitHub
parent 648c02e622
commit 3ddcd2d0f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 119 deletions

View File

@ -436,3 +436,19 @@ export const getWeatherStateIcon = (
return undefined; return undefined;
}; };
const DAY_IN_MILLISECONDS = 86400000;
export const isForecastHourly = (
forecast?: ForecastAttribute[]
): boolean | undefined => {
if (forecast && forecast?.length && forecast?.length > 2) {
const date1 = new Date(forecast[1].datetime);
const date2 = new Date(forecast[2].datetime);
const timeDiff = date2.getTime() - date1.getTime();
return timeDiff < DAY_IN_MILLISECONDS;
}
return undefined;
};

View File

@ -33,7 +33,11 @@ import { formatDateWeekday } from "../../../common/datetime/format_date";
import { formatTimeWeekday } from "../../../common/datetime/format_time"; import { formatTimeWeekday } from "../../../common/datetime/format_time";
import { formatNumber } from "../../../common/number/format_number"; import { formatNumber } from "../../../common/number/format_number";
import "../../../components/ha-svg-icon"; import "../../../components/ha-svg-icon";
import { getWeatherUnit, getWind } from "../../../data/weather"; import {
getWeatherUnit,
getWind,
isForecastHourly,
} from "../../../data/weather";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
const weatherIcons = { const weatherIcons = {
@ -82,6 +86,8 @@ class MoreInfoWeather extends LitElement {
return html``; return html``;
} }
const hourly = isForecastHourly(this.stateObj.attributes.forecast);
return html` return html`
<div class="flex"> <div class="flex">
<ha-svg-icon .path=${mdiThermometer}></ha-svg-icon> <ha-svg-icon .path=${mdiThermometer}></ha-svg-icon>
@ -169,9 +175,9 @@ class MoreInfoWeather extends LitElement {
<div class="section"> <div class="section">
${this.hass.localize("ui.card.weather.forecast")}: ${this.hass.localize("ui.card.weather.forecast")}:
</div> </div>
${this.stateObj.attributes.forecast.map( ${this.stateObj.attributes.forecast.map((item) =>
(item) => html` this._showValue(item.templow) || this._showValue(item.temperature)
<div class="flex"> ? html`<div class="flex">
${item.condition ${item.condition
? html` ? html`
<ha-svg-icon <ha-svg-icon
@ -179,7 +185,7 @@ class MoreInfoWeather extends LitElement {
></ha-svg-icon> ></ha-svg-icon>
` `
: ""} : ""}
${!this._showValue(item.templow) ${hourly
? html` ? html`
<div class="main"> <div class="main">
${formatTimeWeekday( ${formatTimeWeekday(
@ -188,29 +194,30 @@ class MoreInfoWeather extends LitElement {
)} )}
</div> </div>
` `
: ""} : html`
${this._showValue(item.templow)
? html`
<div class="main"> <div class="main">
${formatDateWeekday( ${formatDateWeekday(
new Date(item.datetime), new Date(item.datetime),
this.hass.locale this.hass.locale
)} )}
</div> </div>
`}
<div class="templow"> <div class="templow">
${formatNumber(item.templow, this.hass.locale)} ${this._showValue(item.templow)
${getWeatherUnit(this.hass, "temperature")} ? `${formatNumber(item.templow, this.hass.locale)}
${getWeatherUnit(this.hass, "temperature")}`
: hourly
? ""
: "—"}
</div> </div>
`
: ""}
<div class="temp"> <div class="temp">
${this._showValue(item.temperature) ${this._showValue(item.temperature)
? `${formatNumber(item.temperature, this.hass.locale)} ? `${formatNumber(item.temperature, this.hass.locale)}
${getWeatherUnit(this.hass, "temperature")}` ${getWeatherUnit(this.hass, "temperature")}`
: ""} : ""}
</div> </div>
</div> </div>`
` : ""
)} )}
` `
: ""} : ""}

View File

@ -24,6 +24,7 @@ import {
getWeatherStateIcon, getWeatherStateIcon,
getWeatherUnit, getWeatherUnit,
getWind, getWind,
isForecastHourly,
weatherAttrIcons, weatherAttrIcons,
WeatherEntity, WeatherEntity,
weatherSVGStyles, weatherSVGStyles,
@ -177,24 +178,16 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
: undefined; : undefined;
const weather = !forecast || this._config?.show_current !== false; const weather = !forecast || this._config?.show_current !== false;
let hourly: boolean | undefined; const hourly = isForecastHourly(forecast);
let dayNight: boolean | undefined; let dayNight: boolean | undefined;
if (forecast?.length && forecast?.length > 2) {
const date1 = new Date(forecast[1].datetime);
const date2 = new Date(forecast[2].datetime);
const timeDiff = date2.getTime() - date1.getTime();
hourly = timeDiff < DAY_IN_MILLISECONDS;
if (hourly) { if (hourly) {
const dateFirst = new Date(forecast[0].datetime); const dateFirst = new Date(forecast![0].datetime);
const datelast = new Date(forecast[forecast.length - 1].datetime); const datelast = new Date(forecast![forecast!.length - 1].datetime);
const dayDiff = datelast.getTime() - dateFirst.getTime(); const dayDiff = datelast.getTime() - dateFirst.getTime();
dayNight = dayDiff > DAY_IN_MILLISECONDS; dayNight = dayDiff > DAY_IN_MILLISECONDS;
} }
}
const weatherStateIcon = getWeatherStateIcon(stateObj.state, this); const weatherStateIcon = getWeatherStateIcon(stateObj.state, this);
const name = this._config.name ?? computeStateName(stateObj); const name = this._config.name ?? computeStateName(stateObj);
@ -288,8 +281,10 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
${forecast ${forecast
? html` ? html`
<div class="forecast"> <div class="forecast">
${forecast.map( ${forecast.map((item) =>
(item) => html` this._showValue(item.templow) ||
this._showValue(item.temperature)
? html`
<div> <div>
<div> <div>
${dayNight ${dayNight
@ -300,7 +295,9 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
)} )}
<div class="daynight"> <div class="daynight">
${item.daytime === undefined || item.daytime ${item.daytime === undefined || item.daytime
? this.hass!.localize("ui.card.weather.day") ? this.hass!.localize(
"ui.card.weather.day"
)
: this.hass!.localize( : this.hass!.localize(
"ui.card.weather.night" "ui.card.weather.night"
)}<br /> )}<br />
@ -320,37 +317,40 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
)} )}
`} `}
</div> </div>
${item.condition !== undefined && item.condition !== null ${this._showValue(item.condition)
? html` ? html`
<div class="forecast-image-icon"> <div class="forecast-image-icon">
${getWeatherStateIcon( ${getWeatherStateIcon(
item.condition, item.condition!,
this, this,
!(item.daytime || item.daytime === undefined) !(
item.daytime || item.daytime === undefined
)
)} )}
</div> </div>
` `
: ""} : ""}
${item.temperature !== undefined &&
item.temperature !== null
? html`
<div class="temp"> <div class="temp">
${formatNumber( ${this._showValue(item.temperature)
? html`${formatNumber(
item.temperature, item.temperature,
this.hass!.locale this.hass!.locale
)}° )}°`
: "—"}
</div> </div>
`
: ""}
${item.templow !== undefined && item.templow !== null
? html`
<div class="templow"> <div class="templow">
${formatNumber(item.templow, this.hass!.locale)}° ${this._showValue(item.templow)
</div> ? html`${formatNumber(
` item.templow!,
: ""} this.hass!.locale
)}°`
: hourly
? ""
: "—"}
</div>
</div> </div>
` `
: ""
)} )}
</div> </div>
` `
@ -402,6 +402,10 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
this._veryVeryNarrow = card.offsetWidth < 245; this._veryVeryNarrow = card.offsetWidth < 245;
} }
private _showValue(item?: any): boolean {
return typeof item !== "undefined" && item !== null;
}
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return [ return [
weatherSVGStyles, weatherSVGStyles,