Skip to content

Commit

Permalink
Use state display and attribute display
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Nov 22, 2024
1 parent 4737bae commit d17469a
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 378 deletions.
11 changes: 1 addition & 10 deletions src/cards/chips-card/chips/alarm-control-panel-chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -82,15 +81,7 @@ export class AlarmControlPanelChip extends LitElement implements LovelaceChip {
const iconColor = getStateColor(stateObj.state);
const iconPulse = shouldPulse(stateObj.state);

const stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
const stateDisplay = this.hass.formatEntityState(stateObj);

const iconStyle = {};
if (iconColor) {
Expand Down
13 changes: 2 additions & 11 deletions src/cards/chips-card/chips/entity-chip.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HassEntity } from "home-assistant-js-websocket";
import {
css,
CSSResultGroup,
Expand All @@ -13,7 +14,6 @@ import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
getEntityPicture,
handleAction,
hasAction,
Expand All @@ -31,7 +31,6 @@ import {
LovelaceChip,
} from "../../../utils/lovelace/chip/types";
import { LovelaceChipEditor } from "../../../utils/lovelace/types";
import { HassEntity } from "home-assistant-js-websocket";

@customElement(computeChipComponentName("entity"))
export class EntityChip extends LitElement implements LovelaceChip {
Expand Down Expand Up @@ -84,15 +83,7 @@ export class EntityChip extends LitElement implements LovelaceChip {
? getEntityPicture(stateObj)
: undefined;

const stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
const stateDisplay = this.hass.formatEntityState(stateObj);

const active = isActive(stateObj);

Expand Down
11 changes: 1 addition & 10 deletions src/cards/chips-card/chips/light-chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -80,15 +79,7 @@ export class LightChip extends LitElement implements LovelaceChip {
const name = this._config.name || stateObj.attributes.friendly_name || "";
const icon = this._config.icon;

const stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
const stateDisplay = this.hass.formatEntityState(stateObj);

const active = isActive(stateObj);

Expand Down
21 changes: 6 additions & 15 deletions src/cards/chips-card/chips/weather-chip.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HassEntity } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
formatNumber,
handleAction,
hasAction,
Expand All @@ -20,7 +20,6 @@ import {
} from "../../../utils/lovelace/chip/types";
import { LovelaceChipEditor } from "../../../utils/lovelace/types";
import { getWeatherStateSVG, weatherSVGStyles } from "../../../utils/weather";
import { HassEntity } from "home-assistant-js-websocket";

@customElement(computeChipComponentName("weather"))
export class WeatherChip extends LitElement implements LovelaceChip {
Expand Down Expand Up @@ -71,23 +70,15 @@ export class WeatherChip extends LitElement implements LovelaceChip {
const displayLabels: string[] = [];

if (this._config.show_conditions) {
const stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
const stateDisplay = this.hass.formatEntityState(stateObj);
displayLabels.push(stateDisplay);
}

if (this._config.show_temperature) {
const temperatureDisplay = `${formatNumber(
stateObj.attributes.temperature,
this.hass.locale
)} ${this.hass.config.unit_system.temperature}`;
const temperatureDisplay = this.hass.formatEntityAttributeValue(
stateObj,
"temperature"
);
displayLabels.push(temperatureDisplay);
}

Expand Down
20 changes: 5 additions & 15 deletions src/cards/climate-card/climate-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ActionHandlerEvent,
ClimateEntity,
computeRTL,
computeStateDisplay,
formatNumber,
handleAction,
hasAction,
Expand Down Expand Up @@ -167,22 +166,13 @@ export class ClimateCard
const appearance = computeAppearance(this._config);
const picture = computeEntityPicture(stateObj, appearance.icon_type);

let stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
let stateDisplay = this.hass.formatEntityState(stateObj);
if (stateObj.attributes.current_temperature !== null) {
const temperature = formatNumber(
stateObj.attributes.current_temperature,
this.hass.locale
const temperature = this.hass.formatEntityAttributeValue(
stateObj,
"current_temperature"
);
const unit = this.hass.config.unit_system.temperature;
stateDisplay += ` - ${temperature} ${unit}`;
stateDisplay += ` - ${temperature}`;
}
const rtl = computeRTL(this.hass);

Expand Down
11 changes: 1 addition & 10 deletions src/cards/cover-card/cover-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ActionHandlerEvent,
blankBeforePercent,
computeRTL,
computeStateDisplay,
CoverEntity,
handleAction,
hasAction,
Expand Down Expand Up @@ -195,15 +194,7 @@ export class CoverCard
const appearance = computeAppearance(this._config);
const picture = computeEntityPicture(stateObj, appearance.icon_type);

let stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
let stateDisplay = this.hass.formatEntityState(stateObj);
if (this.position) {
stateDisplay += ` - ${this.position}${blankBeforePercent(this.hass.locale)}%`;
}
Expand Down
11 changes: 1 addition & 10 deletions src/cards/fan-card/fan-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ActionHandlerEvent,
blankBeforePercent,
computeRTL,
computeStateDisplay,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -137,15 +136,7 @@ export class FanCard
const appearance = computeAppearance(this._config);
const picture = computeEntityPicture(stateObj, appearance.icon_type);

let stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
let stateDisplay = this.hass.formatEntityState(stateObj);
if (this.percentage != null && stateObj.state === "on") {
stateDisplay = `${this.percentage}${blankBeforePercent(this.hass.locale)}%`;
}
Expand Down
11 changes: 1 addition & 10 deletions src/cards/humidifier-card/humidifier-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ActionHandlerEvent,
blankBeforePercent,
computeRTL,
computeStateDisplay,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -110,15 +109,7 @@ export class HumidifierCard
const appearance = computeAppearance(this._config);
const picture = computeEntityPicture(stateObj, appearance.icon_type);

let stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
let stateDisplay = this.hass.formatEntityState(stateObj);
if (this.humidity) {
stateDisplay = `${this.humidity}${blankBeforePercent(this.hass.locale)}%`;
}
Expand Down
11 changes: 1 addition & 10 deletions src/cards/light-card/light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ActionHandlerEvent,
blankBeforePercent,
computeRTL,
computeStateDisplay,
handleAction,
hasAction,
HomeAssistant,
Expand Down Expand Up @@ -196,15 +195,7 @@ export class LightCard
const appearance = computeAppearance(this._config);
const picture = computeEntityPicture(stateObj, appearance.icon_type);

let stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
let stateDisplay = this.hass.formatEntityState(stateObj);
if (this.brightness != null) {
stateDisplay = `${this.brightness}${blankBeforePercent(this.hass.locale)}%`;
}
Expand Down
11 changes: 1 addition & 10 deletions src/cards/media-player-card/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
UNAVAILABLE,
UNKNOWN,
computeMediaDescription,
computeStateDisplay,
supportsFeature,
} from "../../ha";
import {
Expand Down Expand Up @@ -56,15 +55,7 @@ export function computeMediaStateDisplay(
entity: MediaPlayerEntity,
hass: HomeAssistant
): string {
let state = hass.formatEntityState
? hass.formatEntityState(entity)
: computeStateDisplay(
hass.localize,
entity,
hass.locale,
hass.config,
hass.entities
);
let state = hass.formatEntityState(entity);
if (
![UNAVAILABLE, UNKNOWN, OFF].includes(entity.state) &&
config.use_media_info
Expand Down
11 changes: 1 addition & 10 deletions src/cards/number-card/number-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
formatNumber,
getDefaultFormatOptions,
getNumberFormatOptions,
Expand Down Expand Up @@ -124,15 +123,7 @@ export class NumberCard
const appearance = computeAppearance(this._config);
const picture = computeEntityPicture(stateObj, appearance.icon_type);

let stateDisplay = this.hass.formatEntityState
? this.hass.formatEntityState(stateObj)
: computeStateDisplay(
this.hass.localize,
stateObj,
this.hass.locale,
this.hass.config,
this.hass.entities
);
let stateDisplay = this.hass.formatEntityState(stateObj);
if (this.value !== undefined) {
const numberValue = formatNumber(
this.value,
Expand Down
13 changes: 2 additions & 11 deletions src/cards/select-card/controls/select-option-control.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HassEntity } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators.js";
import { computeStateDisplay, HomeAssistant } from "../../../ha";
import { HomeAssistant } from "../../../ha";
import "../../../shared/form/mushroom-select";
import { getCurrentOption, getOptions } from "../utils";

Expand Down Expand Up @@ -47,16 +47,7 @@ export class SelectOptionControl extends LitElement {
${options.map((option) => {
return html`
<mwc-list-item .value=${option}>
${this.hass.formatEntityState
? this.hass.formatEntityState(this.entity, option)
: computeStateDisplay(
this.hass.localize,
this.entity,
this.hass.locale,
this.hass.config,
this.hass.entities,
option
)}
${this.hass.formatEntityState(this.entity, option)}
</mwc-list-item>
`;
})}
Expand Down
Loading

0 comments on commit d17469a

Please sign in to comment.