mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 11:16:35 +00:00
Weather card: Add wind speed direction (#7202)
Co-authored-by: Zack Barett <zackbarett@hey.com>
This commit is contained in:
parent
b8a67d530f
commit
26b476ab3c
@ -48,7 +48,7 @@ const snowyStates = new Set<string>(["snowy", "snowy-rainy"]);
|
|||||||
|
|
||||||
const lightningStates = new Set<string>(["lightning", "lightning-rainy"]);
|
const lightningStates = new Set<string>(["lightning", "lightning-rainy"]);
|
||||||
|
|
||||||
export const cardinalDirections = [
|
const cardinalDirections = [
|
||||||
"N",
|
"N",
|
||||||
"NNE",
|
"NNE",
|
||||||
"NE",
|
"NE",
|
||||||
@ -77,13 +77,29 @@ const getWindBearingText = (degree: string): string => {
|
|||||||
return degree;
|
return degree;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getWindBearing = (bearing: string): string => {
|
const getWindBearing = (bearing: string): string => {
|
||||||
if (bearing != null) {
|
if (bearing != null) {
|
||||||
return getWindBearingText(bearing);
|
return getWindBearingText(bearing);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getWind = (
|
||||||
|
hass: HomeAssistant,
|
||||||
|
speed: string,
|
||||||
|
bearing: string
|
||||||
|
): string => {
|
||||||
|
if (bearing !== null) {
|
||||||
|
const cardinalDirection = getWindBearing(bearing);
|
||||||
|
return `${speed} ${getWeatherUnit(hass!, "wind_speed")} (${
|
||||||
|
hass.localize(
|
||||||
|
`ui.card.weather.cardinal_direction.${cardinalDirection.toLowerCase()}`
|
||||||
|
) || cardinalDirection
|
||||||
|
})`;
|
||||||
|
}
|
||||||
|
return `${speed} ${getWeatherUnit(hass!, "wind_speed")}`;
|
||||||
|
};
|
||||||
|
|
||||||
export const getWeatherUnit = (
|
export const getWeatherUnit = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
measure: string
|
measure: string
|
||||||
@ -210,7 +226,7 @@ export const weatherSVGStyles = css`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const getWeatherStateSVG = (
|
const getWeatherStateSVG = (
|
||||||
state: string,
|
state: string,
|
||||||
nightTime?: boolean
|
nightTime?: boolean
|
||||||
): SVGTemplateResult => {
|
): SVGTemplateResult => {
|
||||||
|
@ -11,6 +11,8 @@ import {
|
|||||||
import { html, TemplateResult } from "lit-html";
|
import { html, TemplateResult } from "lit-html";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
|
|
||||||
|
import { getWind, getWeatherUnit } from "../../../data/weather";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
mdiAlertCircleOutline,
|
mdiAlertCircleOutline,
|
||||||
mdiEye,
|
mdiEye,
|
||||||
@ -33,26 +35,6 @@ import {
|
|||||||
mdiWeatherWindyVariant,
|
mdiWeatherWindyVariant,
|
||||||
} from "@mdi/js";
|
} from "@mdi/js";
|
||||||
|
|
||||||
const cardinalDirections = [
|
|
||||||
"N",
|
|
||||||
"NNE",
|
|
||||||
"NE",
|
|
||||||
"ENE",
|
|
||||||
"E",
|
|
||||||
"ESE",
|
|
||||||
"SE",
|
|
||||||
"SSE",
|
|
||||||
"S",
|
|
||||||
"SSW",
|
|
||||||
"SW",
|
|
||||||
"WSW",
|
|
||||||
"W",
|
|
||||||
"WNW",
|
|
||||||
"NW",
|
|
||||||
"NNW",
|
|
||||||
"N",
|
|
||||||
];
|
|
||||||
|
|
||||||
const weatherIcons = {
|
const weatherIcons = {
|
||||||
"clear-night": mdiWeatherNight,
|
"clear-night": mdiWeatherNight,
|
||||||
cloudy: mdiWeatherCloudy,
|
cloudy: mdiWeatherCloudy,
|
||||||
@ -106,7 +88,8 @@ class MoreInfoWeather extends LitElement {
|
|||||||
${this.hass.localize("ui.card.weather.attributes.temperature")}
|
${this.hass.localize("ui.card.weather.attributes.temperature")}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
${this.stateObj.attributes.temperature} ${this.getUnit("temperature")}
|
${this.stateObj.attributes.temperature}
|
||||||
|
${getWeatherUnit(this.hass, "temperature")}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
${this._showValue(this.stateObj.attributes.pressure)
|
${this._showValue(this.stateObj.attributes.pressure)
|
||||||
@ -118,7 +101,7 @@ class MoreInfoWeather extends LitElement {
|
|||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
${this.stateObj.attributes.pressure}
|
${this.stateObj.attributes.pressure}
|
||||||
${this.getUnit("air_pressure")}
|
${getWeatherUnit(this.hass, "air_pressure")}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
@ -142,7 +125,8 @@ class MoreInfoWeather extends LitElement {
|
|||||||
${this.hass.localize("ui.card.weather.attributes.wind_speed")}
|
${this.hass.localize("ui.card.weather.attributes.wind_speed")}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
${this.getWind(
|
${getWind(
|
||||||
|
this.hass,
|
||||||
this.stateObj.attributes.wind_speed,
|
this.stateObj.attributes.wind_speed,
|
||||||
this.stateObj.attributes.wind_bearing
|
this.stateObj.attributes.wind_bearing
|
||||||
)}
|
)}
|
||||||
@ -158,7 +142,8 @@ class MoreInfoWeather extends LitElement {
|
|||||||
${this.hass.localize("ui.card.weather.attributes.visibility")}
|
${this.hass.localize("ui.card.weather.attributes.visibility")}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
${this.stateObj.attributes.visibility} ${this.getUnit("length")}
|
${this.stateObj.attributes.visibility}
|
||||||
|
${getWeatherUnit(this.hass, "length")}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
@ -191,12 +176,14 @@ class MoreInfoWeather extends LitElement {
|
|||||||
${this.computeDate(item.datetime)}
|
${this.computeDate(item.datetime)}
|
||||||
</div>
|
</div>
|
||||||
<div class="templow">
|
<div class="templow">
|
||||||
${item.templow} ${this.getUnit("temperature")}
|
${item.templow}
|
||||||
|
${getWeatherUnit(this.hass, "temperature")}
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
<div class="temp">
|
<div class="temp">
|
||||||
${item.temperature} ${this.getUnit("temperature")}
|
${item.temperature}
|
||||||
|
${getWeatherUnit(this.hass, "temperature")}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
@ -269,41 +256,6 @@ class MoreInfoWeather extends LitElement {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private getUnit(measure: string): string {
|
|
||||||
const lengthUnit = this.hass.config.unit_system.length || "";
|
|
||||||
switch (measure) {
|
|
||||||
case "air_pressure":
|
|
||||||
return lengthUnit === "km" ? "hPa" : "inHg";
|
|
||||||
case "length":
|
|
||||||
return lengthUnit;
|
|
||||||
case "precipitation":
|
|
||||||
return lengthUnit === "km" ? "mm" : "in";
|
|
||||||
default:
|
|
||||||
return this.hass.config.unit_system[measure] || "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private windBearingToText(degree: string): string {
|
|
||||||
const degreenum = parseInt(degree, 10);
|
|
||||||
if (isFinite(degreenum)) {
|
|
||||||
// eslint-disable-next-line no-bitwise
|
|
||||||
return cardinalDirections[(((degreenum + 11.25) / 22.5) | 0) % 16];
|
|
||||||
}
|
|
||||||
return degree;
|
|
||||||
}
|
|
||||||
|
|
||||||
private getWind(speed: string, bearing: string) {
|
|
||||||
if (bearing != null) {
|
|
||||||
const cardinalDirection = this.windBearingToText(bearing);
|
|
||||||
return `${speed} ${this.getUnit("length")}/h (${
|
|
||||||
this.hass.localize(
|
|
||||||
`ui.card.weather.cardinal_direction.${cardinalDirection.toLowerCase()}`
|
|
||||||
) || cardinalDirection
|
|
||||||
})`;
|
|
||||||
}
|
|
||||||
return `${speed} ${this.getUnit("length")}/h`;
|
|
||||||
}
|
|
||||||
|
|
||||||
private _showValue(item: string): boolean {
|
private _showValue(item: string): boolean {
|
||||||
return typeof item !== "undefined" && item !== null;
|
return typeof item !== "undefined" && item !== null;
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import {
|
|||||||
getSecondaryWeatherAttribute,
|
getSecondaryWeatherAttribute,
|
||||||
getWeatherUnit,
|
getWeatherUnit,
|
||||||
getWeatherStateIcon,
|
getWeatherStateIcon,
|
||||||
|
getWind,
|
||||||
weatherSVGStyles,
|
weatherSVGStyles,
|
||||||
} from "../../../data/weather";
|
} from "../../../data/weather";
|
||||||
import type { HomeAssistant, WeatherEntity } from "../../../types";
|
import type { HomeAssistant, WeatherEntity } from "../../../types";
|
||||||
@ -224,6 +225,13 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
${this.hass!.localize(
|
${this.hass!.localize(
|
||||||
`ui.card.weather.attributes.${this._config.secondary_info_attribute}`
|
`ui.card.weather.attributes.${this._config.secondary_info_attribute}`
|
||||||
)}
|
)}
|
||||||
|
${this._config.secondary_info_attribute === "wind_speed"
|
||||||
|
? getWind(
|
||||||
|
this.hass,
|
||||||
|
stateObj.attributes.wind_speed,
|
||||||
|
stateObj.attributes.wind_bearing
|
||||||
|
)
|
||||||
|
: html`
|
||||||
${stateObj.attributes[
|
${stateObj.attributes[
|
||||||
this._config.secondary_info_attribute
|
this._config.secondary_info_attribute
|
||||||
]}
|
]}
|
||||||
@ -231,6 +239,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
|
|||||||
this.hass,
|
this.hass,
|
||||||
this._config.secondary_info_attribute
|
this._config.secondary_info_attribute
|
||||||
)}
|
)}
|
||||||
|
`}
|
||||||
`
|
`
|
||||||
: getSecondaryWeatherAttribute(this.hass, stateObj)}
|
: getSecondaryWeatherAttribute(this.hass, stateObj)}
|
||||||
</div>
|
</div>
|
||||||
|
@ -360,5 +360,7 @@ export type WeatherEntity = HassEntityBase & {
|
|||||||
temperature: number;
|
temperature: number;
|
||||||
humidity?: number;
|
humidity?: number;
|
||||||
forecast?: ForecastAttribute[];
|
forecast?: ForecastAttribute[];
|
||||||
|
wind_speed: string;
|
||||||
|
wind_bearing: string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user