Avoid accessing coordinator in gardena_bluetooth tests (#96921)

Avoid accessing coordinator in tests
This commit is contained in:
Joakim Plate 2023-07-20 08:47:26 +02:00 committed by GitHub
parent 9da155955a
commit 1c19c54e38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 27 deletions

View file

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

View file

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

View file

@ -22,7 +22,7 @@
'entity_id': 'sensor.mock_title_valve_closing',
'last_changed': <ANY>,
'last_updated': <ANY>,
'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

View file

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

View file

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

View file

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

View file

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

View file

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