-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
Fix race when handling rapid succession of MQTT discovery messages #68785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
361fcfb
Fix race when handling rapid succession of MQTT discovery messages
emontnemery c28d9dd
Fix try_connection_mock
jbouwh 0a20f28
Scene send discovery done signal
jbouwh 013d697
move to async_added_to_hass
jbouwh 762bcaf
Don't allow overriding async_added_to_hass in MQTT entities
emontnemery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -550,6 +550,58 @@ def callback(event): | |||
| assert events[3].data["old_state"] is None | ||||
|
|
||||
|
|
||||
| async def test_rapid_reconfigure(hass, mqtt_mock, caplog): | ||||
| """Test immediate reconfigure of added component.""" | ||||
|
|
||||
| events = [] | ||||
|
|
||||
| @ha.callback | ||||
| def callback(event): | ||||
| """Verify event got called.""" | ||||
| events.append(event) | ||||
|
|
||||
| hass.bus.async_listen(EVENT_STATE_CHANGED, callback) | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have this helper: Line 1184 in 44d8f2f
|
||||
|
|
||||
| # Discovery immediately followed by reconfig | ||||
| async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla/config", "") | ||||
| async_fire_mqtt_message( | ||||
| hass, | ||||
| "homeassistant/binary_sensor/bla/config", | ||||
| '{ "name": "Beer", "state_topic": "test-topic1" }', | ||||
| ) | ||||
| async_fire_mqtt_message( | ||||
| hass, | ||||
| "homeassistant/binary_sensor/bla/config", | ||||
| '{ "name": "Milk", "state_topic": "test-topic2" }', | ||||
| ) | ||||
| async_fire_mqtt_message( | ||||
| hass, | ||||
| "homeassistant/binary_sensor/bla/config", | ||||
| '{ "name": "Wine", "state_topic": "test-topic3" }', | ||||
| ) | ||||
| await hass.async_block_till_done() | ||||
|
|
||||
| assert len(hass.states.async_entity_ids("binary_sensor")) == 1 | ||||
| state = hass.states.get("binary_sensor.beer") | ||||
| assert state is not None | ||||
|
|
||||
| assert len(events) == 3 | ||||
| # Add the entity | ||||
| assert events[0].data["entity_id"] == "binary_sensor.beer" | ||||
| assert events[0].data["old_state"] is None | ||||
| assert events[0].data["new_state"].attributes["friendly_name"] == "Beer" | ||||
| # Update the entity | ||||
| assert events[1].data["entity_id"] == "binary_sensor.beer" | ||||
| assert events[1].data["new_state"] is not None | ||||
| assert events[1].data["old_state"] is not None | ||||
| assert events[1].data["new_state"].attributes["friendly_name"] == "Milk" | ||||
| # Update the entity | ||||
| assert events[2].data["entity_id"] == "binary_sensor.beer" | ||||
| assert events[2].data["new_state"] is not None | ||||
| assert events[2].data["old_state"] is not None | ||||
| assert events[2].data["new_state"].attributes["friendly_name"] == "Wine" | ||||
|
|
||||
|
|
||||
| async def test_duplicate_removal(hass, mqtt_mock, caplog): | ||||
| """Test for a non duplicate component.""" | ||||
| async_fire_mqtt_message( | ||||
|
|
||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async_added_to_hass is overrided called from sensor and binary_sensor. These overrides should process the restore before calling super().async_added_to_hass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be async_added_to_hass is also called when a config_entry is added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems no issue since
self.async_send_discovery_done()is not doing anything when the entity is restored, only when it is discovered.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MQTT entities are only added when they are discovered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct