Skip to content

Commit

Permalink
Fix number state when changing value (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Dec 4, 2023
1 parent 5ae6565 commit a4c5d07
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/cards/number-card/number-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HassEntity } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, nothing, TemplateResult } from "lit";
import { css, CSSResultGroup, html, nothing, PropertyValues, TemplateResult } from "lit";
import { customElement, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
Expand Down Expand Up @@ -87,6 +87,24 @@ export class NumberCard extends MushroomBaseCard implements LovelaceCard {
}
}

protected updated(changedProperties: PropertyValues) {
super.updated(changedProperties);
if (this.hass && changedProperties.has("hass")) {
this.updateValue();
}
}

updateValue() {
this.value = undefined;
if (!this._config || !this.hass || !this._config.entity) return;

const entityId = this._config.entity;
const stateObj = this.hass.states[entityId] as HassEntity | undefined;

if (!stateObj || Number.isNaN(stateObj.state)) return;
this.value = Number(stateObj.state);
}

protected render() {
if (!this._config || !this.hass || !this._config.entity) {
return nothing;
Expand Down

0 comments on commit a4c5d07

Please sign in to comment.