From b532161f344770fb4e8655c55da77c98ba1889b6 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Fri, 22 Dec 2023 12:21:47 +0000 Subject: [PATCH] Capitalize forst letter of entity name --- README.md | 30 ++++++++++++- src/entity-fields/get-name.ts | 51 +++++++++++++++++------ src/typings.d.ts | 14 ++++++- test/other/entity-fields/get-name.test.ts | 15 +++++++ 4 files changed, 95 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ec2f6272..f0105c40 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. @@ -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) diff --git a/src/entity-fields/get-name.ts b/src/entity-fields/get-name.ts index d8a4050b..ff5fcd33 100644 --- a/src/entity-fields/get-name.ts +++ b/src/entity-fields/get-name.ts @@ -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 = config.bulk_rename; + + let capitalizeFirstLetter = true; + + // testing if it's IBulkRename + if (config.bulk_rename && !Array.isArray(config.bulk_rename) && (config.bulk_rename)?.from === undefined) { + // we are assuming it is a IBulkRename config + const bulkRename = 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) \ No newline at end of file diff --git a/src/typings.d.ts b/src/typings.d.ts index d7a382fb..04b381a1 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -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 */ @@ -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 diff --git a/test/other/entity-fields/get-name.test.ts b/test/other/entity-fields/get-name.test.ts index b4e186b8..ef41831b 100644 --- a/test/other/entity-fields/get-name.test.ts +++ b/test/other/entity-fields/get-name.test.ts @@ -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); + }) }); \ No newline at end of file