Fixes moon badge icon (#15015)

This commit is contained in:
Paul Bottein 2023-01-10 18:28:07 +01:00
parent caa852559f
commit 1d15f81b6c
No known key found for this signature in database

View File

@ -22,6 +22,7 @@ import {
isNumericState, isNumericState,
} from "../../common/number/format_number"; } from "../../common/number/format_number";
import { isUnavailableState, UNAVAILABLE, UNKNOWN } from "../../data/entity"; import { isUnavailableState, UNAVAILABLE, UNKNOWN } from "../../data/entity";
import { EntityRegistryEntry } from "../../data/entity_registry";
import { timerTimeRemaining } from "../../data/timer"; import { timerTimeRemaining } from "../../data/timer";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "../ha-label-badge"; import "../ha-label-badge";
@ -103,8 +104,10 @@ export class HaStateLabelBadge extends LitElement {
// 4. Icon determined via entity state // 4. Icon determined via entity state
// 5. Value string as fallback // 5. Value string as fallback
const domain = computeStateDomain(entityState); const domain = computeStateDomain(entityState);
const entry = this.hass?.entities[entityState.entity_id];
const showIcon = this.icon || this._computeShowIcon(domain, entityState); const showIcon =
this.icon || this._computeShowIcon(domain, entityState, entry);
const image = this.icon const image = this.icon
? "" ? ""
: this.image : this.image
@ -112,7 +115,9 @@ export class HaStateLabelBadge extends LitElement {
: entityState.attributes.entity_picture_local || : entityState.attributes.entity_picture_local ||
entityState.attributes.entity_picture; entityState.attributes.entity_picture;
const value = const value =
!image && !showIcon ? this._computeValue(domain, entityState) : undefined; !image && !showIcon
? this._computeValue(domain, entityState, entry)
: undefined;
return html` return html`
<ha-label-badge <ha-label-badge
@ -152,7 +157,11 @@ export class HaStateLabelBadge extends LitElement {
} }
} }
private _computeValue(domain: string, entityState: HassEntity) { private _computeValue(
domain: string,
entityState: HassEntity,
entry?: EntityRegistryEntry
) {
switch (domain) { switch (domain) {
case "alarm_control_panel": case "alarm_control_panel":
case "binary_sensor": case "binary_sensor":
@ -165,7 +174,7 @@ export class HaStateLabelBadge extends LitElement {
return null; return null;
// @ts-expect-error we don't break and go to default // @ts-expect-error we don't break and go to default
case "sensor": case "sensor":
if (entityState.attributes.device_class === "moon__phase") { if (entry?.platform === "moon") {
return null; return null;
} }
// eslint-disable-next-line: disable=no-fallthrough // eslint-disable-next-line: disable=no-fallthrough
@ -188,7 +197,11 @@ export class HaStateLabelBadge extends LitElement {
} }
} }
private _computeShowIcon(domain: string, entityState: HassEntity): boolean { private _computeShowIcon(
domain: string,
entityState: HassEntity,
entry?: EntityRegistryEntry
): boolean {
if (entityState.state === UNAVAILABLE) { if (entityState.state === UNAVAILABLE) {
return false; return false;
} }
@ -204,7 +217,7 @@ export class HaStateLabelBadge extends LitElement {
case "timer": case "timer":
return true; return true;
case "sensor": case "sensor":
return entityState.attributes.device_class === "moon__phase"; return entry?.platform === "moon";
default: default:
return false; return false;
} }