diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 65b60546f61..09d9e3655f0 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -6,7 +6,11 @@ from typing import Any, Final from aioshelly.block_device import BlockDevice, BlockUpdateType from aioshelly.common import ConnectionOptions -from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError +from aioshelly.exceptions import ( + DeviceConnectionError, + InvalidAuthError, + MacAddressMismatchError, +) from aioshelly.rpc_device import RpcDevice, RpcUpdateType import voluptuous as vol @@ -185,7 +189,7 @@ async def _async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> b LOGGER.debug("Setting up online block device %s", entry.title) try: await device.initialize() - except DeviceConnectionError as err: + except (DeviceConnectionError, MacAddressMismatchError) as err: raise ConfigEntryNotReady(repr(err)) from err except InvalidAuthError as err: raise ConfigEntryAuthFailed(repr(err)) from err @@ -271,7 +275,7 @@ async def _async_setup_rpc_entry(hass: HomeAssistant, entry: ConfigEntry) -> boo LOGGER.debug("Setting up online RPC device %s", entry.title) try: await device.initialize() - except DeviceConnectionError as err: + except (DeviceConnectionError, MacAddressMismatchError) as err: raise ConfigEntryNotReady(repr(err)) from err except InvalidAuthError as err: raise ConfigEntryAuthFailed(repr(err)) from err diff --git a/tests/components/shelly/test_init.py b/tests/components/shelly/test_init.py index a62dfda82f9..1fdfc9d4304 100644 --- a/tests/components/shelly/test_init.py +++ b/tests/components/shelly/test_init.py @@ -3,7 +3,11 @@ from __future__ import annotations from unittest.mock import AsyncMock, patch -from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError +from aioshelly.exceptions import ( + DeviceConnectionError, + InvalidAuthError, + MacAddressMismatchError, +) import pytest from homeassistant.components.shelly.const import ( @@ -86,6 +90,22 @@ async def test_device_connection_error( assert entry.state == ConfigEntryState.SETUP_RETRY +@pytest.mark.parametrize("gen", [1, 2]) +async def test_mac_mismatch_error( + hass: HomeAssistant, gen, mock_block_device, mock_rpc_device, monkeypatch +) -> None: + """Test device MAC address mismatch error.""" + monkeypatch.setattr( + mock_block_device, "initialize", AsyncMock(side_effect=MacAddressMismatchError) + ) + monkeypatch.setattr( + mock_rpc_device, "initialize", AsyncMock(side_effect=MacAddressMismatchError) + ) + + entry = await init_integration(hass, gen) + assert entry.state == ConfigEntryState.SETUP_RETRY + + @pytest.mark.parametrize("gen", [1, 2]) async def test_device_auth_error( hass: HomeAssistant, gen, mock_block_device, mock_rpc_device, monkeypatch