Reduce Motionblinds error log spam on retries (#188456)

This commit is contained in:
suraj_velip
2026-10-25 16:58:27 +01:00
committed by GitHub
parent f450fd0715
commit d140cb04bb
4 changed files with 54 additions and 4 deletions
@@ -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(
@@ -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
@@ -60,6 +60,11 @@
}
}
},
"exceptions": {
"cannot_connect": {
"message": "Cannot connect to Motion Gateway at {host}"
}
},
"options": {
"step": {
"init": {
+39 -2
View File
@@ -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]