Fix button card not updating enough (#16102)

This commit is contained in:
Bram Kragten 2023-04-11 10:25:54 +02:00 committed by GitHub
parent 8a8da45761
commit 770675ab27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 18 deletions

View File

@ -24,26 +24,47 @@ import { LocalizeFunc } from "../translations/localize";
import { computeDomain } from "./compute_domain"; import { computeDomain } from "./compute_domain";
import { supportsFeatureFromAttributes } from "./supports-feature"; import { supportsFeatureFromAttributes } from "./supports-feature";
export const computeStateDisplaySingleEntity = (
localize: LocalizeFunc,
stateObj: HassEntity,
locale: FrontendLocaleData,
entity: EntityRegistryDisplayEntry | undefined,
state?: string
): string =>
computeStateDisplayFromEntityAttributes(
localize,
locale,
entity,
stateObj.entity_id,
stateObj.attributes,
state !== undefined ? state : stateObj.state
);
export const computeStateDisplay = ( export const computeStateDisplay = (
localize: LocalizeFunc, localize: LocalizeFunc,
stateObj: HassEntity, stateObj: HassEntity,
locale: FrontendLocaleData, locale: FrontendLocaleData,
entities: HomeAssistant["entities"], entities: HomeAssistant["entities"],
state?: string state?: string
): string => ): string => {
computeStateDisplayFromEntityAttributes( const entity = entities[stateObj.entity_id] as
| EntityRegistryDisplayEntry
| undefined;
return computeStateDisplayFromEntityAttributes(
localize, localize,
locale, locale,
entities, entity,
stateObj.entity_id, stateObj.entity_id,
stateObj.attributes, stateObj.attributes,
state !== undefined ? state : stateObj.state state !== undefined ? state : stateObj.state
); );
};
export const computeStateDisplayFromEntityAttributes = ( export const computeStateDisplayFromEntityAttributes = (
localize: LocalizeFunc, localize: LocalizeFunc,
locale: FrontendLocaleData, locale: FrontendLocaleData,
entities: HomeAssistant["entities"], entity: EntityRegistryDisplayEntry | undefined,
entityId: string, entityId: string,
attributes: any, attributes: any,
state: string state: string
@ -52,8 +73,6 @@ export const computeStateDisplayFromEntityAttributes = (
return localize(`state.default.${state}`); return localize(`state.default.${state}`);
} }
const entity = entities[entityId] as EntityRegistryDisplayEntry | undefined;
// Entities with a `unit_of_measurement` or `state_class` are numeric values and should use `formatNumber` // Entities with a `unit_of_measurement` or `state_class` are numeric values and should use `formatNumber`
if (isNumericFromAttributes(attributes)) { if (isNumericFromAttributes(attributes)) {
// state is duration // state is duration

View File

@ -4,7 +4,7 @@ import { FrontendLocaleData } from "../../data/translation";
export const blankBeforePercent = ( export const blankBeforePercent = (
localeOptions: FrontendLocaleData localeOptions: FrontendLocaleData
): string => { ): string => {
switch (localeOptions.language) { switch (localeOptions?.language) {
case "cz": case "cz":
case "de": case "de":
case "fi": case "fi":

View File

@ -291,7 +291,7 @@ const processTimelineEntity = (
state_localize: computeStateDisplayFromEntityAttributes( state_localize: computeStateDisplayFromEntityAttributes(
localize, localize,
language, language,
entities, entities[entityId],
entityId, entityId,
{ {
...(state.a || first.a), ...(state.a || first.a),

View File

@ -18,7 +18,7 @@ import { DOMAINS_TOGGLE } from "../../../common/const";
import { transform } from "../../../common/decorators/transform"; import { transform } from "../../../common/decorators/transform";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element"; import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import { computeDomain } from "../../../common/entity/compute_domain"; import { computeDomain } from "../../../common/entity/compute_domain";
import { computeStateDisplay } from "../../../common/entity/compute_state_display"; import { computeStateDisplaySingleEntity } from "../../../common/entity/compute_state_display";
import { computeStateDomain } from "../../../common/entity/compute_state_domain"; 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 { stateColorCss } from "../../../common/entity/state_color"; import { stateColorCss } from "../../../common/entity/state_color";
@ -46,6 +46,7 @@ import { ButtonCardConfig } from "./types";
import { LocalizeFunc } from "../../../common/translations/localize"; import { LocalizeFunc } from "../../../common/translations/localize";
import { FrontendLocaleData } from "../../../data/translation"; import { FrontendLocaleData } from "../../../data/translation";
import { Themes } from "../../../data/ws-themes"; import { Themes } from "../../../data/ws-themes";
import { EntityRegistryDisplayEntry } from "../../../data/entity_registry";
@customElement("hui-button-card") @customElement("hui-button-card")
export class HuiButtonCard extends LitElement implements LovelaceCard { export class HuiButtonCard extends LitElement implements LovelaceCard {
@ -90,25 +91,26 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
}) })
_stateObj?: HassEntity; _stateObj?: HassEntity;
@state()
@consume({ context: themesContext, subscribe: true }) @consume({ context: themesContext, subscribe: true })
_themes!: Themes; _themes!: Themes;
@state()
@consume({ context: localizeContext, subscribe: true }) @consume({ context: localizeContext, subscribe: true })
_localize!: LocalizeFunc; _localize!: LocalizeFunc;
@state()
@consume({ context: localeContext, subscribe: true }) @consume({ context: localeContext, subscribe: true })
_locale!: FrontendLocaleData; _locale!: FrontendLocaleData;
@consume({ context: entitiesContext, subscribe: true }) @consume<any>({ context: entitiesContext, subscribe: true })
@transform<HomeAssistant["entities"], HomeAssistant["entities"]>({ @transform<HomeAssistant["entities"], EntityRegistryDisplayEntry>({
transformer: function (this: HuiButtonCard, value) { transformer: function (this: HuiButtonCard, value) {
return this._config?.entity return this._config?.entity ? value[this._config?.entity] : undefined;
? { [this._config?.entity]: value[this._config?.entity] }
: {};
}, },
watch: ["_config"], watch: ["_config"],
}) })
_entities!: HomeAssistant["entities"]; _entity?: EntityRegistryDisplayEntry;
@queryAsync("mwc-ripple") private _ripple!: Promise<Ripple | null>; @queryAsync("mwc-ripple") private _ripple!: Promise<Ripple | null>;
@ -150,7 +152,7 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
} }
protected render() { protected render() {
if (!this._config) { if (!this._config || !this._localize || !this._locale) {
return nothing; return nothing;
} }
const stateObj = this._stateObj; const stateObj = this._stateObj;
@ -217,11 +219,11 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
: ""} : ""}
${this._config.show_state && stateObj ${this._config.show_state && stateObj
? html`<span class="state"> ? html`<span class="state">
${computeStateDisplay( ${computeStateDisplaySingleEntity(
this._localize, this._localize,
stateObj, stateObj,
this._locale, this._locale,
this._entities this._entity
)} )}
</span>` </span>`
: ""} : ""}