mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Don't show a battery if the entity domain is Number (#17631)
This commit is contained in:
parent
2dc08d782f
commit
b4975344a1
@ -67,7 +67,7 @@ export const DOMAIN_ATTRIBUTES_UNITS: Record<string, Record<string, string>> = {
|
|||||||
sun: {
|
sun: {
|
||||||
elevation: "°",
|
elevation: "°",
|
||||||
},
|
},
|
||||||
vaccum: {
|
vacuum: {
|
||||||
battery_level: "%",
|
battery_level: "%",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -6,6 +6,7 @@ import { caseInsensitiveStringCompare } from "../common/string/compare";
|
|||||||
import { debounce } from "../common/util/debounce";
|
import { debounce } from "../common/util/debounce";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
import { LightColor } from "./light";
|
import { LightColor } from "./light";
|
||||||
|
import { computeDomain } from "../common/entity/compute_domain";
|
||||||
|
|
||||||
type entityCategory = "config" | "diagnostic";
|
type entityCategory = "config" | "diagnostic";
|
||||||
|
|
||||||
@ -129,15 +130,29 @@ export interface EntityRegistryEntryUpdateParams {
|
|||||||
aliases?: string[];
|
aliases?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const batteryPriorities = ["sensor", "binary_sensor"];
|
||||||
export const findBatteryEntity = <T extends { entity_id: string }>(
|
export const findBatteryEntity = <T extends { entity_id: string }>(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entities: T[]
|
entities: T[]
|
||||||
): T | undefined =>
|
): T | undefined => {
|
||||||
entities.find(
|
const batteryEntities = entities
|
||||||
(entity) =>
|
.filter(
|
||||||
hass.states[entity.entity_id] &&
|
(entity) =>
|
||||||
hass.states[entity.entity_id].attributes.device_class === "battery"
|
hass.states[entity.entity_id] &&
|
||||||
);
|
hass.states[entity.entity_id].attributes.device_class === "battery" &&
|
||||||
|
batteryPriorities.includes(computeDomain(entity.entity_id))
|
||||||
|
)
|
||||||
|
.sort(
|
||||||
|
(a, b) =>
|
||||||
|
batteryPriorities.indexOf(computeDomain(a.entity_id)) -
|
||||||
|
batteryPriorities.indexOf(computeDomain(b.entity_id))
|
||||||
|
);
|
||||||
|
if (batteryEntities.length > 0) {
|
||||||
|
return batteryEntities[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export const findBatteryChargingEntity = <T extends { entity_id: string }>(
|
export const findBatteryChargingEntity = <T extends { entity_id: string }>(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -262,12 +262,13 @@ class MoreInfoVacuum extends LitElement {
|
|||||||
const battery = batteryEntity
|
const battery = batteryEntity
|
||||||
? this.hass.states[batteryEntity.entity_id]
|
? this.hass.states[batteryEntity.entity_id]
|
||||||
: undefined;
|
: undefined;
|
||||||
|
const batteryDomain = battery ? computeStateDomain(battery) : undefined;
|
||||||
const batteryIsBinary =
|
|
||||||
battery && computeStateDomain(battery) === "binary_sensor";
|
|
||||||
|
|
||||||
// Use device battery entity
|
// Use device battery entity
|
||||||
if (battery && (batteryIsBinary || !isNaN(battery.state as any))) {
|
if (
|
||||||
|
battery &&
|
||||||
|
(batteryDomain === "binary_sensor" || !isNaN(battery.state as any))
|
||||||
|
) {
|
||||||
const batteryChargingEntity = findBatteryChargingEntity(
|
const batteryChargingEntity = findBatteryChargingEntity(
|
||||||
this.hass,
|
this.hass,
|
||||||
entities
|
entities
|
||||||
@ -279,7 +280,9 @@ class MoreInfoVacuum extends LitElement {
|
|||||||
return html`
|
return html`
|
||||||
<div>
|
<div>
|
||||||
<span>
|
<span>
|
||||||
${batteryIsBinary ? "" : this.hass.formatEntityState(battery)}
|
${batteryDomain === "sensor"
|
||||||
|
? this.hass.formatEntityState(battery)
|
||||||
|
: nothing}
|
||||||
<ha-battery-icon
|
<ha-battery-icon
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.batteryStateObj=${battery}
|
.batteryStateObj=${battery}
|
||||||
|
@ -28,7 +28,6 @@ import { computeStateDomain } from "../../../common/entity/compute_state_domain"
|
|||||||
import { computeStateName } from "../../../common/entity/compute_state_name";
|
import { computeStateName } from "../../../common/entity/compute_state_name";
|
||||||
import { stringCompare } from "../../../common/string/compare";
|
import { stringCompare } from "../../../common/string/compare";
|
||||||
import { slugify } from "../../../common/string/slugify";
|
import { slugify } from "../../../common/string/slugify";
|
||||||
import { blankBeforePercent } from "../../../common/translations/blank_before_percent";
|
|
||||||
import { groupBy } from "../../../common/util/group-by";
|
import { groupBy } from "../../../common/util/group-by";
|
||||||
import "../../../components/entity/ha-battery-icon";
|
import "../../../components/entity/ha-battery-icon";
|
||||||
import "../../../components/ha-alert";
|
import "../../../components/ha-alert";
|
||||||
@ -329,11 +328,11 @@ export class HaConfigDevicePage extends LitElement {
|
|||||||
const entitiesByCategory = this._entitiesByCategory(entities);
|
const entitiesByCategory = this._entitiesByCategory(entities);
|
||||||
const batteryEntity = this._batteryEntity(entities);
|
const batteryEntity = this._batteryEntity(entities);
|
||||||
const batteryChargingEntity = this._batteryChargingEntity(entities);
|
const batteryChargingEntity = this._batteryChargingEntity(entities);
|
||||||
const batteryState = batteryEntity
|
const battery = batteryEntity
|
||||||
? this.hass.states[batteryEntity.entity_id]
|
? this.hass.states[batteryEntity.entity_id]
|
||||||
: undefined;
|
: undefined;
|
||||||
const batteryIsBinary =
|
const batteryDomain = battery ? computeStateDomain(battery) : undefined;
|
||||||
batteryState && computeStateDomain(batteryState) === "binary_sensor";
|
|
||||||
const batteryChargingState = batteryChargingEntity
|
const batteryChargingState = batteryChargingEntity
|
||||||
? this.hass.states[batteryChargingEntity.entity_id]
|
? this.hass.states[batteryChargingEntity.entity_id]
|
||||||
: undefined;
|
: undefined;
|
||||||
@ -712,17 +711,17 @@ export class HaConfigDevicePage extends LitElement {
|
|||||||
}
|
}
|
||||||
<div class="header-right">
|
<div class="header-right">
|
||||||
${
|
${
|
||||||
batteryState
|
battery &&
|
||||||
|
(batteryDomain === "binary_sensor" ||
|
||||||
|
!isNaN(battery.state as any))
|
||||||
? html`
|
? html`
|
||||||
<div class="battery">
|
<div class="battery">
|
||||||
${batteryIsBinary
|
${batteryDomain === "sensor"
|
||||||
? ""
|
? this.hass.formatEntityState(battery)
|
||||||
: batteryState.state +
|
: nothing}
|
||||||
blankBeforePercent(this.hass.locale) +
|
|
||||||
"%"}
|
|
||||||
<ha-battery-icon
|
<ha-battery-icon
|
||||||
.hass=${this.hass!}
|
.hass=${this.hass}
|
||||||
.batteryStateObj=${batteryState}
|
.batteryStateObj=${battery}
|
||||||
.batteryChargingStateObj=${batteryChargingState}
|
.batteryChargingStateObj=${batteryChargingState}
|
||||||
></ha-battery-icon>
|
></ha-battery-icon>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
import "@lrnwebcomponents/simple-tooltip/simple-tooltip";
|
import "@lrnwebcomponents/simple-tooltip/simple-tooltip";
|
||||||
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
|
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
|
||||||
import { mdiCancel, mdiFilterVariant, mdiPlus } from "@mdi/js";
|
import { mdiCancel, mdiFilterVariant, mdiPlus } from "@mdi/js";
|
||||||
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
|
import {
|
||||||
|
css,
|
||||||
|
CSSResultGroup,
|
||||||
|
html,
|
||||||
|
LitElement,
|
||||||
|
nothing,
|
||||||
|
TemplateResult,
|
||||||
|
} from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import { HASSDomEvent } from "../../../common/dom/fire_event";
|
import { HASSDomEvent } from "../../../common/dom/fire_event";
|
||||||
@ -11,7 +18,6 @@ import {
|
|||||||
PROTOCOL_INTEGRATIONS,
|
PROTOCOL_INTEGRATIONS,
|
||||||
} from "../../../common/integrations/protocolIntegrationPicked";
|
} from "../../../common/integrations/protocolIntegrationPicked";
|
||||||
import { navigate } from "../../../common/navigate";
|
import { navigate } from "../../../common/navigate";
|
||||||
import { blankBeforePercent } from "../../../common/translations/blank_before_percent";
|
|
||||||
import { LocalizeFunc } from "../../../common/translations/localize";
|
import { LocalizeFunc } from "../../../common/translations/localize";
|
||||||
import { computeRTL } from "../../../common/util/compute_rtl";
|
import { computeRTL } from "../../../common/util/compute_rtl";
|
||||||
import {
|
import {
|
||||||
@ -374,22 +380,22 @@ export class HaConfigDeviceDashboard extends LitElement {
|
|||||||
batteryEntityPair && batteryEntityPair[0]
|
batteryEntityPair && batteryEntityPair[0]
|
||||||
? this.hass.states[batteryEntityPair[0]]
|
? this.hass.states[batteryEntityPair[0]]
|
||||||
: undefined;
|
: undefined;
|
||||||
|
const batteryDomain = battery
|
||||||
|
? computeStateDomain(battery)
|
||||||
|
: undefined;
|
||||||
const batteryCharging =
|
const batteryCharging =
|
||||||
batteryEntityPair && batteryEntityPair[1]
|
batteryEntityPair && batteryEntityPair[1]
|
||||||
? this.hass.states[batteryEntityPair[1]]
|
? this.hass.states[batteryEntityPair[1]]
|
||||||
: undefined;
|
: undefined;
|
||||||
const batteryIsBinary =
|
|
||||||
battery && computeStateDomain(battery) === "binary_sensor";
|
|
||||||
|
|
||||||
return battery && (batteryIsBinary || !isNaN(battery.state as any))
|
return battery &&
|
||||||
|
(batteryDomain === "binary_sensor" || !isNaN(battery.state as any))
|
||||||
? html`
|
? html`
|
||||||
${batteryIsBinary
|
${batteryDomain === "sensor"
|
||||||
? ""
|
? this.hass.formatEntityState(battery)
|
||||||
: Number(battery.state).toFixed() +
|
: nothing}
|
||||||
blankBeforePercent(this.hass.locale) +
|
|
||||||
"%"}
|
|
||||||
<ha-battery-icon
|
<ha-battery-icon
|
||||||
.hass=${this.hass!}
|
.hass=${this.hass}
|
||||||
.batteryStateObj=${battery}
|
.batteryStateObj=${battery}
|
||||||
.batteryChargingStateObj=${batteryCharging}
|
.batteryChargingStateObj=${batteryCharging}
|
||||||
></ha-battery-icon>
|
></ha-battery-icon>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user