From 87318c91119093e01f08b551211ce3ee83660c0c Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Sat, 9 Mar 2024 11:54:27 +0100 Subject: [PATCH] Cleanup mqtt discovery code (#112749) * Cleanup mqtt discovery code * Cleanup mqtt discovery code --- homeassistant/components/mqtt/__init__.py | 2 +- homeassistant/components/mqtt/discovery.py | 9 +++------ tests/components/mqtt/test_discovery.py | 9 +++------ 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 14f92e29cdf..0bf2d8f53e1 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -463,7 +463,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Setup discovery if conf.get(CONF_DISCOVERY, DEFAULT_DISCOVERY): await discovery.async_start( - hass, conf.get(CONF_DISCOVERY_PREFIX, DEFAULT_PREFIX), entry + hass, conf.get(CONF_DISCOVERY_PREFIX, DEFAULT_PREFIX) ) # Setup reload service after all platforms have loaded await async_setup_reload_service() diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 5fa1b6297d7..806059b77f9 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -12,7 +12,6 @@ from typing import TYPE_CHECKING, Any import voluptuous as vol -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_DEVICE, CONF_NAME, CONF_PLATFORM from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResultType @@ -137,11 +136,10 @@ def async_log_discovery_origin_info( async def async_start( # noqa: C901 - hass: HomeAssistant, discovery_topic: str, config_entry: ConfigEntry + hass: HomeAssistant, discovery_topic: str ) -> None: """Start MQTT Discovery.""" mqtt_data = get_mqtt_data(hass) - mqtt_integrations = {} @callback def async_discovery_message_received(msg: ReceiveMessage) -> None: # noqa: C901 @@ -155,9 +153,8 @@ async def async_start( # noqa: C901 if topic_trimmed.endswith("config"): _LOGGER.warning( ( - "Received message on illegal discovery topic '%s'. The topic" - " contains " - "not allowed characters. For more information see " + "Received message on illegal discovery topic '%s'. The topic " + "contains not allowed characters. For more information see " "https://www.home-assistant.io/integrations/mqtt/#discovery-topic" ), topic, diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index 6121e1dc15e..5249789a13f 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -55,10 +55,9 @@ async def test_subscribing_config_topic( ) -> None: """Test setting up discovery.""" mqtt_mock = await mqtt_mock_entry() - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] discovery_topic = "homeassistant" - await async_start(hass, discovery_topic, entry) + await async_start(hass, discovery_topic) call_args1 = mqtt_mock.async_subscribe.mock_calls[0][1] assert call_args1[2] == 0 @@ -1503,14 +1502,13 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe( mqtt_mock = await mqtt_mock_entry() mock_platform(hass, "comp.config_flow", None) - entry = hass.config_entries.async_entries("mqtt")[0] mqtt_mock().connected = True with patch( "homeassistant.components.mqtt.discovery.async_get_mqtt", return_value={"comp": ["comp/discovery/#"]}, ): - await async_start(hass, "homeassistant", entry) + await async_start(hass, "homeassistant") await hass.async_block_till_done() await hass.async_block_till_done() @@ -1553,14 +1551,13 @@ async def test_mqtt_discovery_unsubscribe_once( mqtt_mock = await mqtt_mock_entry() mock_platform(hass, "comp.config_flow", None) - entry = hass.config_entries.async_entries("mqtt")[0] mqtt_mock().connected = True with patch( "homeassistant.components.mqtt.discovery.async_get_mqtt", return_value={"comp": ["comp/discovery/#"]}, ): - await async_start(hass, "homeassistant", entry) + await async_start(hass, "homeassistant") await hass.async_block_till_done() await hass.async_block_till_done()