Skip to content

Commit

Permalink
Convert additional entities config to list (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
benct committed Feb 13, 2020
1 parent 9024bba commit f839fec
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 94 deletions.
146 changes: 66 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ resources:
| toggle | bool | `false` | Display a toggle (if supported) instead of state
| hide_state | bool | `false` | Hide the entity state
| name_state | string | | Show name/header above the main entity state
| state_color | bool | false | Enable colored icon when entity is active
| state_color | bool | `false` | Enable colored icon when entity is active
| | | |
| primary | object | *see below* | Primary additional entity object
| secondary | object | *see below* | Secondary additional entity object (not to be confused with `secondary_info`)
| tertiary | object | *see below* | Tertiary additional entity object (make sure there is enough room)
| info | object | *see below* | Additional entity object as `secondary_info`
| entities | list | *see below* | Additional entity IDs or entity object(s)
| info | object | *see below* | Custom `secondary_info` entity object

### Entity objects (primary | secondary | tertiary | info*)
### Entity objects

Either `entity`, `attribute` or `service` needs to be specified. `entity` is only required if you want to display data from another entity
than the main entity specified above. `attribute` is necessary if you want to display an entity attribute value instead of the state
Expand All @@ -59,7 +57,7 @@ value. `service` lets you call a specified service on click instead of showing t
| service | string | | A valid service, including domain (e.g. `light.turn_on`)
| service_data | object | | Optional data to send together with `service`

\* The `info` object does not support `toggle`, `icon` or `service`
\* The `info` entity object does not support `toggle`, `icon` or `service`

## Example

Expand All @@ -70,31 +68,28 @@ type: entities
entities:
- entity: sensor.bedroom_temperature
type: custom:multiple-entity-row
name: Primary
name: One entity
secondary_info: last-changed
primary:
entity: sensor.bedroom_max_temp
entities:
- sensor.bedroom_max_temp

- entity: sensor.bedroom_temperature
type: custom:multiple-entity-row
name: Primary + Secondary
name: Two entities
secondary_info: last-changed
primary:
entity: sensor.bedroom_min_temp
secondary:
entity: sensor.bedroom_max_temp
entities:
- sensor.bedroom_min_temp
- sensor.bedroom_max_temp

- entity: sensor.bedroom_temperature
type: custom:multiple-entity-row
name: Pri + Sec + Tertiary
name: Three entities
secondary_info: last-changed
primary:
entity: sensor.bedroom_humidity
name: humidity
secondary:
entity: sensor.bedroom_min_temp
tertiary:
entity: sensor.bedroom_max_temp
entities:
- entity: sensor.bedroom_humidity
name: humidity
- sensor.bedroom_min_temp
- sensor.bedroom_max_temp

- entity: sensor.bedroom_temperature
type: custom:multiple-entity-row
Expand All @@ -108,73 +103,66 @@ entities:
- entity: vacuum.xiaomi_vacuum_cleaner
type: custom:multiple-entity-row
name: Attributes
primary:
attribute: battery_level
name: Battery
unit: '%'
secondary:
attribute: status
name: Status
entities:
- attribute: battery_level
name: Battery
unit: '%'
- attribute: status
name: Status

- entity: sensor.lovelace_multiple_entity_row
type: custom:multiple-entity-row
name: Attributes (hide_state)
hide_state: true
primary:
attribute: stargazers
name: Stars
secondary:
attribute: open_issues
name: Issues
tertiary:
attribute: open_pull_requests
name: PRs
entities:
- attribute: stargazers
name: Stars
- attribute: open_issues
name: Issues
- attribute: open_pull_requests
name: PRs

- type: section
- entity: sensor.bedroom_temperature
type: custom:multiple-entity-row
name: Hide names
primary:
entity: sensor.bedroom_min_temp
name: false
secondary:
entity: sensor.bedroom_max_temp
name: false
entities:
- entity: sensor.bedroom_min_temp
name: false
- entity: sensor.bedroom_max_temp
name: false

- entity: sensor.bedroom_temperature
type: custom:multiple-entity-row
name: Main state name
name_state: current
primary:
entity: sensor.bedroom_min_temp
secondary:
entity: sensor.bedroom_max_temp
entities:
- sensor.bedroom_min_temp
- sensor.bedroom_max_temp

- type: section
- entity: switch.livingroom_tv
type: custom:multiple-entity-row
name: Toggle
toggle: true
primary:
entity: sensor.livingroom_tv_power
name: Power
secondary:
entity: sensor.livingroom_tv_power_total
name: Total
entities:
- entity: sensor.livingroom_tv_power
name: Power
- entity: sensor.livingroom_tv_power_total
name: Total

- entity: switch.livingroom_tv
type: custom:multiple-entity-row
name: Multiple toggles
name_state: main
toggle: true
primary:
entity: switch.livingroom_light
name: toggle1
toggle: true
secondary:
entity: switch.livingroom_light_2
name: toggle2
toggle: true
entities:
- entity: switch.livingroom_light
name: toggle1
toggle: true
- entity: switch.livingroom_light_2
name: toggle2
toggle: true

- type: section
- entity: sensor.bedroom_temperature
Expand All @@ -183,29 +171,27 @@ entities:
icon: mdi:fire
unit: °F
secondary_info: last-changed
primary:
name: custom name
entity: sensor.bedroom_max_temp
unit: temp
entities:
- entity: sensor.bedroom_max_temp
name: custom name
unit: temp

- type: section
- entity: sensor.living_room_temperature
type: custom:multiple-entity-row
name: Icons (service calls)
secondary_info: last-changed
primary:
entity: light.living_room
icon: mdi:palette
secondary:
icon: mdi:lightbulb-off-outline
service: light.turn_off
service_data:
entity_id: light.living_room
tertiary:
icon: mdi:lightbulb-outline
service: light.turn_on
service_data:
entity_id: light.living_room
entities:
- entity: light.living_room
icon: mdi:palette
- icon: mdi:lightbulb-off-outline
service: light.turn_off
service_data:
entity_id: light.living_room
- icon: mdi:lightbulb-outline
service: light.turn_on
service_data:
entity_id: light.living_room
```

## My cards
Expand Down
29 changes: 15 additions & 14 deletions multiple-entity-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@
: null}
</div>
</div>
${this.renderEntity(this.state.primary)}
${this.renderEntity(this.state.secondary)}
${this.renderEntity(this.state.tertiary)}
${this.state.entities.map(entity => this.renderEntity(entity))}
${this.state.value ? html`
<div class="state entity" @click="${this.onClick}">
${this.stateHeader && html`<span>${this.stateHeader}</span>`}
Expand All @@ -114,10 +112,10 @@

setConfig(config) {
if (!config.entity) throw new Error('Please define a main entity.');
this.checkEntity(config, 'primary');
this.checkEntity(config, 'secondary');
this.checkEntity(config, 'tertiary');
this.checkEntity(config, 'info');
if (config.entities) {
config.entities.map(entity => this.checkEntity(entity));
}
this.checkEntity(config.info);

this.lastChanged = config.secondary_info === 'last-changed' && !config.info;
this.stateHeader = config.name_state !== undefined ? config.name_state : null;
Expand All @@ -142,17 +140,19 @@
value: this._config.hide_state !== true ? this.entityStateValue(mainStateObj, this._config.unit) : null,
toggle: this.checkToggle(this._config, mainStateObj),

primary: this.initEntity(this._config.primary, mainStateObj),
secondary: this.initEntity(this._config.secondary, mainStateObj),
tertiary: this.initEntity(this._config.tertiary, mainStateObj),
entities: this._config.entities ? this._config.entities.map(entity => this.initEntity(entity, mainStateObj)) : [],
info: this.initEntity(this._config.info, mainStateObj),
}
}
}

checkEntity(config, key) {
if (config[key] && !(config[key].entity || config[key].attribute || config[key].service)) {
throw new Error(`Object '${key}' requires at least one 'entity', 'attribute' or 'service'.`);
checkEntity(config) {
if (config && typeof config === 'object' && !(config.entity || config.attribute || config.service)) {
throw new Error(`Entity object requires at least one 'entity', 'attribute' or 'service'.`);
} else if (config && typeof config === 'string' && config === '') {
throw new Error('Entity ID string must not be blank.');
} else if (config && typeof config !== 'string' && typeof config !== 'object') {
throw new Error('Entity config must be a valid entity ID string or entity object.');
}
}

Expand All @@ -163,7 +163,8 @@
initEntity(config, mainStateObj) {
if (!config) return null;

const stateObj = config.entity ? (this._hass && this._hass.states[config.entity]) : mainStateObj;
const entity = typeof config === 'string' ? config : config.entity;
const stateObj = entity ? (this._hass && this._hass.states[entity]) : mainStateObj;

if (!stateObj) return {value: this._hass.localize('state.default.unavailable')};

Expand Down

0 comments on commit f839fec

Please sign in to comment.