Skip to content

Commit fdd9571

Browse files
authored
Fix MQTT entity cannot be renamed (home-assistant#157540)
1 parent 4f4c515 commit fdd9571

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

homeassistant/components/mqtt/entity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,7 @@ async def async_added_to_hass(self) -> None:
14861486
entity_registry.async_update_entity(
14871487
self.entity_id, new_entity_id=self._update_registry_entity_id
14881488
)
1489+
self._update_registry_entity_id = None
14891490

14901491
await super().async_added_to_hass()
14911492
self._subscriptions = {}

tests/components/mqtt/test_discovery.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,7 @@ async def test_discovery_with_object_id(
15841584
async def test_discovery_with_default_entity_id_for_previous_deleted_entity(
15851585
hass: HomeAssistant,
15861586
mqtt_mock_entry: MqttMockHAClientGenerator,
1587+
entity_registry: er.EntityRegistry,
15871588
) -> None:
15881589
"""Test discovering an MQTT entity with default_entity_id and unique_id."""
15891590

@@ -1598,6 +1599,7 @@ async def test_discovery_with_default_entity_id_for_previous_deleted_entity(
15981599
)
15991600
initial_entity_id = "sensor.hello_id"
16001601
new_entity_id = "sensor.updated_hello_id"
1602+
later_entity_id = "sensor.later_hello_id"
16011603
name = "Hello World 11"
16021604
domain = "sensor"
16031605

@@ -1626,6 +1628,14 @@ async def test_discovery_with_default_entity_id_for_previous_deleted_entity(
16261628
assert state.name == name
16271629
assert (domain, "object bla") in hass.data["mqtt"].discovery_already_discovered
16281630

1631+
# Assert the entity ID can be changed later
1632+
entity_registry.async_update_entity(new_entity_id, new_entity_id=later_entity_id)
1633+
await hass.async_block_till_done()
1634+
state = hass.states.get(later_entity_id)
1635+
1636+
assert state is not None
1637+
assert state.name == name
1638+
16291639

16301640
async def test_discovery_incl_nodeid(
16311641
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator

0 commit comments

Comments
 (0)