Skip to content

Commit

Permalink
Set mqtt entity name to null when it is a duplicate of the device n…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh authored Jul 27, 2023
1 parent c2bbb0b commit 3fcfe7d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,16 @@ def _set_entity_name(self, config: ConfigType) -> None:
"MQTT device information always needs to include a name, got %s, "
"if device information is shared between multiple entities, the device "
"name must be included in each entity's device configuration",
config,
)
elif config[CONF_DEVICE][CONF_NAME] == entity_name:
_LOGGER.warning(
"MQTT device name is equal to entity name in your config %s, "
"this is not expected. Please correct your configuration. "
"The entity name will be set to `null`",
config,
)
self._attr_name = None

def _setup_common_attributes_from_config(self, config: ConfigType) -> None:
"""(Re)Setup the common attributes for the entity."""
Expand Down
21 changes: 21 additions & 0 deletions tests/components/mqtt/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ def test_callback(event) -> None:
None,
True,
),
( # entity_name_and_device_name_the_sane
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"name": "Hello world",
"state_topic": "test-topic",
"unique_id": "veryunique",
"device_class": "humidity",
"device": {
"identifiers": ["helloworld"],
"name": "Hello world",
},
}
}
},
"sensor.hello_world",
"Hello world",
"Hello world",
False,
),
],
ids=[
"default_entity_name_without_device_name",
Expand All @@ -222,6 +242,7 @@ def test_callback(event) -> None:
"name_set_no_device_name_set",
"none_entity_name_with_device_name",
"none_entity_name_without_device_name",
"entity_name_and_device_name_the_sane",
],
)
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR])
Expand Down

0 comments on commit 3fcfe7d

Please sign in to comment.