Fix entity name attribute on mqtt entity is not removed on update (#100187)
Fix entity name attribute is not remove on updatepull/100207/head
parent
76c3a638c4
commit
6b265120b3
|
@ -1137,6 +1137,11 @@ class MqttEntity(
|
|||
elif not self._default_to_device_class_name():
|
||||
# Assign the default name
|
||||
self._attr_name = self._default_name
|
||||
elif hasattr(self, "_attr_name"):
|
||||
# An entity name was not set in the config
|
||||
# don't set the name attribute and derive
|
||||
# the name from the device_class
|
||||
delattr(self, "_attr_name")
|
||||
if CONF_DEVICE in config:
|
||||
device_name: str
|
||||
if CONF_NAME not in config[CONF_DEVICE]:
|
||||
|
|
|
@ -7,6 +7,7 @@ import pytest
|
|||
from homeassistant.components import mqtt, sensor
|
||||
from homeassistant.components.mqtt.sensor import DEFAULT_NAME as DEFAULT_SENSOR_NAME
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
EVENT_STATE_CHANGED,
|
||||
Platform,
|
||||
|
@ -324,7 +325,6 @@ async def test_default_entity_and_device_name(
|
|||
|
||||
This is a test helper for the _setup_common_attributes_from_config mixin.
|
||||
"""
|
||||
# mqtt_mock = await mqtt_mock_entry()
|
||||
|
||||
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
|
||||
hass.state = CoreState.starting
|
||||
|
@ -352,3 +352,61 @@ async def test_default_entity_and_device_name(
|
|||
|
||||
# Assert that an issues ware registered
|
||||
assert len(events) == issue_events
|
||||
|
||||
|
||||
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR])
|
||||
async def test_name_attribute_is_set_or_not(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
) -> None:
|
||||
"""Test frendly name with device_class set.
|
||||
|
||||
This is a test helper for the _setup_common_attributes_from_config mixin.
|
||||
"""
|
||||
await mqtt_mock_entry()
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
"homeassistant/binary_sensor/bla/config",
|
||||
'{ "name": "Gate", "state_topic": "test-topic", "device_class": "door", '
|
||||
'"object_id": "gate",'
|
||||
'"device": {"identifiers": "very_unique", "name": "xyz_door_sensor"}'
|
||||
"}",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.gate")
|
||||
|
||||
assert state is not None
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Gate"
|
||||
|
||||
# Remove the name in a discovery update
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
"homeassistant/binary_sensor/bla/config",
|
||||
'{ "state_topic": "test-topic", "device_class": "door", '
|
||||
'"object_id": "gate",'
|
||||
'"device": {"identifiers": "very_unique", "name": "xyz_door_sensor"}'
|
||||
"}",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.gate")
|
||||
|
||||
assert state is not None
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Door"
|
||||
|
||||
# Set the name to `null` in a discovery update
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
"homeassistant/binary_sensor/bla/config",
|
||||
'{ "name": null, "state_topic": "test-topic", "device_class": "door", '
|
||||
'"object_id": "gate",'
|
||||
'"device": {"identifiers": "very_unique", "name": "xyz_door_sensor"}'
|
||||
"}",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.gate")
|
||||
|
||||
assert state is not None
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) is None
|
||||
|
|
Loading…
Reference in New Issue