Abort Shelly setup if MAC address mismatch (#98807)

pull/98784/head^2
Shay Levy 2023-08-22 15:41:50 +03:00 committed by GitHub
parent 097c7fbfef
commit 406f06f0fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -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

View File

@ -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