Skip to content

Commit

Permalink
add support for hiding entity name in overview page
Browse files Browse the repository at this point in the history
  • Loading branch information
niels committed Oct 22, 2020
1 parent 4e360ca commit 87e11e8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/custom-elements/condition-entity-row.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LitElement, html, customElement, css, property } from 'lit-element';
import { Condition, EConditionMatchType, CardConfig, EntityElement } from '../types';
import { PrettyPrintIcon, PrettyPrintName } from '../helpers';
import { HomeAssistant } from 'custom-card-helpers';
import { HomeAssistant, computeEntity } from 'custom-card-helpers';
import { entityConfig } from '../entity';
import { DefaultEntityIcon } from '../const';

Expand All @@ -26,8 +26,9 @@ export class ConditionEntityRow extends LitElement {

render() {
if (!this.item || !this.hass || !this.config) return html``;
const stateObj = this.hass.states[this.item.entity];

if (!this.entity) {
if (!this.entity || !stateObj) {
return html`
<hui-warning>
Entity not found '${this.item.entity}'
Expand All @@ -39,7 +40,7 @@ export class ConditionEntityRow extends LitElement {
<div class="list-item">
<mwc-button @click="${this.entityButtonClick}" class="${this.selected ? 'active' : ''}">
<ha-icon icon="${PrettyPrintIcon(this.entity.icon || DefaultEntityIcon)}"></ha-icon>
${PrettyPrintName(this.entity.name)}
${PrettyPrintName(this.entity.name || stateObj.attributes.friendly_name || computeEntity(stateObj.entity_id))}
</mwc-button>
${this.getMatchTypeButton()}
${this.getStateButton()}
Expand Down
2 changes: 1 addition & 1 deletion src/custom-elements/scheduler-entities-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class SchedulerEntitiesCard extends LitElement {

if (this.config.discover_existing !== undefined && !this.config.discover_existing) {
schedules = schedules.filter(el =>
(el.attributes.actions.map(e => e.entity) as string[]).every(e => this._hass!.states[e] && entityFilter(e, this._hass!, this.config!))
(el.attributes.actions.map(importAction).map(e => e.entity) as string[]).every(e => this._hass!.states[e] && entityFilter(e, this._hass!, this.config!))
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/custom-elements/scheduler-entity-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ScheduleEntityRow extends LitElement {
if (entityCfg) { //entity exists in HASS
let actionCfg = findAction(entityCfg, action);
icon = actionCfg.icon || entityCfg.icon || DefaultActionIcon;
entityName = entityCfg.name;
entityName = entityCfg.name !== undefined ? entityCfg.name : this.hass.states[action.entity].attributes.friendly_name || computeEntity(action.entity);
}

let friendlyName = stateObj.attributes.friendly_name?.match(/^schedule\ #[0-9a-f]{6}$/i) ? '' : stateObj.attributes.friendly_name;
Expand All @@ -62,7 +62,8 @@ export class ScheduleEntityRow extends LitElement {
>
</state-badge>
<div class="info">
${capitalize(PrettyPrintName(entityName))}: ${capitalize(formatAction(action, this.hass))}
${entityName.length ? `${capitalize(PrettyPrintName(entityName))}: ` : ''}
${capitalize(formatAction(action, this.hass))}
<div class="secondary">
${capitalize(relativeTime(this.computeTimestamp(nextEntry)))}<br>
${entries.length > 1 ? entries.length == 2 ? localize('misc.one_additional_task') : localize("misc.x_additional_tasks", "{count}", String(entries.length - 1)) : ''}
Expand Down
7 changes: 4 additions & 3 deletions src/custom-elements/scheduler-entitypicker-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LitElement, html, customElement, property } from 'lit-element';
import { HomeAssistant } from 'custom-card-helpers';
import { HomeAssistant, computeEntity } from 'custom-card-helpers';
import { localize } from '../localize/localize';
import { CardConfig, EntityElement, GroupElement, ActionElement } from '../types';
import { entityFilter, entityConfig } from '../entity';
Expand Down Expand Up @@ -39,9 +39,10 @@ export class SchedulerEditorCard extends LitElement {
let entities = groupConfig
.find(e => e.id == this.selectedGroup)!.entities
.map(e => entityConfig(e, this.hass!, this.config!))
.filter(e => e) as EntityElement[];
.filter(e => e)
.map(e => e!.name ? e : Object.assign(e, { name: this.hass!.states[e!.id].attributes.friendly_name || computeEntity(e!.id) })) as EntityElement[];

entities.sort((a, b) => a.name.trim().toLowerCase() < b.name.trim().toLowerCase() ? -1 : 1);
entities.sort((a, b) => a.name!.trim().toLowerCase() < b.name!.trim().toLowerCase() ? -1 : 1);
return entities;
}

Expand Down
7 changes: 4 additions & 3 deletions src/custom-elements/scheduler-options-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { localize } from '../localize/localize';
import { EConditionType, CardConfig, Entry, EntityElement, Condition, EConditionMatchType } from '../types';

import './condition-entity-row';
import { HomeAssistant } from 'custom-card-helpers';
import { HomeAssistant, computeEntity } from 'custom-card-helpers';
import { entityGroups } from '../group';
import { entityConfig, entityFilter } from '../entity';
import { commonStyle } from '../styles';
Expand Down Expand Up @@ -114,8 +114,9 @@ export class SchedulerOptionsCard extends LitElement {
if (this.selectedGroup) {
entities = groups.find(e => e.id == this.selectedGroup)!.entities
.map(e => entityConfig(e, this.hass!, this.config!))
.filter(e => e) as EntityElement[];
entities.sort((a, b) => a.name.trim().toLowerCase() < b.name.trim().toLowerCase() ? -1 : 1);
.filter(e => e)
.map(e => e!.name ? e : Object.assign(e, { name: this.hass!.states[e!.id].attributes.friendly_name || computeEntity(e!.id) })) as EntityElement[];
entities.sort((a, b) => a.name!.trim().toLowerCase() < b.name!.trim().toLowerCase() ? -1 : 1);
}

return html`
Expand Down
6 changes: 3 additions & 3 deletions src/custom-elements/scheduler-timepicker-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LitElement, html, customElement, css, property } from 'lit-element';
import { HomeAssistant } from 'custom-card-helpers';
import { HomeAssistant, computeEntity } from 'custom-card-helpers';
import { localize } from '../localize/localize';
import { CardConfig, ActionElement, EntityElement, EVariableType, LevelVariableConfig, LevelVariable, ListVariable, Entry, ListVariableConfig } from '../types';
import { PrettyPrintIcon, PrettyPrintName, capitalize } from '../helpers';
Expand Down Expand Up @@ -55,7 +55,7 @@ export class SchedulerTimepickerCard extends LitElement {
<div class="summary-entity">
<ha-icon icon="${PrettyPrintIcon(this.entity.icon)}">
</ha-icon>
${capitalize(PrettyPrintName(this.entity.name))}
${capitalize(PrettyPrintName(this.entity.name || this.hass!.states[this.entity.id].attributes.friendly_name || computeEntity(this.entity.id)))}
</div>
<div class="summary-arrow">
<ha-icon icon="hass:arrow-right">
Expand Down Expand Up @@ -114,7 +114,7 @@ export class SchedulerTimepickerCard extends LitElement {
<div class="summary-entity">
<ha-icon icon="${PrettyPrintIcon(this.entity.icon)}">
</ha-icon>
${capitalize(PrettyPrintName(this.entity.name))}
${capitalize(PrettyPrintName(this.entity.name || this.hass!.states[this.entity.id].attributes.friendly_name || computeEntity(this.entity.id)))}
</div>
<div class="summary-arrow">
<ha-icon icon="hass:arrow-right">
Expand Down
1 change: 0 additions & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function entityConfig(entity_id: string, hass: HomeAssistant, config: Par

let output: EntityElement = {
id: entity_id,
name: stateObj.attributes.friendly_name || computeEntity(entity_id),
icon: DefaultEntityIcon,
actions: [],
};
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface GroupConfig {

export interface EntityElement extends EntityConfig {
id: string;
name: string;
name?: string;
icon?: string;
actions: ActionConfig[];
exclude_actions?: string[];
Expand Down

0 comments on commit 87e11e8

Please sign in to comment.