2023-07-12 14:08:15 +00:00
|
|
|
"""Common fixtures for the Gardena Bluetooth tests."""
|
|
|
|
from collections.abc import Generator
|
2023-07-17 19:13:13 +00:00
|
|
|
from typing import Any
|
2023-07-12 14:08:15 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
|
|
|
2023-07-17 19:13:13 +00:00
|
|
|
from freezegun import freeze_time
|
2023-07-12 14:08:15 +00:00
|
|
|
from gardena_bluetooth.client import Client
|
2023-07-17 19:13:13 +00:00
|
|
|
from gardena_bluetooth.const import DeviceInformation
|
|
|
|
from gardena_bluetooth.exceptions import CharacteristicNotFound
|
|
|
|
from gardena_bluetooth.parse import Characteristic
|
2023-07-12 14:08:15 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-07-17 19:13:13 +00:00
|
|
|
from homeassistant.components.gardena_bluetooth.const import DOMAIN
|
|
|
|
from homeassistant.const import CONF_ADDRESS
|
|
|
|
|
|
|
|
from . import WATER_TIMER_SERVICE_INFO
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_entry():
|
|
|
|
"""Create hass config fixture."""
|
|
|
|
return MockConfigEntry(
|
|
|
|
domain=DOMAIN, data={CONF_ADDRESS: WATER_TIMER_SERVICE_INFO.address}
|
|
|
|
)
|
|
|
|
|
2023-07-12 14:08:15 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
|
|
|
"""Override async_setup_entry."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.gardena_bluetooth.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
yield mock_setup_entry
|
|
|
|
|
|
|
|
|
2023-07-17 19:13:13 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_read_char_raw():
|
|
|
|
"""Mock data on device."""
|
|
|
|
return {
|
|
|
|
DeviceInformation.firmware_version.uuid: b"1.2.3",
|
|
|
|
DeviceInformation.model_number.uuid: b"Mock Model",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-12 14:08:15 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2023-07-17 19:13:13 +00:00
|
|
|
def mock_client(enable_bluetooth: None, mock_read_char_raw: dict[str, Any]) -> None:
|
2023-07-12 14:08:15 +00:00
|
|
|
"""Auto mock bluetooth."""
|
|
|
|
|
|
|
|
client = Mock(spec_set=Client)
|
2023-07-17 19:13:13 +00:00
|
|
|
|
|
|
|
SENTINEL = object()
|
|
|
|
|
|
|
|
def _read_char(char: Characteristic, default: Any = SENTINEL):
|
|
|
|
try:
|
|
|
|
return char.decode(mock_read_char_raw[char.uuid])
|
|
|
|
except KeyError:
|
|
|
|
if default is SENTINEL:
|
|
|
|
raise CharacteristicNotFound from KeyError
|
|
|
|
return default
|
|
|
|
|
|
|
|
def _read_char_raw(uuid: str, default: Any = SENTINEL):
|
|
|
|
try:
|
2023-07-18 22:31:01 +00:00
|
|
|
val = mock_read_char_raw[uuid]
|
|
|
|
if isinstance(val, Exception):
|
|
|
|
raise val
|
|
|
|
return val
|
2023-07-17 19:13:13 +00:00
|
|
|
except KeyError:
|
|
|
|
if default is SENTINEL:
|
|
|
|
raise CharacteristicNotFound from KeyError
|
|
|
|
return default
|
|
|
|
|
|
|
|
def _all_char():
|
|
|
|
return set(mock_read_char_raw.keys())
|
|
|
|
|
|
|
|
client.read_char.side_effect = _read_char
|
|
|
|
client.read_char_raw.side_effect = _read_char_raw
|
|
|
|
client.get_all_characteristics_uuid.side_effect = _all_char
|
2023-07-12 14:08:15 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.gardena_bluetooth.config_flow.Client",
|
|
|
|
return_value=client,
|
2023-07-17 19:13:13 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.gardena_bluetooth.Client", return_value=client
|
|
|
|
), freeze_time(
|
|
|
|
"2023-01-01", tz_offset=1
|
2023-07-12 14:08:15 +00:00
|
|
|
):
|
|
|
|
yield client
|
2023-07-18 22:31:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def enable_all_entities():
|
|
|
|
"""Make sure all entities are enabled."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.gardena_bluetooth.coordinator.GardenaBluetoothEntity.entity_registry_enabled_default",
|
|
|
|
new=Mock(return_value=True),
|
|
|
|
):
|
|
|
|
yield
|