Skip to content

Commit

Permalink
feat: map_attributes (#113)
Browse files Browse the repository at this point in the history
Allow overriding result based on mappers for attribute values just like map_state allows for mapping to values based on state.
* Add map_attribute
* Add map_attribute to readme
  • Loading branch information
mikesplain authored May 2, 2021
1 parent 96639b4 commit a456f0c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
50 changes: 33 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,24 @@ Also release management have been automated so its likely you will see more freq

## Available configuration options:

| Key | Type | Description | Example |
| -------------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| heading | \_string | array\_ | The heading to display. Set to false to hide header (**Note**: Also remember to escape!). Can also be an array, with icons. | `heading: "\U0001F6CB Living room"` |
| background | _string_ | A valid CSS color to use as the background | `background: "#EDE7B0"`, `background: red` |
| color | _string_ | A valid CSS color to use as the text color | `color: "#EDE7B0"`, `color: red` |
| link | _string_ | A link, to a different view in HA for example | `link: /lovelace/living_room` |
| entities | _array_ | An array of entities to display for glances. Either as strings or as objects | `entities: [binary_sensor.remote_ui]` |
| row_size | \_number | string\_ | Number of columns in the grid. 3 is the default and what looks best _in many cases_. Set "auto" to equal row_size to number of entities provided | `row_size: 4` |
| entities[].entity | _string_ | Entity id | `- entity: binary_sensor.remote_ui` |
| entities[].unit | _string_ or _false_ | Override the automatic unit | `unit: My unit` |
| entities[].name | _string_ | Override the automatic usage of friendly_name | `name: A sensor` |
| entities[].map_state | _object_ | Map state values to resulting text or icons. A string prefixed with mdi: or hass: will yield a rendered icon. | map_state:<br /> home: mdi:home-account<br /> not_home: mdi:walk |
| entities[].attribute | _string_ | Display an attribute instead of the state | |
| entities[].size | _number_ | Override how many "entity cells" this entity will fill. The default for most entities is 1 cell, except if you include a media_player which will use whatever is the value for `row_size`, thus full width. | |
| entities[].when | _string_ or _object_ | Only display this entity when these tests pass | See separate section |
| entities[].image | _bool_ | Force display the value as a rounded image | Will use the provided value as a background for the `<state-badge>` component from HA |
| entities[].action | _object_ | Specify a service to be called on tap. Will result in either an icon (if a valid icon is set as value with map_state) or a button with the state value as text | See separate section |
| Key | Type | Description | Example |
| ------------------------ | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| heading | \_string | array\_ | The heading to display. Set to false to hide header (**Note**: Also remember to escape!). Can also be an array, with icons. | `heading: "\U0001F6CB Living room"` |
| background | _string_ | A valid CSS color to use as the background | `background: "#EDE7B0"`, `background: red` |
| color | _string_ | A valid CSS color to use as the text color | `color: "#EDE7B0"`, `color: red` |
| link | _string_ | A link, to a different view in HA for example | `link: /lovelace/living_room` |
| entities | _array_ | An array of entities to display for glances. Either as strings or as objects | `entities: [binary_sensor.remote_ui]` |
| row_size | \_number | string\_ | Number of columns in the grid. 3 is the default and what looks best _in many cases_. Set "auto" to equal row_size to number of entities provided | `row_size: 4` |
| entities[].entity | _string_ | Entity id | `- entity: binary_sensor.remote_ui` |
| entities[].unit | _string_ or _false_ | Override the automatic unit | `unit: My unit` |
| entities[].name | _string_ | Override the automatic usage of friendly_name | `name: A sensor` |
| entities[].map_state | _object_ | Map state values to resulting text or icons. A string prefixed with mdi: or hass: will yield a rendered icon. | map_state:<br /> home: mdi:home-account<br /> not_home: mdi:walk |
| entities[].attribute | _string_ | Display an attribute instead of the state | |
| entities[].map_attribute | _object_ | Map attribute values, instead of state, to resulting text or icons. A string prefixed with mdi: or hass: will yield a rendered icon. | map_state:<br /> home: mdi:home-account<br /> not_home: mdi:walk |
| entities[].size | _number_ | Override how many "entity cells" this entity will fill. The default for most entities is 1 cell, except if you include a media_player which will use whatever is the value for `row_size`, thus full width. | |
| entities[].when | _string_ or _object_ | Only display this entity when these tests pass | See separate section |
| entities[].image | _bool_ | Force display the value as a rounded image | Will use the provided value as a background for the `<state-badge>` component from HA |
| entities[].action | _object_ | Specify a service to be called on tap. Will result in either an icon (if a valid icon is set as value with map_state) or a button with the state value as text | See separate section |

### heading

Expand Down Expand Up @@ -95,6 +96,21 @@ map_state:
name: A custom entity heading
```

### map_attribute

You can use `map_attribute` the same way as map_state to force a value or icon to be rendered when the entity attribute has a certain value. This should be used alongside the `attribute` key.
Both forms in an example:

```yaml
entity: media_player.office
attribute: current_activity
map_attribute:
game_system: mdi:controller-classic
tv:
value: mdi:television-classic
name: A custom TV title
```

## Using when

You can filter entities with a simple but powerful `when` object. This allows you to filter based on state and/or attributes. It is probably simpliest explained through a few examples
Expand Down
16 changes: 16 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ export default class BannerCard extends LitElement {
}
}

if (config.attribute) {
const attributeValue = attributes[config.attribute];

if (config.map_attribute && attributeValue in config.map_attribute) {
const mappedAttribute = config.map_attribute[attributeValue];
const mapAttributeType = typeof mappedAttribute;
if (mapAttributeType === "string") {
dynamicData.value = mappedAttribute;
} else if (mapAttributeType === "object") {
Object.entries(mappedAttribute).forEach(([key, val]) => {
dynamicData[key] = val;
});
}
}
}

const data = {
name: attributes.friendly_name,
state: state ? state.state : "",
Expand Down

0 comments on commit a456f0c

Please sign in to comment.