Skip to content

Commit

Permalink
Format state value in info
Browse files Browse the repository at this point in the history
  • Loading branch information
Anonym-tsk committed Jan 13, 2024
1 parent 8fcb02f commit f194825
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
40 changes: 34 additions & 6 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
declare type StateValue = number | string | boolean;

interface EntityState {
state: StateValue;
attributes: Record<string, StateValue>;
}

interface Entity {
entity_id: string;
device_id: string;
}

type Context = {
id: string;
user_id: string | null;
parent_id: string | null;
};

type HassEntityBase = {
entity_id: string;
state: string;
last_changed: string;
last_updated: string;
attributes: HassEntityAttributeBase;
context: Context;
};

type HassEntityAttributeBase = {
friendly_name?: string;
unit_of_measurement?: string;
icon?: string;
entity_picture?: string;
supported_features?: number;
hidden?: boolean;
assumed_state?: boolean;
device_class?: string;
state_class?: string;
restored?: boolean;
};

type HassEntity = HassEntityBase & {
attributes: { [key: string]: any };
};

interface Hass {
language: string;
states: Record<string, EntityState>;
states: Record<string, HassEntity>;
entities: Record<string, Entity>;
callService: (event: string, action: string, data: { [key: string]: StateValue }) => void;
formatEntityState: (stateObj: HassEntity, state?: string) => '',
}

interface CustomCard {
Expand Down
23 changes: 18 additions & 5 deletions src/ts/StarlineCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ export class StarlineCard extends HTMLElement {
return null;
}

_getStateObject(entity_id: ConfigEntity): HassEntity | null {
const entityName = this._config.entities?.[entity_id];
if (!entityName || !this._hass) {
return null;
}

return this._hass.states[entityName];
}

_getAttr(entity_id: ConfigEntity, name: string): StateValue | null {
const entityName = this._config.entities?.[entity_id];
if (!entityName) {
Expand Down Expand Up @@ -277,12 +286,16 @@ export class StarlineCard extends HTMLElement {
const state = this._getState(key);
visible = state !== null;

if (state !== null && state !== data.value) {
if (state !== null && state !== data.value && this._hass) {
this._info[key].value = state;
// Не показываем 'satellites' для спутников
const unit = key === 'gps' ? '' :
this._getAttr(key, 'unit_of_measurement');
this._info[key].element!.querySelector('.info-i-cnt')!.textContent = `${state} ${unit}`;

const stateObj = this._getStateObject(key)!;
if (key === 'gps') {
// Не показываем 'satellites' для спутников
stateObj.attributes.unit_of_measurement = '';
}

this._info[key].element!.querySelector('.info-i-cnt')!.textContent = this._hass.formatEntityState(stateObj);
}
}

Expand Down

0 comments on commit f194825

Please sign in to comment.