mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-24 09:46:36 +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: {
|
||||
elevation: "°",
|
||||
},
|
||||
vaccum: {
|
||||
vacuum: {
|
||||
battery_level: "%",
|
||||
},
|
||||
};
|
||||
|
@ -6,6 +6,7 @@ import { caseInsensitiveStringCompare } from "../common/string/compare";
|
||||
import { debounce } from "../common/util/debounce";
|
||||
import { HomeAssistant } from "../types";
|
||||
import { LightColor } from "./light";
|
||||
import { computeDomain } from "../common/entity/compute_domain";
|
||||
|
||||
type entityCategory = "config" | "diagnostic";
|
||||
|
||||
@ -129,15 +130,29 @@ export interface EntityRegistryEntryUpdateParams {
|
||||
aliases?: string[];
|
||||
}
|
||||
|
||||
const batteryPriorities = ["sensor", "binary_sensor"];
|
||||
export const findBatteryEntity = <T extends { entity_id: string }>(
|
||||
hass: HomeAssistant,
|
||||
entities: T[]
|
||||
): T | undefined =>
|
||||
entities.find(
|
||||
(entity) =>
|
||||
hass.states[entity.entity_id] &&
|
||||
hass.states[entity.entity_id].attributes.device_class === "battery"
|
||||
);
|
||||
): T | undefined => {
|
||||
const batteryEntities = entities
|
||||
.filter(
|
||||
(entity) =>
|
||||
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 }>(
|
||||
hass: HomeAssistant,
|
||||
|
@ -262,12 +262,13 @@ class MoreInfoVacuum extends LitElement {
|
||||
const battery = batteryEntity
|
||||
? this.hass.states[batteryEntity.entity_id]
|
||||
: undefined;
|
||||
|
||||
const batteryIsBinary =
|
||||
battery && computeStateDomain(battery) === "binary_sensor";
|
||||
const batteryDomain = battery ? computeStateDomain(battery) : undefined;
|
||||
|
||||
// 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(
|
||||
this.hass,
|
||||
entities
|
||||
@ -279,7 +280,9 @@ class MoreInfoVacuum extends LitElement {
|
||||
return html`
|
||||
<div>
|
||||
<span>
|
||||
${batteryIsBinary ? "" : this.hass.formatEntityState(battery)}
|
||||
${batteryDomain === "sensor"
|
||||
? this.hass.formatEntityState(battery)
|
||||
: nothing}
|
||||
<ha-battery-icon
|
||||
.hass=${this.hass}
|
||||
.batteryStateObj=${battery}
|
||||
|
@ -28,7 +28,6 @@ import { computeStateDomain } from "../../../common/entity/compute_state_domain"
|
||||
import { computeStateName } from "../../../common/entity/compute_state_name";
|
||||
import { stringCompare } from "../../../common/string/compare";
|
||||
import { slugify } from "../../../common/string/slugify";
|
||||
import { blankBeforePercent } from "../../../common/translations/blank_before_percent";
|
||||
import { groupBy } from "../../../common/util/group-by";
|
||||
import "../../../components/entity/ha-battery-icon";
|
||||
import "../../../components/ha-alert";
|
||||
@ -329,11 +328,11 @@ export class HaConfigDevicePage extends LitElement {
|
||||
const entitiesByCategory = this._entitiesByCategory(entities);
|
||||
const batteryEntity = this._batteryEntity(entities);
|
||||
const batteryChargingEntity = this._batteryChargingEntity(entities);
|
||||
const batteryState = batteryEntity
|
||||
const battery = batteryEntity
|
||||
? this.hass.states[batteryEntity.entity_id]
|
||||
: undefined;
|
||||
const batteryIsBinary =
|
||||
batteryState && computeStateDomain(batteryState) === "binary_sensor";
|
||||
const batteryDomain = battery ? computeStateDomain(battery) : undefined;
|
||||
|
||||
const batteryChargingState = batteryChargingEntity
|
||||
? this.hass.states[batteryChargingEntity.entity_id]
|
||||
: undefined;
|
||||
@ -712,17 +711,17 @@ export class HaConfigDevicePage extends LitElement {
|
||||
}
|
||||
<div class="header-right">
|
||||
${
|
||||
batteryState
|
||||
battery &&
|
||||
(batteryDomain === "binary_sensor" ||
|
||||
!isNaN(battery.state as any))
|
||||
? html`
|
||||
<div class="battery">
|
||||
${batteryIsBinary
|
||||
? ""
|
||||
: batteryState.state +
|
||||
blankBeforePercent(this.hass.locale) +
|
||||
"%"}
|
||||
${batteryDomain === "sensor"
|
||||
? this.hass.formatEntityState(battery)
|
||||
: nothing}
|
||||
<ha-battery-icon
|
||||
.hass=${this.hass!}
|
||||
.batteryStateObj=${batteryState}
|
||||
.hass=${this.hass}
|
||||
.batteryStateObj=${battery}
|
||||
.batteryChargingStateObj=${batteryChargingState}
|
||||
></ha-battery-icon>
|
||||
</div>
|
||||
|
@ -1,7 +1,14 @@
|
||||
import "@lrnwebcomponents/simple-tooltip/simple-tooltip";
|
||||
import type { RequestSelectedDetail } from "@material/mwc-list/mwc-list-item";
|
||||
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 memoizeOne from "memoize-one";
|
||||
import { HASSDomEvent } from "../../../common/dom/fire_event";
|
||||
@ -11,7 +18,6 @@ import {
|
||||
PROTOCOL_INTEGRATIONS,
|
||||
} from "../../../common/integrations/protocolIntegrationPicked";
|
||||
import { navigate } from "../../../common/navigate";
|
||||
import { blankBeforePercent } from "../../../common/translations/blank_before_percent";
|
||||
import { LocalizeFunc } from "../../../common/translations/localize";
|
||||
import { computeRTL } from "../../../common/util/compute_rtl";
|
||||
import {
|
||||
@ -374,22 +380,22 @@ export class HaConfigDeviceDashboard extends LitElement {
|
||||
batteryEntityPair && batteryEntityPair[0]
|
||||
? this.hass.states[batteryEntityPair[0]]
|
||||
: undefined;
|
||||
const batteryDomain = battery
|
||||
? computeStateDomain(battery)
|
||||
: undefined;
|
||||
const batteryCharging =
|
||||
batteryEntityPair && batteryEntityPair[1]
|
||||
? this.hass.states[batteryEntityPair[1]]
|
||||
: 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`
|
||||
${batteryIsBinary
|
||||
? ""
|
||||
: Number(battery.state).toFixed() +
|
||||
blankBeforePercent(this.hass.locale) +
|
||||
"%"}
|
||||
${batteryDomain === "sensor"
|
||||
? this.hass.formatEntityState(battery)
|
||||
: nothing}
|
||||
<ha-battery-icon
|
||||
.hass=${this.hass!}
|
||||
.hass=${this.hass}
|
||||
.batteryStateObj=${battery}
|
||||
.batteryChargingStateObj=${batteryCharging}
|
||||
></ha-battery-icon>
|
||||
|
Loading…
x
Reference in New Issue
Block a user