From 1c19c54e380539e303e58382087a59be9fbd25b4 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Thu, 20 Jul 2023 08:47:26 +0200 Subject: [PATCH] Avoid accessing coordinator in gardena_bluetooth tests (#96921) Avoid accessing coordinator in tests --- .../components/gardena_bluetooth/__init__.py | 6 +--- .../components/gardena_bluetooth/conftest.py | 33 ++++++++++++++----- .../snapshots/test_sensor.ambr | 2 +- .../gardena_bluetooth/test_binary_sensor.py | 7 ++-- .../gardena_bluetooth/test_button.py | 6 ++-- .../gardena_bluetooth/test_number.py | 11 ++++--- .../gardena_bluetooth/test_sensor.py | 7 ++-- .../gardena_bluetooth/test_switch.py | 6 ++-- 8 files changed, 51 insertions(+), 27 deletions(-) diff --git a/tests/components/gardena_bluetooth/__init__.py b/tests/components/gardena_bluetooth/__init__.py index a5ea94088fd..7de0780e129 100644 --- a/tests/components/gardena_bluetooth/__init__.py +++ b/tests/components/gardena_bluetooth/__init__.py @@ -2,8 +2,6 @@ from unittest.mock import patch -from homeassistant.components.gardena_bluetooth.const import DOMAIN -from homeassistant.components.gardena_bluetooth.coordinator import Coordinator from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo @@ -74,7 +72,7 @@ UNSUPPORTED_GROUP_SERVICE_INFO = BluetoothServiceInfo( async def setup_entry( hass: HomeAssistant, mock_entry: MockConfigEntry, platforms: list[Platform] -) -> Coordinator: +) -> None: """Make sure the device is available.""" inject_bluetooth_service_info(hass, WATER_TIMER_SERVICE_INFO) @@ -83,5 +81,3 @@ async def setup_entry( mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() - - return hass.data[DOMAIN][mock_entry.entry_id] diff --git a/tests/components/gardena_bluetooth/conftest.py b/tests/components/gardena_bluetooth/conftest.py index a1d31c45807..98ae41d195b 100644 --- a/tests/components/gardena_bluetooth/conftest.py +++ b/tests/components/gardena_bluetooth/conftest.py @@ -1,5 +1,5 @@ """Common fixtures for the Gardena Bluetooth tests.""" -from collections.abc import Generator +from collections.abc import Awaitable, Callable, Generator from typing import Any from unittest.mock import AsyncMock, Mock, patch @@ -11,11 +11,13 @@ from gardena_bluetooth.parse import Characteristic import pytest from homeassistant.components.gardena_bluetooth.const import DOMAIN +from homeassistant.components.gardena_bluetooth.coordinator import SCAN_INTERVAL from homeassistant.const import CONF_ADDRESS +from homeassistant.core import HomeAssistant from . import WATER_TIMER_SERVICE_INFO -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_fire_time_changed @pytest.fixture @@ -45,8 +47,27 @@ def mock_read_char_raw(): } +@pytest.fixture +async def scan_step( + hass: HomeAssistant, +) -> Generator[None, None, Callable[[], Awaitable[None]]]: + """Step system time forward.""" + + with freeze_time("2023-01-01", tz_offset=1) as frozen_time: + + async def delay(): + """Trigger delay in system.""" + frozen_time.tick(delta=SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + yield delay + + @pytest.fixture(autouse=True) -def mock_client(enable_bluetooth: None, mock_read_char_raw: dict[str, Any]) -> None: +def mock_client( + enable_bluetooth: None, scan_step, mock_read_char_raw: dict[str, Any] +) -> None: """Auto mock bluetooth.""" client = Mock(spec_set=Client) @@ -82,11 +103,7 @@ def mock_client(enable_bluetooth: None, mock_read_char_raw: dict[str, Any]) -> N with patch( "homeassistant.components.gardena_bluetooth.config_flow.Client", return_value=client, - ), patch( - "homeassistant.components.gardena_bluetooth.Client", return_value=client - ), freeze_time( - "2023-01-01", tz_offset=1 - ): + ), patch("homeassistant.components.gardena_bluetooth.Client", return_value=client): yield client diff --git a/tests/components/gardena_bluetooth/snapshots/test_sensor.ambr b/tests/components/gardena_bluetooth/snapshots/test_sensor.ambr index 5a23b6d7f50..14135cb390c 100644 --- a/tests/components/gardena_bluetooth/snapshots/test_sensor.ambr +++ b/tests/components/gardena_bluetooth/snapshots/test_sensor.ambr @@ -22,7 +22,7 @@ 'entity_id': 'sensor.mock_title_valve_closing', 'last_changed': , 'last_updated': , - 'state': '2023-01-01T01:00:10+00:00', + 'state': '2023-01-01T01:01:10+00:00', }) # --- # name: test_setup[98bd0f13-0b0e-421a-84e5-ddbf75dc6de4-raw1-sensor.mock_title_valve_closing].2 diff --git a/tests/components/gardena_bluetooth/test_binary_sensor.py b/tests/components/gardena_bluetooth/test_binary_sensor.py index cda24f871e8..d12f825b1a7 100644 --- a/tests/components/gardena_bluetooth/test_binary_sensor.py +++ b/tests/components/gardena_bluetooth/test_binary_sensor.py @@ -1,6 +1,8 @@ """Test Gardena Bluetooth binary sensor.""" +from collections.abc import Awaitable, Callable + from gardena_bluetooth.const import Valve import pytest from syrupy.assertion import SnapshotAssertion @@ -28,6 +30,7 @@ async def test_setup( snapshot: SnapshotAssertion, mock_entry: MockConfigEntry, mock_read_char_raw: dict[str, bytes], + scan_step: Callable[[], Awaitable[None]], uuid: str, raw: list[bytes], entity_id: str, @@ -35,10 +38,10 @@ async def test_setup( """Test setup creates expected entities.""" mock_read_char_raw[uuid] = raw[0] - coordinator = await setup_entry(hass, mock_entry, [Platform.BINARY_SENSOR]) + await setup_entry(hass, mock_entry, [Platform.BINARY_SENSOR]) assert hass.states.get(entity_id) == snapshot for char_raw in raw[1:]: mock_read_char_raw[uuid] = char_raw - await coordinator.async_refresh() + await scan_step() assert hass.states.get(entity_id) == snapshot diff --git a/tests/components/gardena_bluetooth/test_button.py b/tests/components/gardena_bluetooth/test_button.py index e184a2ecce8..52fa3d4b00e 100644 --- a/tests/components/gardena_bluetooth/test_button.py +++ b/tests/components/gardena_bluetooth/test_button.py @@ -1,6 +1,7 @@ """Test Gardena Bluetooth sensor.""" +from collections.abc import Awaitable, Callable from unittest.mock import Mock, call from gardena_bluetooth.const import Reset @@ -31,15 +32,16 @@ async def test_setup( snapshot: SnapshotAssertion, mock_entry: MockConfigEntry, mock_switch_chars: dict[str, bytes], + scan_step: Callable[[], Awaitable[None]], ) -> None: """Test setup creates expected entities.""" entity_id = "button.mock_title_factory_reset" - coordinator = await setup_entry(hass, mock_entry, [Platform.BUTTON]) + await setup_entry(hass, mock_entry, [Platform.BUTTON]) assert hass.states.get(entity_id) == snapshot mock_switch_chars[Reset.factory_reset.uuid] = b"\x01" - await coordinator.async_refresh() + await scan_step() assert hass.states.get(entity_id) == snapshot diff --git a/tests/components/gardena_bluetooth/test_number.py b/tests/components/gardena_bluetooth/test_number.py index 588b73aadbb..3b04d0cc818 100644 --- a/tests/components/gardena_bluetooth/test_number.py +++ b/tests/components/gardena_bluetooth/test_number.py @@ -1,6 +1,7 @@ """Test Gardena Bluetooth sensor.""" +from collections.abc import Awaitable, Callable from typing import Any from unittest.mock import Mock, call @@ -62,6 +63,7 @@ async def test_setup( snapshot: SnapshotAssertion, mock_entry: MockConfigEntry, mock_read_char_raw: dict[str, bytes], + scan_step: Callable[[], Awaitable[None]], uuid: str, raw: list[bytes], entity_id: str, @@ -69,12 +71,12 @@ async def test_setup( """Test setup creates expected entities.""" mock_read_char_raw[uuid] = raw[0] - coordinator = await setup_entry(hass, mock_entry, [Platform.NUMBER]) + await setup_entry(hass, mock_entry, [Platform.NUMBER]) assert hass.states.get(entity_id) == snapshot for char_raw in raw[1:]: mock_read_char_raw[uuid] = char_raw - await coordinator.async_refresh() + await scan_step() assert hass.states.get(entity_id) == snapshot @@ -128,6 +130,7 @@ async def test_bluetooth_error_unavailable( snapshot: SnapshotAssertion, mock_entry: MockConfigEntry, mock_read_char_raw: dict[str, bytes], + scan_step: Callable[[], Awaitable[None]], ) -> None: """Verify that a connectivity error makes all entities unavailable.""" @@ -138,7 +141,7 @@ async def test_bluetooth_error_unavailable( Valve.remaining_open_time.uuid ] = Valve.remaining_open_time.encode(0) - coordinator = await setup_entry(hass, mock_entry, [Platform.NUMBER]) + await setup_entry(hass, mock_entry, [Platform.NUMBER]) assert hass.states.get("number.mock_title_remaining_open_time") == snapshot assert hass.states.get("number.mock_title_manual_watering_time") == snapshot @@ -146,6 +149,6 @@ async def test_bluetooth_error_unavailable( "Test for errors on bluetooth" ) - await coordinator.async_refresh() + await scan_step() assert hass.states.get("number.mock_title_remaining_open_time") == snapshot assert hass.states.get("number.mock_title_manual_watering_time") == snapshot diff --git a/tests/components/gardena_bluetooth/test_sensor.py b/tests/components/gardena_bluetooth/test_sensor.py index e9fd452e6a2..307a9467f00 100644 --- a/tests/components/gardena_bluetooth/test_sensor.py +++ b/tests/components/gardena_bluetooth/test_sensor.py @@ -1,5 +1,5 @@ """Test Gardena Bluetooth sensor.""" - +from collections.abc import Awaitable, Callable from gardena_bluetooth.const import Battery, Valve import pytest @@ -37,6 +37,7 @@ async def test_setup( snapshot: SnapshotAssertion, mock_entry: MockConfigEntry, mock_read_char_raw: dict[str, bytes], + scan_step: Callable[[], Awaitable[None]], uuid: str, raw: list[bytes], entity_id: str, @@ -44,10 +45,10 @@ async def test_setup( """Test setup creates expected entities.""" mock_read_char_raw[uuid] = raw[0] - coordinator = await setup_entry(hass, mock_entry, [Platform.SENSOR]) + await setup_entry(hass, mock_entry, [Platform.SENSOR]) assert hass.states.get(entity_id) == snapshot for char_raw in raw[1:]: mock_read_char_raw[uuid] = char_raw - await coordinator.async_refresh() + await scan_step() assert hass.states.get(entity_id) == snapshot diff --git a/tests/components/gardena_bluetooth/test_switch.py b/tests/components/gardena_bluetooth/test_switch.py index c2571b7a588..40e8c148335 100644 --- a/tests/components/gardena_bluetooth/test_switch.py +++ b/tests/components/gardena_bluetooth/test_switch.py @@ -1,6 +1,7 @@ """Test Gardena Bluetooth sensor.""" +from collections.abc import Awaitable, Callable from unittest.mock import Mock, call from gardena_bluetooth.const import Valve @@ -40,15 +41,16 @@ async def test_setup( mock_entry: MockConfigEntry, mock_client: Mock, mock_switch_chars: dict[str, bytes], + scan_step: Callable[[], Awaitable[None]], ) -> None: """Test setup creates expected entities.""" entity_id = "switch.mock_title_open" - coordinator = await setup_entry(hass, mock_entry, [Platform.SWITCH]) + await setup_entry(hass, mock_entry, [Platform.SWITCH]) assert hass.states.get(entity_id) == snapshot mock_switch_chars[Valve.state.uuid] = b"\x01" - await coordinator.async_refresh() + await scan_step() assert hass.states.get(entity_id) == snapshot