diff --git a/homeassistant/components/motion_blinds/__init__.py b/homeassistant/components/motion_blinds/__init__.py index 7b6fb0cb5dcb..d932f98f424d 100644 --- a/homeassistant/components/motion_blinds/__init__.py +++ b/homeassistant/components/motion_blinds/__init__.py @@ -105,7 +105,11 @@ async def async_setup_entry( if not await connect_gateway_class.async_connect_gateway( host, key, blind_type_list ): - raise ConfigEntryNotReady + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="cannot_connect", + translation_placeholders={"host": host}, + ) motion_gateway = connect_gateway_class.gateway_device coordinator = DataUpdateCoordinatorMotionBlinds( diff --git a/homeassistant/components/motion_blinds/gateway.py b/homeassistant/components/motion_blinds/gateway.py index 9826557919cc..910cea3aac76 100644 --- a/homeassistant/components/motion_blinds/gateway.py +++ b/homeassistant/components/motion_blinds/gateway.py @@ -50,6 +50,10 @@ class ConnectMotionGateway: ) -> bool: """Connect to the Motion Gateway.""" _LOGGER.debug("Initializing with host %s (key %s)", host, key[:3]) + if self._multicast is not None: + # A previous attempt (e.g. a config entry retry) may still hold a + # callback for this host on the shared multicast listener. + self._multicast.Unregister_motion_gateway(host) self._gateway_device = MotionGateway( ip=host, key=key, multicast=self._multicast, blind_type_list=blind_type_list ) @@ -57,7 +61,7 @@ class ConnectMotionGateway: # update device info and get the connected sub devices await self._hass.async_add_executor_job(self.update_gateway) except TimeoutError: - _LOGGER.error( + _LOGGER.debug( "Timeout trying to connect to Motion Gateway with host %s", host ) return False diff --git a/homeassistant/components/motion_blinds/strings.json b/homeassistant/components/motion_blinds/strings.json index 601e4b6579bb..dde24c02c372 100644 --- a/homeassistant/components/motion_blinds/strings.json +++ b/homeassistant/components/motion_blinds/strings.json @@ -60,6 +60,11 @@ } } }, + "exceptions": { + "cannot_connect": { + "message": "Cannot connect to Motion Gateway at {host}" + } + }, "options": { "step": { "init": { diff --git a/tests/components/motion_blinds/test_gateway.py b/tests/components/motion_blinds/test_gateway.py index 3258790b9401..6efedf217201 100644 --- a/tests/components/motion_blinds/test_gateway.py +++ b/tests/components/motion_blinds/test_gateway.py @@ -1,13 +1,20 @@ """Test the Motionblinds config flow.""" -from unittest.mock import Mock +import logging +from unittest.mock import Mock, patch from motionblinds import DEVICE_TYPES_WIFI, BlindType +import pytest -from homeassistant.components.motion_blinds.gateway import device_name +from homeassistant.components.motion_blinds.gateway import ( + ConnectMotionGateway, + device_name, +) from homeassistant.core import HomeAssistant TEST_BLIND_MAC = "abcdefghujkl0001" +TEST_HOST = "1.2.3.4" +TEST_API_KEY = "12ab345c-d67e-8f" async def test_device_name(hass: HomeAssistant) -> None: @@ -19,3 +26,33 @@ async def test_device_name(hass: HomeAssistant) -> None: blind.device_type = DEVICE_TYPES_WIFI[0] assert device_name(blind) == "RollerBlind" + + +async def test_connect_gateway_timeout_no_error_spam( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test a failed connect attempt cleans up and does not log an error. + + Regression test for https://github.com/home-assistant/core/issues/188448 . + Retried setup attempts must not spam ERROR logs, and a previous multicast + callback for the host must be unregistered before a new gateway registers. + """ + multicast = Mock() + connect = ConnectMotionGateway(hass, multicast=multicast) + + gateway = Mock() + gateway.GetDeviceList.side_effect = TimeoutError + + with ( + patch( + "homeassistant.components.motion_blinds.gateway.MotionGateway", + return_value=gateway, + ), + caplog.at_level(logging.DEBUG), + ): + assert await connect.async_connect_gateway(TEST_HOST, TEST_API_KEY) is False + + multicast.Unregister_motion_gateway.assert_called_once_with(TEST_HOST) + assert "Timeout trying to connect" in caplog.text + assert not [record for record in caplog.records if record.levelno >= logging.ERROR]