Do not unsubscribe mqtt integration discovery if entry is already configured (#126907)

* Do not unsubscribe mqtt integration discovery if entry is already configured

* Test cases without unsubscribe
pull/126782/head
Jan Bouwhuis 2024-09-27 17:28:51 +02:00 committed by Franck Nijhof
parent 57028a0807
commit b606b50cec
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 25 additions and 15 deletions

View File

@ -393,8 +393,7 @@ async def async_start( # noqa: C901
if ( if (
result result
and result["type"] == FlowResultType.ABORT and result["type"] == FlowResultType.ABORT
and result["reason"] and result["reason"] == "single_instance_allowed"
in ("already_configured", "single_instance_allowed")
): ):
integration_unsubscribe.pop(key)() integration_unsubscribe.pop(key)()

View File

@ -1444,8 +1444,19 @@ async def test_complex_discovery_topic_prefix(
@patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0) @patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0) @patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0)
@patch("homeassistant.components.mqtt.client.UNSUBSCRIBE_COOLDOWN", 0.0) @patch("homeassistant.components.mqtt.client.UNSUBSCRIBE_COOLDOWN", 0.0)
@pytest.mark.parametrize(
("reason", "unsubscribes"),
[
("single_instance_allowed", True),
("already_configured", False),
("some_abort_error", False),
],
)
async def test_mqtt_integration_discovery_subscribe_unsubscribe( async def test_mqtt_integration_discovery_subscribe_unsubscribe(
hass: HomeAssistant, mqtt_client_mock: MqttMockPahoClient hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient,
reason: str,
unsubscribes: bool,
) -> None: ) -> None:
"""Check MQTT integration discovery subscribe and unsubscribe.""" """Check MQTT integration discovery subscribe and unsubscribe."""
@ -1454,7 +1465,7 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
async def async_step_mqtt(self, discovery_info: MqttServiceInfo) -> FlowResult: async def async_step_mqtt(self, discovery_info: MqttServiceInfo) -> FlowResult:
"""Test mqtt step.""" """Test mqtt step."""
return self.async_abort(reason="already_configured") return self.async_abort(reason=reason)
mock_platform(hass, "comp.config_flow", None) mock_platform(hass, "comp.config_flow", None)
@ -1465,13 +1476,6 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
"""Handle birth message.""" """Handle birth message."""
birth.set() birth.set()
wait_unsub = asyncio.Event()
@callback
def _mock_unsubscribe(topics: list[str]) -> tuple[int, int]:
wait_unsub.set()
return (0, 0)
entry = MockConfigEntry(domain=mqtt.DOMAIN, data=ENTRY_DEFAULT_BIRTH_MESSAGE) entry = MockConfigEntry(domain=mqtt.DOMAIN, data=ENTRY_DEFAULT_BIRTH_MESSAGE)
entry.add_to_hass(hass) entry.add_to_hass(hass)
with ( with (
@ -1480,7 +1484,6 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
return_value={"comp": ["comp/discovery/#"]}, return_value={"comp": ["comp/discovery/#"]},
), ),
mock_config_flow("comp", TestFlow), mock_config_flow("comp", TestFlow),
patch.object(mqtt_client_mock, "unsubscribe", side_effect=_mock_unsubscribe),
): ):
assert await hass.config_entries.async_setup(entry.entry_id) assert await hass.config_entries.async_setup(entry.entry_id)
await mqtt.async_subscribe(hass, "homeassistant/status", wait_birth) await mqtt.async_subscribe(hass, "homeassistant/status", wait_birth)
@ -1493,8 +1496,16 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe(
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "") async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
await wait_unsub.wait() await hass.async_block_till_done()
mqtt_client_mock.unsubscribe.assert_called_once_with(["comp/discovery/#"]) await hass.async_block_till_done(wait_background_tasks=True)
assert (
unsubscribes
and call(["comp/discovery/#"]) in mqtt_client_mock.unsubscribe.mock_calls
or not unsubscribes
and call(["comp/discovery/#"])
not in mqtt_client_mock.unsubscribe.mock_calls
)
await hass.async_block_till_done(wait_background_tasks=True) await hass.async_block_till_done(wait_background_tasks=True)
@ -1513,7 +1524,7 @@ async def test_mqtt_discovery_unsubscribe_once(
async def async_step_mqtt(self, discovery_info: MqttServiceInfo) -> FlowResult: async def async_step_mqtt(self, discovery_info: MqttServiceInfo) -> FlowResult:
"""Test mqtt step.""" """Test mqtt step."""
await asyncio.sleep(0) await asyncio.sleep(0)
return self.async_abort(reason="already_configured") return self.async_abort(reason="single_instance_allowed")
mock_platform(hass, "comp.config_flow", None) mock_platform(hass, "comp.config_flow", None)