Weather Row: Generic (#5389)

* Bye lots of hours

* Merge image states into states we have

* add space
This commit is contained in:
Zack Arnett 2020-04-01 10:58:05 -04:00 committed by GitHub
parent 2e17c96866
commit e54d904e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 130 deletions

View File

@ -1,21 +1,24 @@
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
export const weatherIcons = { export const weatherImages = {
"clear-night": "/static/images/weather/night.png", "clear-night": "/static/images/weather/night.png",
cloudy: "/static/images/weather/cloudy.png", cloudy: "/static/images/weather/cloudy.png",
exceptional: "hass:alert-circle-outline", fog: "/static/images/weather/cloudy.png",
fog: "hass:weather-fog", hail: "/static/images/weather/rainy.png",
hail: "hass:weather-hail",
lightning: "/static/images/weather/lightning.png", lightning: "/static/images/weather/lightning.png",
"lightning-rainy": "/static/images/weather/lightning-rainy.png", "lightning-rainy": "/static/images/weather/lightning-rainy.png",
partlycloudy: "/static/images/weather/partly-cloudy.png", partlycloudy: "/static/images/weather/partly-cloudy.png",
pouring: "/static/images/weather/pouring.png", pouring: "/static/images/weather/pouring.png",
rainy: "/static/images/weather/rainy.png", rainy: "/static/images/weather/rainy.png",
snowy: "/static/images/weather/snowy.png", snowy: "/static/images/weather/snowy.png",
"snowy-rainy": "hass:weather-snowy-rainy", "snowy-rainy": "/static/images/weather/rainy.png",
sunny: "/static/images/weather/sunny.png", sunny: "/static/images/weather/sunny.png",
windy: "/static/images/weather/windy.png", windy: "/static/images/weather/windy.png",
"windy-variant": "hass:weather-windy-variant", "windy-variant": "/static/images/weather/windy.png",
};
export const weatherIcons = {
exceptional: "hass:alert-circle-outline",
}; };
export const cardinalDirections = [ export const cardinalDirections = [

View File

@ -8,8 +8,6 @@ import {
customElement, customElement,
PropertyValues, PropertyValues,
} from "lit-element"; } from "lit-element";
import { ifDefined } from "lit-html/directives/if-defined";
import { classMap } from "lit-html/directives/class-map";
import "../../../components/entity/state-badge"; import "../../../components/entity/state-badge";
import "../components/hui-warning"; import "../components/hui-warning";
@ -18,15 +16,11 @@ import { LovelaceRow } from "./types";
import { HomeAssistant, WeatherEntity } from "../../../types"; import { HomeAssistant, WeatherEntity } from "../../../types";
import { EntitiesCardEntityConfig } from "../cards/types"; import { EntitiesCardEntityConfig } from "../cards/types";
import { hasConfigOrEntityChanged } from "../common/has-changed"; import { hasConfigOrEntityChanged } from "../common/has-changed";
import { UNAVAILABLE } from "../../../data/entity"; import {
import { weatherIcons, getWeatherUnit } from "../../../data/weather"; weatherIcons,
import { DOMAINS_HIDE_MORE_INFO } from "../../../common/const"; getWeatherUnit,
import { computeDomain } from "../../../common/entity/compute_domain"; weatherImages,
import { actionHandler } from "../common/directives/action-handler-directive"; } from "../../../data/weather";
import { hasAction } from "../common/has-action";
import { computeStateName } from "../../../common/entity/compute_state_name";
import { ActionHandlerEvent } from "../../../data/lovelace";
import { handleAction } from "../common/handle-action";
@customElement("hui-weather-entity-row") @customElement("hui-weather-entity-row")
class HuiWeatherEntityRow extends LitElement implements LovelaceRow { class HuiWeatherEntityRow extends LitElement implements LovelaceRow {
@ -52,7 +46,7 @@ class HuiWeatherEntityRow extends LitElement implements LovelaceRow {
const stateObj = this.hass.states[this._config.entity] as WeatherEntity; const stateObj = this.hass.states[this._config.entity] as WeatherEntity;
if (!stateObj || stateObj.state === UNAVAILABLE) { if (!stateObj) {
return html` return html`
<hui-warning <hui-warning
>${this.hass.localize( >${this.hass.localize(
@ -64,58 +58,24 @@ class HuiWeatherEntityRow extends LitElement implements LovelaceRow {
`; `;
} }
const pointer = const weatherRowConfig = {
(this._config.tap_action && this._config.tap_action.action !== "none") || ...this._config,
(this._config.entity && icon: weatherIcons[stateObj.state],
!DOMAINS_HIDE_MORE_INFO.includes(computeDomain(this._config.entity))); image: weatherImages[stateObj.state],
};
const secondaryAttribute = this._getSecondaryAttribute(stateObj);
return html` return html`
<div <hui-generic-entity-row .hass=${this.hass} .config=${weatherRowConfig}>
class="main ${classMap({ <div class="attributes">
pointer, <div>
})}" ${stateObj.attributes.temperature}
@action=${this._handleAction} ${getWeatherUnit(this.hass, "temperature")}
.actionHandler=${actionHandler({ </div>
hasHold: hasAction(this._config!.hold_action), <div class="secondary">
hasDoubleClick: hasAction(this._config!.double_tap_action), ${this._getSecondaryAttribute(stateObj)}
})}
>
<state-badge
.hass=${this.hass}
.stateObj=${stateObj}
.overrideImage=${weatherIcons[stateObj.state]}
tabindex=${ifDefined(pointer ? "0" : undefined)}
></state-badge>
<div class="container">
<div style="display: flex;">
<div class="info">
<div class="name">
${this._config.name || computeStateName(stateObj)}
</div>
<div class="state">
${this.hass.localize(`state.weather.${stateObj.state}`) ||
stateObj.state}
</div>
</div>
<div class="temperature">
${stateObj.attributes.temperature}<span
>${getWeatherUnit(this.hass, "temperature")}</span
>
</div>
</div> </div>
${secondaryAttribute
? html`
<div class="attributes">
<span>
${secondaryAttribute}
</span>
</div>
`
: ""}
</div> </div>
</div> </hui-generic-entity-row>
`; `;
} }
@ -146,7 +106,7 @@ class HuiWeatherEntityRow extends LitElement implements LovelaceRow {
return ` return `
${this.hass!.localize( ${this.hass!.localize(
`ui.card.weather.attributes.${attribute}` `ui.card.weather.attributes.${attribute}`
)} ${value}${getWeatherUnit(this.hass!, attribute)} )} ${value} ${getWeatherUnit(this.hass!, attribute)}
`; `;
} }
@ -184,7 +144,7 @@ class HuiWeatherEntityRow extends LitElement implements LovelaceRow {
${ ${
tempHigh tempHigh
? ` ? `
${this.hass!.localize(`ui.card.weather.high`)} ${tempHigh}${unit} ${this.hass!.localize(`ui.card.weather.high`)} ${tempHigh} ${unit}
` `
: "" : ""
} }
@ -192,82 +152,25 @@ class HuiWeatherEntityRow extends LitElement implements LovelaceRow {
${ ${
tempLow tempLow
? ` ? `
${this.hass!.localize(`ui.card.weather.low`)} ${tempLow}${unit} ${this.hass!.localize(`ui.card.weather.low`)} ${tempLow} ${unit}
` `
: "" : ""
} }
`; `;
} }
private _handleAction(ev: ActionHandlerEvent) {
handleAction(this, this.hass!, this._config!, ev.detail.action!);
}
static get styles(): CSSResult { static get styles(): CSSResult {
return css` return css`
.pointer { .attributes {
cursor: pointer;
}
.main {
display: flex; display: flex;
align-items: center; flex-direction: column;
justify-content: flex-end;
width: 100%;
}
.name {
font-weight: 500;
}
.state {
color: var(--secondary-text-color);
}
.temperature {
font-size: 28px;
margin-right: 16px;
}
.temperature span {
font-size: 18px;
position: absolute;
}
.container {
flex: 1 0;
display: flex;
flex-flow: column;
}
.info {
display: flex;
flex-flow: column;
justify-content: center; justify-content: center;
text-align: right;
} }
.info, .secondary {
.attributes {
flex: 1 0;
margin-left: 16px;
overflow: hidden;
}
.info > *,
.attributes > * {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.attributes {
padding-top: 1px;
color: var(--secondary-text-color); color: var(--secondary-text-color);
} }
.attributes > *:not(:first-child) {
padding-left: 4px;
}
`; `;
} }
} }