Skip to content

Commit

Permalink
Refactored items a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
avee87 committed Jul 6, 2022
1 parent 8e39870 commit 8bd33d7
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions dist/weather-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,39 +191,48 @@ class WeatherCard extends LitElement {
const items = [];

if (stateObj.attributes.humidity != null) {
items.push(html`
<ha-icon icon="mdi:water-percent"></ha-icon>
${stateObj.attributes.humidity}<span class="unit"> % </span>
`);
items.push(
this.renderItem(
"mdi:water-percent",
stateObj.attributes.humidity,
this.getUnit("humidity"),
),
);
}

if (stateObj.attributes.wind_speed != null) {
const windBearing = stateObj.attributes.wind_bearing;
const windDirection = windBearing != null
? this.getWindDirection(windBearing)
: "";

items.push(html`
<ha-icon icon="mdi:weather-windy"></ha-icon>
${windBearing != null
? this.getWindDirection(windBearing)
: ""}
${stateObj.attributes.wind_speed}<span class="unit">
${this.getUnit("wind_speed")}
</span>
`);
items.push(
this.renderItem(
"mdi:weather-windy",
windDirection + " " + stateObj.attributes.wind_speed,
this.getUnit("wind_speed"),
),
);
}

if (stateObj.attributes.pressure != null) {
items.push(html`
<ha-icon icon="mdi:gauge"></ha-icon>
${stateObj.attributes.pressure}
<span class="unit"> ${this.getUnit("air_pressure")} </span>
`);
items.push(
this.renderItem(
"mdi:gauge",
stateObj.attributes.pressure,
this.getUnit("air_pressure"),
),
);
}

if (stateObj.attributes.visibility != null) {
items.push(html`
<ha-icon icon="mdi:weather-fog"></ha-icon> ${stateObj.attributes
.visibility}<span class="unit"> ${this.getUnit("visibility")} </span>
`);
items.push(
this.renderItem(
"mdi:weather-fog",
stateObj.attributes.visibility,
this.getUnit("visibility"),
),
);
}

const sun = this.hass.states['sun.sun'];
Expand All @@ -241,14 +250,8 @@ class WeatherCard extends LitElement {
items.push(html`<div />`);
}

items.push(html`
<ha-icon icon="mdi:weather-sunset-up"></ha-icon>
${next_rising}
`);
items.push(html`
<ha-icon icon="mdi:weather-sunset-down"></ha-icon>
${next_setting}
`);
items.push(this.renderItem("mdi:weather-sunset-up", next_rising));
items.push(this.renderItem("mdi:weather-sunset-down", next_setting));
}

const listItems = items.map(item => html`<li>${item}</li>`);
Expand All @@ -260,6 +263,14 @@ class WeatherCard extends LitElement {
`;
}

renderItem(icon, value, unit) {
return html`
<ha-icon icon="${icon}"></ha-icon>
${value}
${unit != null ? html`<span class="unit"> ${unit} </span>` : ""}
`;
}

renderForecast(forecast, lang) {
if (!forecast || forecast.length === 0) {
return html``;
Expand Down Expand Up @@ -356,6 +367,8 @@ class WeatherCard extends LitElement {
switch (measure) {
case "air_pressure":
return attributes.pressure_unit ?? (lengthUnit === "km" ? "hPa" : "inHg");
case "humidity":
return "%";
case "precipitation":
return attributes.precipitation_unit ?? (lengthUnit === "km" ? "mm" : "in");
case "precipitation_probability":
Expand Down

0 comments on commit 8bd33d7

Please sign in to comment.