Skip to content
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: 19 additions & 11 deletions src/common/entity/strip_prefix_from_entity_name.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
const SUFFIXES = [" ", ": "];

/**
* Strips a device name from an entity name.
* @param entityName the entity name
* @param lowerCasedPrefixWithSpaceSuffix the prefix to strip, lower cased with a space suffix
* @param lowerCasedPrefix the prefix to strip, lower cased
* @returns
*/
export const stripPrefixFromEntityName = (
entityName: string,
lowerCasedPrefixWithSpaceSuffix: string
lowerCasedPrefix: string
) => {
if (!entityName.toLowerCase().startsWith(lowerCasedPrefixWithSpaceSuffix)) {
return undefined;
}
const lowerCasedEntityName = entityName.toLowerCase();

for (const suffix of SUFFIXES) {
const lowerCasedPrefixWithSuffix = `${lowerCasedPrefix}${suffix}`;

const newName = entityName.substring(lowerCasedPrefixWithSpaceSuffix.length);
if (lowerCasedEntityName.startsWith(lowerCasedPrefixWithSuffix)) {
const newName = entityName.substring(lowerCasedPrefixWithSuffix.length);

// If first word already has an upper case letter (e.g. from brand name)
// leave as-is, otherwise capitalize the first word.
return hasUpperCase(newName.substr(0, newName.indexOf(" ")))
? newName
: newName[0].toUpperCase() + newName.slice(1);
}
}

// If first word already has an upper case letter (e.g. from brand name)
// leave as-is, otherwise capitalize the first word.
return hasUpperCase(newName.substr(0, newName.indexOf(" ")))
? newName
: newName[0].toUpperCase() + newName.slice(1);
return undefined;
};

const hasUpperCase = (str: string): boolean => str.toLowerCase() !== str;
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class HaDeviceEntitiesCard extends LitElement {
const stateObj = this.hass.states[entry.entity_id];
const name = stripPrefixFromEntityName(
computeStateName(stateObj),
`${this.deviceName} `.toLowerCase()
this.deviceName.toLowerCase()
);
if (name) {
config.name = name;
Expand Down Expand Up @@ -198,7 +198,7 @@ export class HaDeviceEntitiesCard extends LitElement {
${name
? stripPrefixFromEntityName(
name,
`${this.deviceName} `.toLowerCase()
this.deviceName.toLowerCase()
) || name
: entry.entity_id}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/common/generate-lovelace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const computeCards = (
const entities: Array<string | LovelaceRowConfig> = [];

const titlePrefix = entityCardOptions.title
? `${entityCardOptions.title} `.toLowerCase()
? entityCardOptions.title.toLowerCase()
: undefined;

const footerEntities: ButtonsHeaderFooterConfig["entities"] = [];
Expand Down