Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capitalize first letter of entity name #615

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ colors:
| sort | list([Sort](#sort-object) \| string) | | v3.0.0 | Sets the sorting options
| collapse | number \| list([Group](#group-object)) | | v1.0.0 | Number of entities to show. Rest will be available in expandable section ([example](#sorted-list-and-collapsed-view)). Or list of entity/battery groups ([example](#battery-groups))
| filter | [Filters](#filters) | | v1.3.0 | Filter groups to automatically include or exclude entities ([example](#entity-filtering-and-bulk-renaming))
| bulk_rename | list([Convert](#convert)) | | v1.3.0 | Rename rules applied for all entities ([example](#entity-filtering-and-bulk-renaming))
| bulk_rename | list([Convert](#convert)) \| [BulkRename](#bulk-rename) | | v1.3.0 | Rename rules applied for all entities ([example](#entity-filtering-and-bulk-renaming))

+[common options](#common-options) (if specified they will be apllied to all entities)

Expand Down Expand Up @@ -227,6 +227,13 @@ The definition is similar to the default [tap-action](https://www.home-assistant
| to | any | **(required)** | v1.1.0 | Target value
| display | string | | v3.0.0 | Override for displayed entity state (when the current entiy state matches the `from` value)

### Bulk rename

| Name | Type | Default | Since | Description |
|:-----|:-----|:-----|:-----|:-----|
| rules | list([Convert](#convert)) | | v3.1.0 | Rename rules applied for all entities
| capitalize_first | bool | `true` | v3.1.0 | Whether to capitalize first letter ([example](#entity-filtering-and-bulk-renaming))

### Charging-state object

Note: All of these values are optional but at least `entity_id` or `state` or `attribute` is required.
Expand Down Expand Up @@ -550,6 +557,27 @@ filter:
operator: ">"
```

Bulk rename using BulkRename object to disable capitalizing the first letter of entity name (enabled by default)

```yaml
type: 'custom:battery-state-card'
title: Filters
sort: "state"
bulk_rename:
rules:
- from: "Battery Level" # simple string replace (note: "to" is not required if you want to remove string)
to: "sensor"
- from: "/\\s(temperature|temp)\\s/" # regular expression
to: " temp. "
capitalize_first: false
filter:
include: # filters for auto-adding
- name: entity_id # entities which id ends with "_battery_level"
value: "*_battery_level"
- name: attributes.device_class # and entities which device_class attribute equals "battery"
value: battery
```

### Secondary info

![image](https://user-images.githubusercontent.com/8268674/80970635-63510b80-8e13-11ea-8a9a-6bc8d873092b.png)
Expand Down
51 changes: 38 additions & 13 deletions src/entity-fields/get-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,45 @@ export const getName = (config: IBatteryEntityConfig, hass: HomeAssistant | unde
return config.entity;
}

let name = hass.states[config.entity]?.attributes.friendly_name || config.entity;

const renameRules = safeGetArray(config.bulk_rename)
renameRules.forEach(r => {
const regex = getRegexFromString(r.from);
if (regex) {
// create regexp after removing slashes
name = name.replace(regex, r.to || "");
}
else {
name = name.replace(r.from, r.to || "");
}
});
let name = hass.states[config.entity]?.attributes.friendly_name;

// when we have failed to get the name we just return entity id
if (!name) {
return config.entity;
}

// assuming it is not IBulkRename
let renameRules = <IConvert | IConvert[] | undefined>config.bulk_rename;

let capitalizeFirstLetter = true;

// testing if it's IBulkRename
if (config.bulk_rename && !Array.isArray(config.bulk_rename) && (<IConvert>config.bulk_rename)?.from === undefined) {
// we are assuming it is a IBulkRename config
const bulkRename = <IBulkRename>config.bulk_rename;

renameRules = bulkRename.rules;
capitalizeFirstLetter = bulkRename.capitalize_first !== false;
}

name = applyRenames(name, renameRules);

if (capitalizeFirstLetter) {
name = name[0].toLocaleUpperCase() + name.substring(1);
}

return name;
}

const applyRenames = (name: string, renameRules: IConvert | IConvert[] | undefined) => safeGetArray(renameRules).reduce((result, rule) => {
const regex = getRegexFromString(rule.from);
if (regex) {
// create regexp after removing slashes
result = result.replace(regex, rule.to || "");
}
else {
result = result.replace(rule.from, rule.to || "");
}

return result;
}, name)
14 changes: 13 additions & 1 deletion src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ interface IConvert {
display?: string;
}

interface IBulkRename {
/**
* Rules for replacing one value by other
*/
rules?: IConvert | IConvert[];

/**
* Whether to capitalize first letter
*/
capitalize_first?: boolean
}

/**
* Attribute
*/
Expand Down Expand Up @@ -226,7 +238,7 @@ interface IBatteryEntityConfig {
/**
* Rules for renaming entities/batteries
*/
bulk_rename?: IConvert | IConvert[];
bulk_rename?: IConvert | IConvert[] | IBulkRename;

/**
* Override for unit shown next to the value
Expand Down
15 changes: 15 additions & 0 deletions test/other/entity-fields/get-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,19 @@ describe("Get name", () => {
let result = getName({entity: "my_entity", name}, hassMock.hass);
expect(result).toBe(expectedResult);
})

test.each([
["Battery kitchen1", { from: "/Battery\\s?/", to: "" }, "Kitchen1"],
["Battery kitchen2", { rules: { from: "/Battery\\s?/", to: "" } }, "Kitchen2"],
["Battery kitchen3", { rules: { from: "/Battery\\s?/", to: "" }, capitalize_first: true }, "Kitchen3"],
["Battery kitchen4", { rules: { from: "/Battery\\s?/", to: "" }, capitalize_first: false }, "kitchen4"],
["battery kitchen5", { capitalize_first: false }, "battery kitchen5"],
["battery kitchen6", undefined, "Battery kitchen6"],
])("KString in the name", (entityName: string, renameRules: IBulkRename | IConvert | IConvert[] | undefined, expectedResult: string) => {
const hassMock = new HomeAssistantMock(true);
hassMock.addEntity("My entity", "45", { friendly_name: entityName });

let result = getName({entity: "my_entity", bulk_rename: renameRules}, hassMock.hass);
expect(result).toBe(expectedResult);
})
});
Loading