diff --git a/src/common/entity/strip_prefix_from_entity_name.ts b/src/common/entity/strip_prefix_from_entity_name.ts index 1efa3704d5..e976b7b18f 100644 --- a/src/common/entity/strip_prefix_from_entity_name.ts +++ b/src/common/entity/strip_prefix_from_entity_name.ts @@ -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}`; + + 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); + } } - const newName = entityName.substring(lowerCasedPrefixWithSpaceSuffix.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); + return undefined; }; const hasUpperCase = (str: string): boolean => str.toLowerCase() !== str; diff --git a/src/panels/config/devices/device-detail/ha-device-entities-card.ts b/src/panels/config/devices/device-detail/ha-device-entities-card.ts index 3b4d461145..ebfbd159dd 100644 --- a/src/panels/config/devices/device-detail/ha-device-entities-card.ts +++ b/src/panels/config/devices/device-detail/ha-device-entities-card.ts @@ -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; @@ -198,7 +198,7 @@ export class HaDeviceEntitiesCard extends LitElement { ${name ? stripPrefixFromEntityName( name, - `${this.deviceName} `.toLowerCase() + this.deviceName.toLowerCase() ) || name : entry.entity_id} diff --git a/src/panels/lovelace/common/generate-lovelace-config.ts b/src/panels/lovelace/common/generate-lovelace-config.ts index 2f9a9560c8..0d3fd0cb9a 100644 --- a/src/panels/lovelace/common/generate-lovelace-config.ts +++ b/src/panels/lovelace/common/generate-lovelace-config.ts @@ -97,7 +97,7 @@ export const computeCards = ( const entities: Array = []; const titlePrefix = entityCardOptions.title - ? `${entityCardOptions.title} `.toLowerCase() + ? entityCardOptions.title.toLowerCase() : undefined; const footerEntities: ButtonsHeaderFooterConfig["entities"] = [];