Skip to content

Commit

Permalink
Clean stateObj
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Mar 5, 2024
1 parent 658a593 commit d27eb91
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ export class AlarmControlPanelCard
}

async loadComponents() {
if (!this._config || !this.hass || !this._config.entity) return;
const entityId = this._config.entity;
const stateObj = this.hass.states[entityId] as HassEntity | undefined;
const stateObj = this._stateObj;

if (stateObj && hasCode(stateObj)) {
void import("../../shared/form/mushroom-textfield");
Expand Down
8 changes: 2 additions & 6 deletions src/cards/cover-card/cover-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ export class CoverCard

updatePosition() {
this.position = undefined;
if (!this._config || !this.hass || !this._config.entity) return;

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

if (!stateObj) return;
this.position = getPosition(stateObj);
Expand All @@ -166,8 +163,7 @@ export class CoverCard
return nothing;
}

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

if (!stateObj) {
return this.renderNotFound(this._config);
Expand Down
8 changes: 2 additions & 6 deletions src/cards/light-card/light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ export class LightCard

updateBrightness() {
this.brightness = undefined;
if (!this._config || !this.hass || !this._config.entity) return;

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

if (!stateObj) return;
this.brightness = getBrightness(stateObj);
Expand Down Expand Up @@ -159,8 +156,7 @@ export class LightCard
return nothing;
}

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

if (!stateObj) {
return this.renderNotFound(this._config);
Expand Down
5 changes: 1 addition & 4 deletions src/cards/media-player-card/media-player-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ export class MediaPlayerCard

updateVolume() {
this.volume = undefined;
if (!this._config || !this.hass || !this._config.entity) return;

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

if (!stateObj) return;
const volume = getVolumeLevel(stateObj);
Expand Down
8 changes: 2 additions & 6 deletions src/cards/number-card/number-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ export class NumberCard extends MushroomBaseCard<NumberCardConfig> implements Lo

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;
const stateObj =this._stateObj;

if (!stateObj || Number.isNaN(stateObj.state)) return;
this.value = Number(stateObj.state);
Expand All @@ -96,8 +93,7 @@ export class NumberCard extends MushroomBaseCard<NumberCardConfig> implements Lo
return nothing;
}

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

if (!stateObj) {
return this.renderNotFound(this._config);
Expand Down

0 comments on commit d27eb91

Please sign in to comment.