mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 18:08:37 -04:00
Add exception translation for switchbot device initialization (#144828)
This commit is contained in:
@@ -47,6 +47,14 @@ async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
|
||||
return entry
|
||||
|
||||
|
||||
def patch_async_ble_device_from_address(return_value: BluetoothServiceInfoBleak | None):
|
||||
"""Patch async ble device from address to return a given value."""
|
||||
return patch(
|
||||
"homeassistant.components.bluetooth.async_ble_device_from_address",
|
||||
return_value=return_value,
|
||||
)
|
||||
|
||||
|
||||
WOHAND_SERVICE_INFO = BluetoothServiceInfoBleak(
|
||||
name="WoHand",
|
||||
manufacturer_data={89: b"\xfd`0U\x92W"},
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
"""Test the switchbot init."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import (
|
||||
HUBMINI_MATTER_SERVICE_INFO,
|
||||
LOCK_SERVICE_INFO,
|
||||
patch_async_ble_device_from_address,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.bluetooth import inject_bluetooth_service_info
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error_message"),
|
||||
[
|
||||
(
|
||||
ValueError("wrong model"),
|
||||
"Switchbot device initialization failed because of incorrect configuration parameters: wrong model",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_exception_handling_for_device_initialization(
|
||||
hass: HomeAssistant,
|
||||
mock_entry_encrypted_factory: Callable[[str], MockConfigEntry],
|
||||
exception: Exception,
|
||||
error_message: str,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test exception handling for lock initialization."""
|
||||
inject_bluetooth_service_info(hass, LOCK_SERVICE_INFO)
|
||||
|
||||
entry = mock_entry_encrypted_factory(sensor_type="lock")
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.switchbot.lock.switchbot.SwitchbotLock.__init__",
|
||||
side_effect=exception,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert error_message in caplog.text
|
||||
|
||||
|
||||
async def test_setup_entry_without_ble_device(
|
||||
hass: HomeAssistant,
|
||||
mock_entry_factory: Callable[[str], MockConfigEntry],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test setup entry without ble device."""
|
||||
|
||||
entry = mock_entry_factory("hygrometer_co2")
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch_async_ble_device_from_address(None):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
||||
assert (
|
||||
"Could not find Switchbot hygrometer_co2 with address aa:bb:cc:dd:ee:ff"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
async def test_coordinator_wait_ready_timeout(
|
||||
hass: HomeAssistant,
|
||||
mock_entry_factory: Callable[[str], MockConfigEntry],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test the coordinator async_wait_ready timeout by calling it directly."""
|
||||
|
||||
inject_bluetooth_service_info(hass, HUBMINI_MATTER_SERVICE_INFO)
|
||||
|
||||
entry = mock_entry_factory("hubmini_matter")
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
timeout_mock = AsyncMock()
|
||||
timeout_mock.__aenter__.side_effect = TimeoutError
|
||||
timeout_mock.__aexit__.return_value = None
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.switchbot.coordinator.asyncio.timeout",
|
||||
return_value=timeout_mock,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
||||
assert "aa:bb:cc:dd:ee:ff is not advertising state" in caplog.text
|
||||
Reference in New Issue
Block a user