Move mock_bluetooth* fixtures to decorator (#118846)

This commit is contained in:
epenet 2024-06-06 17:31:08 +02:00 committed by GitHub
parent 33ed4fd862
commit 632238a7f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 113 additions and 214 deletions

View file

@ -9,6 +9,7 @@ from typing import Any
from unittest.mock import MagicMock from unittest.mock import MagicMock
from bleak.exc import BleakError from bleak.exc import BleakError
import pytest
from homeassistant.components.bluetooth import ( from homeassistant.components.bluetooth import (
DOMAIN, DOMAIN,
@ -96,11 +97,8 @@ class MyCoordinator(ActiveBluetoothDataUpdateCoordinator[dict[str, Any]]):
super()._async_handle_bluetooth_event(service_info, change) super()._async_handle_bluetooth_event(service_info, change)
async def test_basic_usage( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_basic_usage(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the ActiveBluetoothDataUpdateCoordinator.""" """Test basic usage of the ActiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -136,11 +134,8 @@ async def test_basic_usage(
unregister_listener() unregister_listener()
async def test_bleak_error_during_polling( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_bleak_error_during_polling(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test bleak error during polling ActiveBluetoothDataUpdateCoordinator.""" """Test bleak error during polling ActiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
poll_count = 0 poll_count = 0
@ -189,11 +184,8 @@ async def test_bleak_error_during_polling(
unregister_listener() unregister_listener()
async def test_generic_exception_during_polling( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_generic_exception_during_polling(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test generic exception during polling ActiveBluetoothDataUpdateCoordinator.""" """Test generic exception during polling ActiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
poll_count = 0 poll_count = 0
@ -242,11 +234,8 @@ async def test_generic_exception_during_polling(
unregister_listener() unregister_listener()
async def test_polling_debounce( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_polling_debounce(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the ActiveBluetoothDataUpdateCoordinator.""" """Test basic usage of the ActiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
poll_count = 0 poll_count = 0
@ -288,11 +277,8 @@ async def test_polling_debounce(
unregister_listener() unregister_listener()
async def test_polling_debounce_with_custom_debouncer( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_polling_debounce_with_custom_debouncer(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the ActiveBluetoothDataUpdateCoordinator.""" """Test basic usage of the ActiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
poll_count = 0 poll_count = 0
@ -337,11 +323,8 @@ async def test_polling_debounce_with_custom_debouncer(
unregister_listener() unregister_listener()
async def test_polling_rejecting_the_first_time( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_polling_rejecting_the_first_time(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test need_poll rejects the first time ActiveBluetoothDataUpdateCoordinator.""" """Test need_poll rejects the first time ActiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
attempt = 0 attempt = 0
@ -399,11 +382,8 @@ async def test_polling_rejecting_the_first_time(
unregister_listener() unregister_listener()
async def test_no_polling_after_stop_event( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_no_polling_after_stop_event(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test we do not poll after the stop event.""" """Test we do not poll after the stop event."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
needs_poll_calls = 0 needs_poll_calls = 0

View file

@ -49,11 +49,8 @@ GENERIC_BLUETOOTH_SERVICE_INFO_2 = BluetoothServiceInfo(
) )
async def test_basic_usage( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_basic_usage(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the ActiveBluetoothProcessorCoordinator.""" """Test basic usage of the ActiveBluetoothProcessorCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -98,11 +95,8 @@ async def test_basic_usage(
cancel() cancel()
async def test_poll_can_be_skipped( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_poll_can_be_skipped(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test need_poll callback works and can skip a poll if its not needed.""" """Test need_poll callback works and can skip a poll if its not needed."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -157,11 +151,9 @@ async def test_poll_can_be_skipped(
cancel() cancel()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_bleak_error_and_recover( async def test_bleak_error_and_recover(
hass: HomeAssistant, hass: HomeAssistant, caplog: pytest.LogCaptureFixture
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test bleak error handling and recovery.""" """Test bleak error handling and recovery."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -222,11 +214,8 @@ async def test_bleak_error_and_recover(
cancel() cancel()
async def test_poll_failure_and_recover( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_poll_failure_and_recover(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test error handling and recovery.""" """Test error handling and recovery."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -281,11 +270,8 @@ async def test_poll_failure_and_recover(
cancel() cancel()
async def test_second_poll_needed( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_second_poll_needed(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""If a poll is queued, by the time it starts it may no longer be needed.""" """If a poll is queued, by the time it starts it may no longer be needed."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -332,11 +318,8 @@ async def test_second_poll_needed(
cancel() cancel()
async def test_rate_limit( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_rate_limit(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test error handling and recovery.""" """Test error handling and recovery."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -384,11 +367,8 @@ async def test_rate_limit(
cancel() cancel()
async def test_no_polling_after_stop_event( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_no_polling_after_stop_event(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test we do not poll after the stop event.""" """Test we do not poll after the stop event."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
needs_poll_calls = 0 needs_poll_calls = 0

View file

@ -1,6 +1,6 @@
"""Test the bluetooth config flow.""" """Test the bluetooth config flow."""
from unittest.mock import MagicMock, patch from unittest.mock import patch
from bluetooth_adapters import DEFAULT_ADDRESS, AdapterDetails from bluetooth_adapters import DEFAULT_ADDRESS, AdapterDetails
import pytest import pytest
@ -20,12 +20,11 @@ from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator from tests.typing import WebSocketGenerator
@pytest.mark.usefixtures("macos_adapter") @pytest.mark.usefixtures(
"macos_adapter", "mock_bleak_scanner_start", "mock_bluetooth_adapters"
)
async def test_options_flow_disabled_not_setup( async def test_options_flow_disabled_not_setup(
hass: HomeAssistant, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
hass_ws_client: WebSocketGenerator,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test options are disabled if the integration has not been setup.""" """Test options are disabled if the integration has not been setup."""
await async_setup_component(hass, "config", {}) await async_setup_component(hass, "config", {})
@ -338,12 +337,10 @@ async def test_async_step_integration_discovery_already_exists(
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("one_adapter") @pytest.mark.usefixtures(
async def test_options_flow_linux( "one_adapter", "mock_bleak_scanner_start", "mock_bluetooth_adapters"
hass: HomeAssistant, )
mock_bleak_scanner_start: MagicMock, async def test_options_flow_linux(hass: HomeAssistant) -> None:
mock_bluetooth_adapters: None,
) -> None:
"""Test options on Linux.""" """Test options on Linux."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -392,12 +389,11 @@ async def test_options_flow_linux(
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.usefixtures("macos_adapter") @pytest.mark.usefixtures(
"macos_adapter", "mock_bleak_scanner_start", "mock_bluetooth_adapters"
)
async def test_options_flow_disabled_macos( async def test_options_flow_disabled_macos(
hass: HomeAssistant, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
hass_ws_client: WebSocketGenerator,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test options are disabled on MacOS.""" """Test options are disabled on MacOS."""
await async_setup_component(hass, "config", {}) await async_setup_component(hass, "config", {})
@ -422,12 +418,11 @@ async def test_options_flow_disabled_macos(
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.usefixtures("one_adapter") @pytest.mark.usefixtures(
"one_adapter", "mock_bleak_scanner_start", "mock_bluetooth_adapters"
)
async def test_options_flow_enabled_linux( async def test_options_flow_enabled_linux(
hass: HomeAssistant, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
hass_ws_client: WebSocketGenerator,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test options are enabled on Linux.""" """Test options are enabled on Linux."""
await async_setup_component(hass, "config", {}) await async_setup_component(hass, "config", {})

View file

@ -237,12 +237,11 @@ async def test_diagnostics(
@patch("homeassistant.components.bluetooth.HaScanner", FakeHaScanner) @patch("homeassistant.components.bluetooth.HaScanner", FakeHaScanner)
@pytest.mark.usefixtures("macos_adapter") @pytest.mark.usefixtures(
"macos_adapter", "mock_bleak_scanner_start", "mock_bluetooth_adapters"
)
async def test_diagnostics_macos( async def test_diagnostics_macos(
hass: HomeAssistant, hass: HomeAssistant, hass_client: ClientSessionGenerator
hass_client: ClientSessionGenerator,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test diagnostics for macos.""" """Test diagnostics for macos."""
# Normally we do not want to patch our classes, but since bleak will import # Normally we do not want to patch our classes, but since bleak will import
@ -414,12 +413,14 @@ async def test_diagnostics_macos(
@patch("homeassistant.components.bluetooth.HaScanner", FakeHaScanner) @patch("homeassistant.components.bluetooth.HaScanner", FakeHaScanner)
@pytest.mark.usefixtures("enable_bluetooth", "one_adapter") @pytest.mark.usefixtures(
"enable_bluetooth",
"one_adapter",
"mock_bleak_scanner_start",
"mock_bluetooth_adapters",
)
async def test_diagnostics_remote_adapter( async def test_diagnostics_remote_adapter(
hass: HomeAssistant, hass: HomeAssistant, hass_client: ClientSessionGenerator
hass_client: ClientSessionGenerator,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test diagnostics for remote adapter.""" """Test diagnostics for remote adapter."""
manager = _get_manager() manager = _get_manager()

View file

@ -432,11 +432,11 @@ async def test_discovery_match_by_service_uuid(
} }
], ],
) )
@pytest.mark.usefixtures("mock_bluetooth_adapters")
async def test_discovery_match_by_service_uuid_and_short_local_name( async def test_discovery_match_by_service_uuid_and_short_local_name(
mock_async_get_bluetooth: AsyncMock, mock_async_get_bluetooth: AsyncMock,
hass: HomeAssistant, hass: HomeAssistant,
mock_bleak_scanner_start: MagicMock, mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test bluetooth discovery match by service_uuid and short local name.""" """Test bluetooth discovery match by service_uuid and short local name."""
entry = MockConfigEntry(domain="bluetooth", unique_id="00:00:00:00:00:01") entry = MockConfigEntry(domain="bluetooth", unique_id="00:00:00:00:00:01")

View file

@ -744,8 +744,9 @@ async def test_switching_adapters_when_one_stop_scanning(
cancel_hci2() cancel_hci2()
@pytest.mark.usefixtures("mock_bluetooth_adapters")
async def test_goes_unavailable_connectable_only_and_recovers( async def test_goes_unavailable_connectable_only_and_recovers(
hass: HomeAssistant, mock_bluetooth_adapters: None hass: HomeAssistant,
) -> None: ) -> None:
"""Test all connectable scanners go unavailable, and than recover when there is a non-connectable scanner.""" """Test all connectable scanners go unavailable, and than recover when there is a non-connectable scanner."""
assert await async_setup_component(hass, bluetooth.DOMAIN, {}) assert await async_setup_component(hass, bluetooth.DOMAIN, {})
@ -907,8 +908,9 @@ async def test_goes_unavailable_connectable_only_and_recovers(
unsetup_not_connectable_scanner() unsetup_not_connectable_scanner()
@pytest.mark.usefixtures("mock_bluetooth_adapters")
async def test_goes_unavailable_dismisses_discovery_and_makes_discoverable( async def test_goes_unavailable_dismisses_discovery_and_makes_discoverable(
hass: HomeAssistant, mock_bluetooth_adapters: None hass: HomeAssistant,
) -> None: ) -> None:
"""Test that unavailable will dismiss any active discoveries and make device discoverable again.""" """Test that unavailable will dismiss any active discoveries and make device discoverable again."""
mock_bt = [ mock_bt = [

View file

@ -8,6 +8,8 @@ import time
from typing import Any from typing import Any
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.bluetooth import ( from homeassistant.components.bluetooth import (
DOMAIN, DOMAIN,
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS, FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
@ -65,11 +67,8 @@ class MyCoordinator(PassiveBluetoothDataUpdateCoordinator):
super()._async_handle_bluetooth_event(service_info, change) super()._async_handle_bluetooth_event(service_info, change)
async def test_basic_usage( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_basic_usage(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the PassiveBluetoothDataUpdateCoordinator.""" """Test basic usage of the PassiveBluetoothDataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
coordinator = MyCoordinator( coordinator = MyCoordinator(
@ -97,10 +96,9 @@ async def test_basic_usage(
cancel() cancel()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_context_compatiblity_with_data_update_coordinator( async def test_context_compatiblity_with_data_update_coordinator(
hass: HomeAssistant, hass: HomeAssistant,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test contexts can be passed for compatibility with DataUpdateCoordinator.""" """Test contexts can be passed for compatibility with DataUpdateCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -135,10 +133,9 @@ async def test_context_compatiblity_with_data_update_coordinator(
assert not set(coordinator.async_contexts()) assert not set(coordinator.async_contexts())
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_unavailable_callbacks_mark_the_coordinator_unavailable( async def test_unavailable_callbacks_mark_the_coordinator_unavailable(
hass: HomeAssistant, hass: HomeAssistant,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test that the coordinator goes unavailable when the bluetooth stack no longer sees the device.""" """Test that the coordinator goes unavailable when the bluetooth stack no longer sees the device."""
start_monotonic = time.monotonic() start_monotonic = time.monotonic()
@ -196,11 +193,8 @@ async def test_unavailable_callbacks_mark_the_coordinator_unavailable(
assert coordinator.available is False assert coordinator.available is False
async def test_passive_bluetooth_coordinator_entity( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_passive_bluetooth_coordinator_entity(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test integration of PassiveBluetoothDataUpdateCoordinator with PassiveBluetoothCoordinatorEntity.""" """Test integration of PassiveBluetoothDataUpdateCoordinator with PassiveBluetoothCoordinatorEntity."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
coordinator = MyCoordinator( coordinator = MyCoordinator(

View file

@ -174,11 +174,8 @@ GENERIC_PASSIVE_BLUETOOTH_DATA_UPDATE_WITH_DEVICE_NAME_AND_TEMP_CHANGE = (
) )
async def test_basic_usage( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_basic_usage(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the PassiveBluetoothProcessorCoordinator.""" """Test basic usage of the PassiveBluetoothProcessorCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -276,10 +273,9 @@ async def test_basic_usage(
cancel_coordinator() cancel_coordinator()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_entity_key_is_dispatched_on_entity_key_change( async def test_entity_key_is_dispatched_on_entity_key_change(
hass: HomeAssistant, hass: HomeAssistant,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test entity key listeners are only dispatched on change.""" """Test entity key listeners are only dispatched on change."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -398,11 +394,8 @@ async def test_entity_key_is_dispatched_on_entity_key_change(
cancel_coordinator() cancel_coordinator()
async def test_unavailable_after_no_data( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_unavailable_after_no_data(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test that the coordinator is unavailable after no data for a while.""" """Test that the coordinator is unavailable after no data for a while."""
start_monotonic = time.monotonic() start_monotonic = time.monotonic()
@ -513,11 +506,8 @@ async def test_unavailable_after_no_data(
cancel_coordinator() cancel_coordinator()
async def test_no_updates_once_stopping( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_no_updates_once_stopping(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test updates are ignored once hass is stopping.""" """Test updates are ignored once hass is stopping."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -570,11 +560,9 @@ async def test_no_updates_once_stopping(
cancel_coordinator() cancel_coordinator()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_exception_from_update_method( async def test_exception_from_update_method(
hass: HomeAssistant, hass: HomeAssistant, caplog: pytest.LogCaptureFixture
caplog: pytest.LogCaptureFixture,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test we handle exceptions from the update method.""" """Test we handle exceptions from the update method."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -639,11 +627,8 @@ async def test_exception_from_update_method(
cancel_coordinator() cancel_coordinator()
async def test_bad_data_from_update_method( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_bad_data_from_update_method(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test we handle bad data from the update method.""" """Test we handle bad data from the update method."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -996,11 +981,8 @@ GOVEE_B5178_PRIMARY_AND_REMOTE_PASSIVE_BLUETOOTH_DATA_UPDATE = (
) )
async def test_integration_with_entity( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_integration_with_entity(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test integration of PassiveBluetoothProcessorCoordinator with PassiveBluetoothCoordinatorEntity.""" """Test integration of PassiveBluetoothProcessorCoordinator with PassiveBluetoothCoordinatorEntity."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -1158,11 +1140,8 @@ NO_DEVICES_PASSIVE_BLUETOOTH_DATA_UPDATE = PassiveBluetoothDataUpdate(
) )
async def test_integration_with_entity_without_a_device( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_integration_with_entity_without_a_device(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test integration with PassiveBluetoothCoordinatorEntity with no device.""" """Test integration with PassiveBluetoothCoordinatorEntity with no device."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -1224,10 +1203,9 @@ async def test_integration_with_entity_without_a_device(
cancel_coordinator() cancel_coordinator()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_passive_bluetooth_entity_with_entity_platform( async def test_passive_bluetooth_entity_with_entity_platform(
hass: HomeAssistant, hass: HomeAssistant,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test with a mock entity platform.""" """Test with a mock entity platform."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -1331,11 +1309,8 @@ DEVICE_ONLY_PASSIVE_BLUETOOTH_DATA_UPDATE = PassiveBluetoothDataUpdate(
) )
async def test_integration_multiple_entity_platforms( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_integration_multiple_entity_platforms(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test integration of PassiveBluetoothProcessorCoordinator with multiple platforms.""" """Test integration of PassiveBluetoothProcessorCoordinator with multiple platforms."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -1426,11 +1401,9 @@ async def test_integration_multiple_entity_platforms(
cancel_coordinator() cancel_coordinator()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_exception_from_coordinator_update_method( async def test_exception_from_coordinator_update_method(
hass: HomeAssistant, hass: HomeAssistant, caplog: pytest.LogCaptureFixture
caplog: pytest.LogCaptureFixture,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None: ) -> None:
"""Test we handle exceptions from the update method.""" """Test we handle exceptions from the update method."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -1485,11 +1458,9 @@ async def test_exception_from_coordinator_update_method(
cancel_coordinator() cancel_coordinator()
@pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
async def test_integration_multiple_entity_platforms_with_reload_and_restart( async def test_integration_multiple_entity_platforms_with_reload_and_restart(
hass: HomeAssistant, hass: HomeAssistant, hass_storage: dict[str, Any]
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
hass_storage: dict[str, Any],
) -> None: ) -> None:
"""Test integration of PassiveBluetoothProcessorCoordinator with multiple platforms with reload.""" """Test integration of PassiveBluetoothProcessorCoordinator with multiple platforms with reload."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -1791,11 +1762,8 @@ NAMING_PASSIVE_BLUETOOTH_DATA_UPDATE = PassiveBluetoothDataUpdate(
) )
async def test_naming( @pytest.mark.usefixtures("mock_bleak_scanner_start", "mock_bluetooth_adapters")
hass: HomeAssistant, async def test_naming(hass: HomeAssistant) -> None:
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the PassiveBluetoothProcessorCoordinator.""" """Test basic usage of the PassiveBluetoothProcessorCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})

View file

@ -5,6 +5,7 @@ from unittest.mock import patch
from bleak import BleakError from bleak import BleakError
from freezegun import freeze_time from freezegun import freeze_time
import pytest
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
from homeassistant.components.bluetooth_le_tracker import device_tracker from homeassistant.components.bluetooth_le_tracker import device_tracker
@ -17,7 +18,6 @@ from homeassistant.components.device_tracker import (
CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL,
CONF_TRACK_NEW, CONF_TRACK_NEW,
DOMAIN, DOMAIN,
legacy,
) )
from homeassistant.const import CONF_PLATFORM from homeassistant.const import CONF_PLATFORM
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -66,11 +66,8 @@ class MockBleakClientBattery5(MockBleakClient):
return b"\x05" return b"\x05"
async def test_do_not_see_device_if_time_not_updated( @pytest.mark.usefixtures("mock_bluetooth", "mock_device_tracker_conf")
hass: HomeAssistant, async def test_do_not_see_device_if_time_not_updated(hass: HomeAssistant) -> None:
mock_bluetooth: None,
mock_device_tracker_conf: list[legacy.Device],
) -> None:
"""Test device going not_home after consider_home threshold from first scan if the subsequent scans have not incremented last seen time.""" """Test device going not_home after consider_home threshold from first scan if the subsequent scans have not incremented last seen time."""
address = "DE:AD:BE:EF:13:37" address = "DE:AD:BE:EF:13:37"
@ -132,11 +129,8 @@ async def test_do_not_see_device_if_time_not_updated(
assert state.state == "not_home" assert state.state == "not_home"
async def test_see_device_if_time_updated( @pytest.mark.usefixtures("mock_bluetooth", "mock_device_tracker_conf")
hass: HomeAssistant, async def test_see_device_if_time_updated(hass: HomeAssistant) -> None:
mock_bluetooth: None,
mock_device_tracker_conf: list[legacy.Device],
) -> None:
"""Test device remaining home after consider_home threshold from first scan if the subsequent scans have incremented last seen time.""" """Test device remaining home after consider_home threshold from first scan if the subsequent scans have incremented last seen time."""
address = "DE:AD:BE:EF:13:37" address = "DE:AD:BE:EF:13:37"
@ -214,11 +208,8 @@ async def test_see_device_if_time_updated(
assert state.state == "home" assert state.state == "home"
async def test_preserve_new_tracked_device_name( @pytest.mark.usefixtures("mock_bluetooth", "mock_device_tracker_conf")
hass: HomeAssistant, async def test_preserve_new_tracked_device_name(hass: HomeAssistant) -> None:
mock_bluetooth: None,
mock_device_tracker_conf: list[legacy.Device],
) -> None:
"""Test preserving tracked device name across new seens.""" """Test preserving tracked device name across new seens."""
address = "DE:AD:BE:EF:13:37" address = "DE:AD:BE:EF:13:37"
@ -284,11 +275,8 @@ async def test_preserve_new_tracked_device_name(
assert state.name == name assert state.name == name
async def test_tracking_battery_times_out( @pytest.mark.usefixtures("mock_bluetooth", "mock_device_tracker_conf")
hass: HomeAssistant, async def test_tracking_battery_times_out(hass: HomeAssistant) -> None:
mock_bluetooth: None,
mock_device_tracker_conf: list[legacy.Device],
) -> None:
"""Test tracking the battery times out.""" """Test tracking the battery times out."""
address = "DE:AD:BE:EF:13:37" address = "DE:AD:BE:EF:13:37"
@ -353,11 +341,8 @@ async def test_tracking_battery_times_out(
assert "battery" not in state.attributes assert "battery" not in state.attributes
async def test_tracking_battery_fails( @pytest.mark.usefixtures("mock_bluetooth", "mock_device_tracker_conf")
hass: HomeAssistant, async def test_tracking_battery_fails(hass: HomeAssistant) -> None:
mock_bluetooth: None,
mock_device_tracker_conf: list[legacy.Device],
) -> None:
"""Test tracking the battery fails.""" """Test tracking the battery fails."""
address = "DE:AD:BE:EF:13:37" address = "DE:AD:BE:EF:13:37"
@ -421,11 +406,8 @@ async def test_tracking_battery_fails(
assert "battery" not in state.attributes assert "battery" not in state.attributes
async def test_tracking_battery_successful( @pytest.mark.usefixtures("mock_bluetooth", "mock_device_tracker_conf")
hass: HomeAssistant, async def test_tracking_battery_successful(hass: HomeAssistant) -> None:
mock_bluetooth: None,
mock_device_tracker_conf: list[legacy.Device],
) -> None:
"""Test tracking the battery gets a value.""" """Test tracking the battery gets a value."""
address = "DE:AD:BE:EF:13:37" address = "DE:AD:BE:EF:13:37"

View file

@ -33,9 +33,8 @@ def recorder_url_mock():
yield yield
async def test_setup( @pytest.mark.usefixtures("mock_bluetooth", "mock_zeroconf")
hass: HomeAssistant, mock_zeroconf: None, mock_bluetooth: None async def test_setup(hass: HomeAssistant) -> None:
) -> None:
"""Test setup.""" """Test setup."""
recorder_helper.async_initialize_recorder(hass) recorder_helper.async_initialize_recorder(hass)
# default_config needs the homeassistant integration, assert it will be # default_config needs the homeassistant integration, assert it will be

View file

@ -12,9 +12,8 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_setup_user_no_bluetooth( @pytest.mark.usefixtures("mock_bluetooth_adapters")
hass: HomeAssistant, mock_bluetooth_adapters: None async def test_setup_user_no_bluetooth(hass: HomeAssistant) -> None:
) -> None:
"""Test setting up via user interaction when bluetooth is not enabled.""" """Test setting up via user interaction when bluetooth is not enabled."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,

View file

@ -20,9 +20,8 @@ def assert_form_error(result: FlowResult, key: str, value: str) -> None:
assert result["errors"][key] == value assert result["errors"][key] == value
async def test_setup_user_no_bluetooth( @pytest.mark.usefixtures("mock_bluetooth_adapters")
hass: HomeAssistant, mock_bluetooth_adapters: None async def test_setup_user_no_bluetooth(hass: HomeAssistant) -> None:
) -> None:
"""Test setting up via user interaction when bluetooth is not enabled.""" """Test setting up via user interaction when bluetooth is not enabled."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
const.DOMAIN, const.DOMAIN,