diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index 70b681ffbb2f60..9f0849a4d4c74b 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -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.""" diff --git a/tests/components/mqtt/test_mixins.py b/tests/components/mqtt/test_mixins.py index 5a30a3a65de052..23367d7829f216 100644 --- a/tests/components/mqtt/test_mixins.py +++ b/tests/components/mqtt/test_mixins.py @@ -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", @@ -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])