mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-30 04:36:36 +00:00
Support twice daily forecasts in weather-forecast-card (#6065)
Co-authored-by: Zack Arnett <arnett.zackary@gmail.com>
This commit is contained in:
parent
dac6edea90
commit
4ba6698c4b
@ -210,7 +210,10 @@ export const weatherSVGStyles = css`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const getWeatherStateSVG = (state: string): SVGTemplateResult => {
|
export const getWeatherStateSVG = (
|
||||||
|
state: string,
|
||||||
|
nightTime?: boolean
|
||||||
|
): SVGTemplateResult => {
|
||||||
return svg`
|
return svg`
|
||||||
<svg
|
<svg
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
@ -237,7 +240,14 @@ export const getWeatherStateSVG = (state: string): SVGTemplateResult => {
|
|||||||
: ""
|
: ""
|
||||||
}
|
}
|
||||||
${
|
${
|
||||||
state === "partlycloudy"
|
state === "partlycloudy" && nightTime
|
||||||
|
? svg`
|
||||||
|
<path
|
||||||
|
class="moon"
|
||||||
|
d="m14.981 4.2112c0 1.9244-1.56 3.4844-3.484 3.4844-1.9244 0-3.4844-1.56-3.4844-3.4844s1.56-3.484 3.4844-3.484c1.924 0 3.484 1.5596 3.484 3.484"
|
||||||
|
/>
|
||||||
|
`
|
||||||
|
: state === "partlycloudy"
|
||||||
? svg`
|
? svg`
|
||||||
<path
|
<path
|
||||||
class="sun"
|
class="sun"
|
||||||
@ -343,7 +353,8 @@ export const getWeatherStateSVG = (state: string): SVGTemplateResult => {
|
|||||||
|
|
||||||
export const getWeatherStateIcon = (
|
export const getWeatherStateIcon = (
|
||||||
state: string,
|
state: string,
|
||||||
element: HTMLElement
|
element: HTMLElement,
|
||||||
|
nightTime?: boolean
|
||||||
): TemplateResult | undefined => {
|
): TemplateResult | undefined => {
|
||||||
const userDefinedIcon = getComputedStyle(element).getPropertyValue(
|
const userDefinedIcon = getComputedStyle(element).getPropertyValue(
|
||||||
`--weather-icon-${state}`
|
`--weather-icon-${state}`
|
||||||
@ -360,7 +371,7 @@ export const getWeatherStateIcon = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (weatherSVGs.has(state)) {
|
if (weatherSVGs.has(state)) {
|
||||||
return html`${getWeatherStateSVG(state)}`;
|
return html`${getWeatherStateSVG(state, nightTime)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state in weatherIcons) {
|
if (state in weatherIcons) {
|
||||||
|
@ -163,6 +163,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
let hourly: boolean | undefined;
|
let hourly: boolean | undefined;
|
||||||
|
let dayNight: boolean | undefined;
|
||||||
|
|
||||||
if (forecast?.length && forecast?.length > 2) {
|
if (forecast?.length && forecast?.length > 2) {
|
||||||
const date1 = new Date(forecast[1].datetime);
|
const date1 = new Date(forecast[1].datetime);
|
||||||
@ -170,6 +171,14 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
const timeDiff = date2.getTime() - date1.getTime();
|
const timeDiff = date2.getTime() - date1.getTime();
|
||||||
|
|
||||||
hourly = timeDiff < DAY_IN_MILLISECONDS;
|
hourly = timeDiff < DAY_IN_MILLISECONDS;
|
||||||
|
|
||||||
|
if (hourly) {
|
||||||
|
const dateFirst = new Date(forecast[0].datetime);
|
||||||
|
const datelast = new Date(forecast[forecast.length - 1].datetime);
|
||||||
|
const dayDiff = datelast.getTime() - dateFirst.getTime();
|
||||||
|
|
||||||
|
dayNight = dayDiff > DAY_IN_MILLISECONDS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const weatherStateIcon = getWeatherStateIcon(stateObj.state, this);
|
const weatherStateIcon = getWeatherStateIcon(stateObj.state, this);
|
||||||
@ -235,7 +244,21 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
(item) => html`
|
(item) => html`
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
${hourly
|
${dayNight
|
||||||
|
? html`
|
||||||
|
${new Date(item.datetime).toLocaleDateString(
|
||||||
|
this.hass!.language,
|
||||||
|
{ weekday: "short" }
|
||||||
|
)}
|
||||||
|
<div class="daynight">
|
||||||
|
${item.daytime === undefined || item.daytime
|
||||||
|
? this.hass!.localize("ui.card.weather.day")
|
||||||
|
: this.hass!.localize(
|
||||||
|
"ui.card.weather.night"
|
||||||
|
)}<br />
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
: hourly
|
||||||
? html`
|
? html`
|
||||||
${new Date(item.datetime).toLocaleTimeString(
|
${new Date(item.datetime).toLocaleTimeString(
|
||||||
this.hass!.language,
|
this.hass!.language,
|
||||||
@ -254,7 +277,11 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
${item.condition !== undefined && item.condition !== null
|
${item.condition !== undefined && item.condition !== null
|
||||||
? html`
|
? html`
|
||||||
<div class="forecast-image-icon">
|
<div class="forecast-image-icon">
|
||||||
${getWeatherStateIcon(item.condition, this)}
|
${getWeatherStateIcon(
|
||||||
|
item.condition,
|
||||||
|
this,
|
||||||
|
!(item.daytime || item.daytime === undefined)
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
@ -445,6 +472,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
.attribute,
|
.attribute,
|
||||||
.templow,
|
.templow,
|
||||||
|
.daynight,
|
||||||
.name {
|
.name {
|
||||||
color: var(--secondary-text-color);
|
color: var(--secondary-text-color);
|
||||||
}
|
}
|
||||||
|
@ -254,6 +254,8 @@
|
|||||||
"wnw": "WNW",
|
"wnw": "WNW",
|
||||||
"wsw": "WSW"
|
"wsw": "WSW"
|
||||||
},
|
},
|
||||||
|
"day": "Day",
|
||||||
|
"night": "Night",
|
||||||
"forecast": "Forecast",
|
"forecast": "Forecast",
|
||||||
"high": "High",
|
"high": "High",
|
||||||
"low": "Low"
|
"low": "Low"
|
||||||
|
@ -341,6 +341,7 @@ interface ForecastAttribute {
|
|||||||
precipitation_probability?: number;
|
precipitation_probability?: number;
|
||||||
humidity?: number;
|
humidity?: number;
|
||||||
condition?: string;
|
condition?: string;
|
||||||
|
daytime?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WeatherEntity = HassEntityBase & {
|
export type WeatherEntity = HassEntityBase & {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user