mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Retry later if bluetooth fails to start (#75647)
This commit is contained in:
@@ -15,6 +15,7 @@ from homeassistant.components.bluetooth import (
|
||||
async_track_unavailable,
|
||||
models,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -70,10 +71,7 @@ async def test_setup_and_stop_no_bluetooth(hass, caplog):
|
||||
|
||||
async def test_setup_and_stop_broken_bluetooth(hass, caplog):
|
||||
"""Test we fail gracefully when bluetooth/dbus is broken."""
|
||||
mock_bt = [
|
||||
{"domain": "switchbot", "service_uuid": "cba20d00-224d-11e6-9fb8-0002a5d5c51b"}
|
||||
]
|
||||
|
||||
mock_bt = []
|
||||
with patch("homeassistant.components.bluetooth.HaBleakScanner.async_setup"), patch(
|
||||
"homeassistant.components.bluetooth.HaBleakScanner.start",
|
||||
side_effect=BleakError,
|
||||
@@ -93,6 +91,42 @@ async def test_setup_and_stop_broken_bluetooth(hass, caplog):
|
||||
assert len(bluetooth.async_discovered_service_info(hass)) == 0
|
||||
|
||||
|
||||
async def test_setup_and_retry_adapter_not_yet_available(hass, caplog):
|
||||
"""Test we retry if the adapter is not yet available."""
|
||||
mock_bt = []
|
||||
with patch("homeassistant.components.bluetooth.HaBleakScanner.async_setup"), patch(
|
||||
"homeassistant.components.bluetooth.HaBleakScanner.start",
|
||||
side_effect=BleakError,
|
||||
), patch(
|
||||
"homeassistant.components.bluetooth.async_get_bluetooth", return_value=mock_bt
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass, bluetooth.DOMAIN, {bluetooth.DOMAIN: {}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entry = hass.config_entries.async_entries(bluetooth.DOMAIN)[0]
|
||||
|
||||
assert "Failed to start Bluetooth" in caplog.text
|
||||
assert len(bluetooth.async_discovered_service_info(hass)) == 0
|
||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.HaBleakScanner.start",
|
||||
):
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10))
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bluetooth.HaBleakScanner.stop",
|
||||
):
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_calling_async_discovered_devices_no_bluetooth(hass, caplog):
|
||||
"""Test we fail gracefully when asking for discovered devices and there is no blueooth."""
|
||||
mock_bt = []
|
||||
|
||||
Reference in New Issue
Block a user