Skip to content

Commit

Permalink
Capitalize entity name in Home Assistant (#16702)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drafteed authored Feb 21, 2023
1 parent 0b6040d commit f5cceb1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1192,13 +1192,16 @@ export default class HomeAssistant extends Extension {

const devicePayload = this.getDevicePayload(entity);

// Set (unique) name, separate by space if device name contains space.
// Set (unique) name, separate and change case according to name of the device.
const nameSeparator = devicePayload.name.includes('_') ? '_' : ' ';
const isNameCapitalize = devicePayload.name[0] === devicePayload.name[0].toUpperCase();
payload.name = devicePayload.name;
if (config.object_id.startsWith(config.type) && config.object_id.includes('_')) {
payload.name += `${nameSeparator}${config.object_id.split(/_(.+)/)[1]}`;
const name = config.object_id.split(/_(.+)/)[1];
payload.name += `${nameSeparator}${isNameCapitalize ? utils.capitalize(name) : name}`;
} else if (!config.object_id.startsWith(config.type)) {
payload.name += `${nameSeparator}${config.object_id.replace(/_/g, nameSeparator)}`;
const name = config.object_id.replace(/_/g, nameSeparator);
payload.name += `${nameSeparator}${isNameCapitalize ? utils.capitalize(name) : name}`;
}

// Set unique_id
Expand Down
4 changes: 2 additions & 2 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ describe('HomeAssistant extension', () => {
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
'name': 'Weather Sensor temperature',
'name': 'Weather Sensor Temperature',
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
'device': {
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
Expand All @@ -611,7 +611,7 @@ describe('HomeAssistant extension', () => {
'value_template': '{{ value_json.humidity }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
'name': 'Weather Sensor humidity',
'name': 'Weather Sensor Humidity',
'unique_id': '0x0017880104e45522_humidity_zigbee2mqtt',
'enabled_by_default': true,
'device': {
Expand Down

0 comments on commit f5cceb1

Please sign in to comment.