forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
193 lines (173 loc) · 5.74 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Test configuration and mocks for the SmartThings component."""
from collections.abc import Generator
import time
from unittest.mock import AsyncMock, patch
from pysmartthings.models import (
DeviceResponse,
DeviceStatus,
LocationResponse,
RoomResponse,
SceneResponse,
)
import pytest
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.components.smartthings import CONF_INSTALLED_APP_ID
from homeassistant.components.smartthings.const import (
CONF_LOCATION_ID,
CONF_REFRESH_TOKEN,
DOMAIN,
SCOPES,
)
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_CLIENT_SECRET
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.smartthings.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture(name="expires_at")
def mock_expires_at() -> int:
"""Fixture to set the oauth token expiration time."""
return time.time() + 3600
@pytest.fixture(autouse=True)
async def setup_credentials(hass: HomeAssistant) -> None:
"""Fixture to setup credentials."""
assert await async_setup_component(hass, "application_credentials", {})
await async_import_client_credential(
hass,
DOMAIN,
ClientCredential("CLIENT_ID", "CLIENT_SECRET"),
DOMAIN,
)
@pytest.fixture
def mock_smartthings() -> Generator[AsyncMock]:
"""Mock a SmartThings client."""
with (
patch(
"homeassistant.components.smartthings.SmartThings",
autospec=True,
) as mock_client,
patch(
"homeassistant.components.smartthings.config_flow.SmartThings",
new=mock_client,
),
):
client = mock_client.return_value
client.get_scenes.return_value = SceneResponse.from_json(
load_fixture("scenes.json", DOMAIN)
).items
client.get_locations.return_value = LocationResponse.from_json(
load_fixture("locations.json", DOMAIN)
).items
client.get_rooms.return_value = RoomResponse.from_json(
load_fixture("rooms.json", DOMAIN)
).items
yield client
@pytest.fixture(
params=[
"da_ac_rac_000001",
"da_ac_rac_100001",
"da_ac_rac_01001",
"multipurpose_sensor",
"contact_sensor",
"base_electric_meter",
"smart_plug",
"vd_stv_2017_k",
"c2c_arlo_pro_3_switch",
"yale_push_button_deadbolt_lock",
"ge_in_wall_smart_dimmer",
"centralite",
"da_ref_normal_000001",
"vd_network_audio_002s",
"iphone",
"da_wm_dw_000001",
"da_wm_wd_000001",
"da_wm_wd_000001_1",
"da_wm_wm_000001",
"da_wm_wm_000001_1",
"da_rvc_normal_000001",
"da_ks_microwave_0101x",
"hue_color_temperature_bulb",
"hue_rgbw_color_bulb",
"c2c_shade",
"sonos_player",
"aeotec_home_energy_meter_gen5",
"virtual_water_sensor",
"virtual_thermostat",
"virtual_valve",
"sensibo_airconditioner_1",
"ecobee_sensor",
"ecobee_thermostat",
"ecobee_thermostat_offline",
"fake_fan",
"generic_fan_3_speed",
"heatit_ztrm3_thermostat",
"generic_ef00_v1",
"bosch_radiator_thermostat_ii",
"im_speaker_ai_0001",
"abl_light_b_001",
"tplink_p110",
]
)
def device_fixture(
mock_smartthings: AsyncMock, request: pytest.FixtureRequest
) -> Generator[str]:
"""Return every device."""
return request.param
@pytest.fixture
def devices(mock_smartthings: AsyncMock, device_fixture: str) -> Generator[AsyncMock]:
"""Return a specific device."""
mock_smartthings.get_devices.return_value = DeviceResponse.from_json(
load_fixture(f"devices/{device_fixture}.json", DOMAIN)
).items
mock_smartthings.get_device_status.return_value = DeviceStatus.from_json(
load_fixture(f"device_status/{device_fixture}.json", DOMAIN)
).components
return mock_smartthings
@pytest.fixture
def mock_config_entry(expires_at: int) -> MockConfigEntry:
"""Mock a config entry."""
return MockConfigEntry(
domain=DOMAIN,
title="My home",
unique_id="397678e5-9995-4a39-9d9f-ae6ba310236c",
data={
"auth_implementation": DOMAIN,
"token": {
"access_token": "mock-access-token",
"refresh_token": "mock-refresh-token",
"expires_at": expires_at,
"scope": " ".join(SCOPES),
"access_tier": 0,
"installed_app_id": "5aaaa925-2be1-4e40-b257-e4ef59083324",
},
CONF_LOCATION_ID: "397678e5-9995-4a39-9d9f-ae6ba310236c",
CONF_INSTALLED_APP_ID: "123",
},
version=3,
)
@pytest.fixture
def mock_old_config_entry() -> MockConfigEntry:
"""Mock the old config entry."""
return MockConfigEntry(
domain=DOMAIN,
title="My home",
unique_id="appid123-2be1-4e40-b257-e4ef59083324_397678e5-9995-4a39-9d9f-ae6ba310236c",
data={
CONF_ACCESS_TOKEN: "mock-access-token",
CONF_REFRESH_TOKEN: "mock-refresh-token",
CONF_CLIENT_ID: "CLIENT_ID",
CONF_CLIENT_SECRET: "CLIENT_SECRET",
CONF_LOCATION_ID: "397678e5-9995-4a39-9d9f-ae6ba310236c",
CONF_INSTALLED_APP_ID: "123aa123-2be1-4e40-b257-e4ef59083324",
},
version=2,
)