Skip to content

Commit

Permalink
feat: add link type for attribute
Browse files Browse the repository at this point in the history
Fix #92
  • Loading branch information
royto committed Dec 21, 2023
1 parent f406c97 commit eb452e6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ All properties are optional.

#### Attribute object

| Name | Type | Default | Description |
| ---------------------- | :----: | :-----------: | ----------------------------------------------------------------------- |
| value **_(required)_** | string | | name of the attributes. |
| label | string | same as value | String to show as label. |
| type | string | string | Type of the value used for formatting. Only date is currently supported |
| Name | Type | Default | Description |
| ---------------------- | :----: | :-----------: | -------------------------------------------------------------------- |
| value **_(required)_** | string | | name of the attributes. |
| label | string | same as value | String to show as label. |
| type | string | | Type of the value used for formatting. `date` or `url` are supported |
| link_label | string | | Link label to use when `type` is `url`. |

#### Duration object

Expand Down
2 changes: 1 addition & 1 deletion src/entity-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const extractAttributes = (
const attributeName = hass.formatEntityAttributeName ? hass.formatEntityAttributeName(item, c.value) : c.value;
p.push({
name: c.label ? c.label : attributeName,
value: formatEntityAttributeValue(hass, item, c.value, attribute, c.type, config.date_format),
value: formatEntityAttributeValue(hass, item, c.value, attribute, c.type, config.date_format, c.link_label),
});
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ export const formatEntityAttributeValue = (
entity: HassEntity,
attribute: string,
value: any,
type: string | undefined,
type: 'date' | 'url' | undefined,
dateFormat: string | 'relative' | undefined,
linkLabel: string | undefined,
): string | TemplateResult => {
if (type === 'date') {
return displayDate(hass, new Date(value), dateFormat);
}
if (type === 'url') {
return html`
<a .href="${value}" target="_blank">${linkLabel ? linkLabel : value}</a>
`;
}
if (hass.formatEntityAttributeValue) {
return hass.formatEntityAttributeValue(entity, attribute);
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export interface ShowConfiguration {
export interface AttributeConfig {
value: string;
label?: string;
type?: string;
type?: 'date' | 'url';
link_label?: string;
}

export interface History {
Expand Down

0 comments on commit eb452e6

Please sign in to comment.