mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Avoid accessing coordinator in gardena_bluetooth tests (#96921)
Avoid accessing coordinator in tests
This commit is contained in:
parent
9da155955a
commit
1c19c54e38
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
from unittest.mock import patch
|
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.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo
|
from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo
|
||||||
@ -74,7 +72,7 @@ UNSUPPORTED_GROUP_SERVICE_INFO = BluetoothServiceInfo(
|
|||||||
|
|
||||||
async def setup_entry(
|
async def setup_entry(
|
||||||
hass: HomeAssistant, mock_entry: MockConfigEntry, platforms: list[Platform]
|
hass: HomeAssistant, mock_entry: MockConfigEntry, platforms: list[Platform]
|
||||||
) -> Coordinator:
|
) -> None:
|
||||||
"""Make sure the device is available."""
|
"""Make sure the device is available."""
|
||||||
|
|
||||||
inject_bluetooth_service_info(hass, WATER_TIMER_SERVICE_INFO)
|
inject_bluetooth_service_info(hass, WATER_TIMER_SERVICE_INFO)
|
||||||
@ -83,5 +81,3 @@ async def setup_entry(
|
|||||||
mock_entry.add_to_hass(hass)
|
mock_entry.add_to_hass(hass)
|
||||||
await hass.config_entries.async_setup(mock_entry.entry_id)
|
await hass.config_entries.async_setup(mock_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
return hass.data[DOMAIN][mock_entry.entry_id]
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""Common fixtures for the Gardena Bluetooth tests."""
|
"""Common fixtures for the Gardena Bluetooth tests."""
|
||||||
from collections.abc import Generator
|
from collections.abc import Awaitable, Callable, Generator
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
@ -11,11 +11,13 @@ from gardena_bluetooth.parse import Characteristic
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.gardena_bluetooth.const import DOMAIN
|
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.const import CONF_ADDRESS
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import WATER_TIMER_SERVICE_INFO
|
from . import WATER_TIMER_SERVICE_INFO
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@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)
|
@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."""
|
"""Auto mock bluetooth."""
|
||||||
|
|
||||||
client = Mock(spec_set=Client)
|
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(
|
with patch(
|
||||||
"homeassistant.components.gardena_bluetooth.config_flow.Client",
|
"homeassistant.components.gardena_bluetooth.config_flow.Client",
|
||||||
return_value=client,
|
return_value=client,
|
||||||
), patch(
|
), patch("homeassistant.components.gardena_bluetooth.Client", return_value=client):
|
||||||
"homeassistant.components.gardena_bluetooth.Client", return_value=client
|
|
||||||
), freeze_time(
|
|
||||||
"2023-01-01", tz_offset=1
|
|
||||||
):
|
|
||||||
yield client
|
yield client
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
'entity_id': 'sensor.mock_title_valve_closing',
|
'entity_id': 'sensor.mock_title_valve_closing',
|
||||||
'last_changed': <ANY>,
|
'last_changed': <ANY>,
|
||||||
'last_updated': <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
|
# name: test_setup[98bd0f13-0b0e-421a-84e5-ddbf75dc6de4-raw1-sensor.mock_title_valve_closing].2
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
"""Test Gardena Bluetooth binary sensor."""
|
"""Test Gardena Bluetooth binary sensor."""
|
||||||
|
|
||||||
|
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
|
|
||||||
from gardena_bluetooth.const import Valve
|
from gardena_bluetooth.const import Valve
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
@ -28,6 +30,7 @@ async def test_setup(
|
|||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
mock_entry: MockConfigEntry,
|
mock_entry: MockConfigEntry,
|
||||||
mock_read_char_raw: dict[str, bytes],
|
mock_read_char_raw: dict[str, bytes],
|
||||||
|
scan_step: Callable[[], Awaitable[None]],
|
||||||
uuid: str,
|
uuid: str,
|
||||||
raw: list[bytes],
|
raw: list[bytes],
|
||||||
entity_id: str,
|
entity_id: str,
|
||||||
@ -35,10 +38,10 @@ async def test_setup(
|
|||||||
"""Test setup creates expected entities."""
|
"""Test setup creates expected entities."""
|
||||||
|
|
||||||
mock_read_char_raw[uuid] = raw[0]
|
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
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
for char_raw in raw[1:]:
|
for char_raw in raw[1:]:
|
||||||
mock_read_char_raw[uuid] = char_raw
|
mock_read_char_raw[uuid] = char_raw
|
||||||
await coordinator.async_refresh()
|
await scan_step()
|
||||||
assert hass.states.get(entity_id) == snapshot
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Test Gardena Bluetooth sensor."""
|
"""Test Gardena Bluetooth sensor."""
|
||||||
|
|
||||||
|
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
from unittest.mock import Mock, call
|
from unittest.mock import Mock, call
|
||||||
|
|
||||||
from gardena_bluetooth.const import Reset
|
from gardena_bluetooth.const import Reset
|
||||||
@ -31,15 +32,16 @@ async def test_setup(
|
|||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
mock_entry: MockConfigEntry,
|
mock_entry: MockConfigEntry,
|
||||||
mock_switch_chars: dict[str, bytes],
|
mock_switch_chars: dict[str, bytes],
|
||||||
|
scan_step: Callable[[], Awaitable[None]],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test setup creates expected entities."""
|
"""Test setup creates expected entities."""
|
||||||
|
|
||||||
entity_id = "button.mock_title_factory_reset"
|
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
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
mock_switch_chars[Reset.factory_reset.uuid] = b"\x01"
|
mock_switch_chars[Reset.factory_reset.uuid] = b"\x01"
|
||||||
await coordinator.async_refresh()
|
await scan_step()
|
||||||
assert hass.states.get(entity_id) == snapshot
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Test Gardena Bluetooth sensor."""
|
"""Test Gardena Bluetooth sensor."""
|
||||||
|
|
||||||
|
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import Mock, call
|
from unittest.mock import Mock, call
|
||||||
|
|
||||||
@ -62,6 +63,7 @@ async def test_setup(
|
|||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
mock_entry: MockConfigEntry,
|
mock_entry: MockConfigEntry,
|
||||||
mock_read_char_raw: dict[str, bytes],
|
mock_read_char_raw: dict[str, bytes],
|
||||||
|
scan_step: Callable[[], Awaitable[None]],
|
||||||
uuid: str,
|
uuid: str,
|
||||||
raw: list[bytes],
|
raw: list[bytes],
|
||||||
entity_id: str,
|
entity_id: str,
|
||||||
@ -69,12 +71,12 @@ async def test_setup(
|
|||||||
"""Test setup creates expected entities."""
|
"""Test setup creates expected entities."""
|
||||||
|
|
||||||
mock_read_char_raw[uuid] = raw[0]
|
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
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
for char_raw in raw[1:]:
|
for char_raw in raw[1:]:
|
||||||
mock_read_char_raw[uuid] = char_raw
|
mock_read_char_raw[uuid] = char_raw
|
||||||
await coordinator.async_refresh()
|
await scan_step()
|
||||||
assert hass.states.get(entity_id) == snapshot
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
|
|
||||||
@ -128,6 +130,7 @@ async def test_bluetooth_error_unavailable(
|
|||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
mock_entry: MockConfigEntry,
|
mock_entry: MockConfigEntry,
|
||||||
mock_read_char_raw: dict[str, bytes],
|
mock_read_char_raw: dict[str, bytes],
|
||||||
|
scan_step: Callable[[], Awaitable[None]],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Verify that a connectivity error makes all entities unavailable."""
|
"""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.uuid
|
||||||
] = Valve.remaining_open_time.encode(0)
|
] = 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_remaining_open_time") == snapshot
|
||||||
assert hass.states.get("number.mock_title_manual_watering_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"
|
"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_remaining_open_time") == snapshot
|
||||||
assert hass.states.get("number.mock_title_manual_watering_time") == snapshot
|
assert hass.states.get("number.mock_title_manual_watering_time") == snapshot
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""Test Gardena Bluetooth sensor."""
|
"""Test Gardena Bluetooth sensor."""
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
|
|
||||||
from gardena_bluetooth.const import Battery, Valve
|
from gardena_bluetooth.const import Battery, Valve
|
||||||
import pytest
|
import pytest
|
||||||
@ -37,6 +37,7 @@ async def test_setup(
|
|||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
mock_entry: MockConfigEntry,
|
mock_entry: MockConfigEntry,
|
||||||
mock_read_char_raw: dict[str, bytes],
|
mock_read_char_raw: dict[str, bytes],
|
||||||
|
scan_step: Callable[[], Awaitable[None]],
|
||||||
uuid: str,
|
uuid: str,
|
||||||
raw: list[bytes],
|
raw: list[bytes],
|
||||||
entity_id: str,
|
entity_id: str,
|
||||||
@ -44,10 +45,10 @@ async def test_setup(
|
|||||||
"""Test setup creates expected entities."""
|
"""Test setup creates expected entities."""
|
||||||
|
|
||||||
mock_read_char_raw[uuid] = raw[0]
|
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
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
for char_raw in raw[1:]:
|
for char_raw in raw[1:]:
|
||||||
mock_read_char_raw[uuid] = char_raw
|
mock_read_char_raw[uuid] = char_raw
|
||||||
await coordinator.async_refresh()
|
await scan_step()
|
||||||
assert hass.states.get(entity_id) == snapshot
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Test Gardena Bluetooth sensor."""
|
"""Test Gardena Bluetooth sensor."""
|
||||||
|
|
||||||
|
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
from unittest.mock import Mock, call
|
from unittest.mock import Mock, call
|
||||||
|
|
||||||
from gardena_bluetooth.const import Valve
|
from gardena_bluetooth.const import Valve
|
||||||
@ -40,15 +41,16 @@ async def test_setup(
|
|||||||
mock_entry: MockConfigEntry,
|
mock_entry: MockConfigEntry,
|
||||||
mock_client: Mock,
|
mock_client: Mock,
|
||||||
mock_switch_chars: dict[str, bytes],
|
mock_switch_chars: dict[str, bytes],
|
||||||
|
scan_step: Callable[[], Awaitable[None]],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test setup creates expected entities."""
|
"""Test setup creates expected entities."""
|
||||||
|
|
||||||
entity_id = "switch.mock_title_open"
|
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
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
mock_switch_chars[Valve.state.uuid] = b"\x01"
|
mock_switch_chars[Valve.state.uuid] = b"\x01"
|
||||||
await coordinator.async_refresh()
|
await scan_step()
|
||||||
assert hass.states.get(entity_id) == snapshot
|
assert hass.states.get(entity_id) == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user