forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
125 lines (104 loc) · 3.9 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Test helpers for Freebox."""
import json
from unittest.mock import AsyncMock, PropertyMock, patch
from freebox_api.exceptions import HttpRequestError
import pytest
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from .const import (
DATA_CALL_GET_CALLS_LOG,
DATA_CONNECTION_GET_STATUS,
DATA_HOME_GET_NODES,
DATA_HOME_PIR_GET_VALUE,
DATA_HOME_SET_VALUE,
DATA_LAN_GET_HOSTS_LIST,
DATA_LAN_GET_HOSTS_LIST_MODE_BRIDGE,
DATA_STORAGE_GET_DISKS,
DATA_STORAGE_GET_RAIDS,
DATA_SYSTEM_GET_CONFIG,
DATA_WIFI_GET_GLOBAL_CONFIG,
)
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
def mock_path():
"""Mock path lib."""
with patch("homeassistant.components.freebox.router.Path"), patch(
"homeassistant.components.freebox.router.os.makedirs"
):
yield
@pytest.fixture(autouse=True)
def enable_all_entities():
"""Make sure all entities are enabled."""
with patch(
"homeassistant.helpers.entity.Entity.entity_registry_enabled_default",
PropertyMock(return_value=True),
):
yield
@pytest.fixture
def mock_device_registry_devices(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
):
"""Create device registry devices so the device tracker entities are enabled."""
config_entry = MockConfigEntry(domain="something_else")
config_entry.add_to_hass(hass)
for idx, device in enumerate(
(
"68:A3:78:00:00:00",
"8C:97:EA:00:00:00",
"DE:00:B0:00:00:00",
"DC:00:B0:00:00:00",
"5E:65:55:00:00:00",
)
):
device_registry.async_get_or_create(
name=f"Device {idx}",
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, device)},
)
@pytest.fixture(name="router")
def mock_router(mock_device_registry_devices):
"""Mock a successful connection."""
with patch("homeassistant.components.freebox.router.Freepybox") as service_mock:
instance = service_mock.return_value
instance.open = AsyncMock()
instance.system.get_config = AsyncMock(return_value=DATA_SYSTEM_GET_CONFIG)
# device_tracker
instance.lan.get_hosts_list = AsyncMock(return_value=DATA_LAN_GET_HOSTS_LIST)
# sensor
instance.call.get_calls_log = AsyncMock(return_value=DATA_CALL_GET_CALLS_LOG)
instance.storage.get_disks = AsyncMock(return_value=DATA_STORAGE_GET_DISKS)
instance.storage.get_raids = AsyncMock(return_value=DATA_STORAGE_GET_RAIDS)
instance.connection.get_status = AsyncMock(
return_value=DATA_CONNECTION_GET_STATUS
)
# switch
instance.wifi.get_global_config = AsyncMock(
return_value=DATA_WIFI_GET_GLOBAL_CONFIG
)
# home devices
instance.home.get_home_nodes = AsyncMock(return_value=DATA_HOME_GET_NODES)
instance.home.get_home_endpoint_value = AsyncMock(
return_value=DATA_HOME_PIR_GET_VALUE
)
instance.home.set_home_endpoint_value = AsyncMock(
return_value=DATA_HOME_SET_VALUE
)
instance.close = AsyncMock()
yield service_mock
@pytest.fixture(name="router_bridge_mode")
def mock_router_bridge_mode(mock_device_registry_devices, router):
"""Mock a successful connection to Freebox Bridge mode."""
router().lan.get_hosts_list = AsyncMock(
side_effect=HttpRequestError(
"Request failed (APIResponse: %s)"
% json.dumps(DATA_LAN_GET_HOSTS_LIST_MODE_BRIDGE)
)
)
return router
@pytest.fixture
def mock_router_bridge_mode_error(mock_device_registry_devices, router):
"""Mock a failed connection to Freebox Bridge mode."""
router().lan.get_hosts_list = AsyncMock(
side_effect=HttpRequestError("Request failed (APIResponse: some unknown error)")
)
return router