Skip to content

Commit 6c74345

Browse files
authored
Fix tado tests opening sockets (home-assistant#156386)
1 parent 5ec1c2b commit 6c74345

File tree

9 files changed

+223
-261
lines changed

9 files changed

+223
-261
lines changed

tests/components/tado/conftest.py

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
from PyTado.http import DeviceActivationStatus
77
import pytest
8+
import requests_mock
89

910
from homeassistant.components.tado import CONF_REFRESH_TOKEN, DOMAIN
11+
from homeassistant.core import HomeAssistant
1012

11-
from tests.common import MockConfigEntry, load_json_object_fixture
13+
from tests.common import MockConfigEntry, async_load_fixture, load_json_object_fixture
1214

1315

1416
@pytest.fixture
@@ -48,3 +50,196 @@ def mock_config_entry() -> MockConfigEntry:
4850
unique_id="1",
4951
version=2,
5052
)
53+
54+
55+
@pytest.fixture
56+
async def init_integration(hass: HomeAssistant):
57+
"""Set up the tado integration in Home Assistant."""
58+
59+
token_fixture = "token.json"
60+
devices_fixture = "devices.json"
61+
mobile_devices_fixture = "mobile_devices.json"
62+
me_fixture = "me.json"
63+
weather_fixture = "weather.json"
64+
home_fixture = "home.json"
65+
home_state_fixture = "home_state.json"
66+
zones_fixture = "zones.json"
67+
zone_states_fixture = "zone_states.json"
68+
69+
# WR1 Device
70+
device_wr1_fixture = "device_wr1.json"
71+
72+
# Smart AC with fanLevel, Vertical and Horizontal swings
73+
zone_6_state_fixture = "smartac4.with_fanlevel.json"
74+
zone_6_capabilities_fixture = "zone_with_fanlevel_horizontal_vertical_swing.json"
75+
76+
# Smart AC with Swing
77+
zone_5_state_fixture = "smartac3.with_swing.json"
78+
zone_5_capabilities_fixture = "zone_with_swing_capabilities.json"
79+
80+
# Water Heater 2
81+
zone_4_state_fixture = "tadov2.water_heater.heating.json"
82+
zone_4_capabilities_fixture = "water_heater_zone_capabilities.json"
83+
84+
# Smart AC
85+
zone_3_state_fixture = "smartac3.cool_mode.json"
86+
zone_3_capabilities_fixture = "zone_capabilities.json"
87+
88+
# Water Heater
89+
zone_2_state_fixture = "tadov2.water_heater.auto_mode.json"
90+
zone_2_capabilities_fixture = "water_heater_zone_capabilities.json"
91+
92+
# Tado V2 with manual heating
93+
zone_1_state_fixture = "tadov2.heating.manual_mode.json"
94+
zone_1_capabilities_fixture = "tadov2.zone_capabilities.json"
95+
96+
# Device Temp Offset
97+
device_temp_offset = "device_temp_offset.json"
98+
99+
# Zone Default Overlay
100+
zone_def_overlay = "zone_default_overlay.json"
101+
102+
with requests_mock.mock() as m:
103+
m.post(
104+
"https://auth.tado.com/oauth/token",
105+
text=await async_load_fixture(hass, token_fixture, DOMAIN),
106+
)
107+
m.get(
108+
"https://my.tado.com/api/v2/me",
109+
text=await async_load_fixture(hass, me_fixture, DOMAIN),
110+
)
111+
m.get(
112+
"https://my.tado.com/api/v2/homes/1/",
113+
text=await async_load_fixture(hass, home_fixture, DOMAIN),
114+
)
115+
m.get(
116+
"https://my.tado.com/api/v2/homes/1/weather",
117+
text=await async_load_fixture(hass, weather_fixture, DOMAIN),
118+
)
119+
m.get(
120+
"https://my.tado.com/api/v2/homes/1/state",
121+
text=await async_load_fixture(hass, home_state_fixture, DOMAIN),
122+
)
123+
m.get(
124+
"https://my.tado.com/api/v2/homes/1/devices",
125+
text=await async_load_fixture(hass, devices_fixture, DOMAIN),
126+
)
127+
m.get(
128+
"https://my.tado.com/api/v2/homes/1/mobileDevices",
129+
text=await async_load_fixture(hass, mobile_devices_fixture, DOMAIN),
130+
)
131+
m.get(
132+
"https://my.tado.com/api/v2/devices/WR1/",
133+
text=await async_load_fixture(hass, device_wr1_fixture, DOMAIN),
134+
)
135+
m.get(
136+
"https://my.tado.com/api/v2/devices/WR1/temperatureOffset",
137+
text=await async_load_fixture(hass, device_temp_offset, DOMAIN),
138+
)
139+
m.get(
140+
"https://my.tado.com/api/v2/devices/WR4/temperatureOffset",
141+
text=await async_load_fixture(hass, device_temp_offset, DOMAIN),
142+
)
143+
m.get(
144+
"https://my.tado.com/api/v2/homes/1/zones",
145+
text=await async_load_fixture(hass, zones_fixture, DOMAIN),
146+
)
147+
m.get(
148+
"https://my.tado.com/api/v2/homes/1/zoneStates",
149+
text=await async_load_fixture(hass, zone_states_fixture, DOMAIN),
150+
)
151+
m.get(
152+
"https://my.tado.com/api/v2/homes/1/zones/6/capabilities",
153+
text=await async_load_fixture(hass, zone_6_capabilities_fixture, DOMAIN),
154+
)
155+
m.get(
156+
"https://my.tado.com/api/v2/homes/1/zones/5/capabilities",
157+
text=await async_load_fixture(hass, zone_5_capabilities_fixture, DOMAIN),
158+
)
159+
m.get(
160+
"https://my.tado.com/api/v2/homes/1/zones/4/capabilities",
161+
text=await async_load_fixture(hass, zone_4_capabilities_fixture, DOMAIN),
162+
)
163+
m.get(
164+
"https://my.tado.com/api/v2/homes/1/zones/3/capabilities",
165+
text=await async_load_fixture(hass, zone_3_capabilities_fixture, DOMAIN),
166+
)
167+
m.get(
168+
"https://my.tado.com/api/v2/homes/1/zones/2/capabilities",
169+
text=await async_load_fixture(hass, zone_2_capabilities_fixture, DOMAIN),
170+
)
171+
m.get(
172+
"https://my.tado.com/api/v2/homes/1/zones/1/capabilities",
173+
text=await async_load_fixture(hass, zone_1_capabilities_fixture, DOMAIN),
174+
)
175+
m.get(
176+
"https://my.tado.com/api/v2/homes/1/zones/1/defaultOverlay",
177+
text=await async_load_fixture(hass, zone_def_overlay, DOMAIN),
178+
)
179+
m.get(
180+
"https://my.tado.com/api/v2/homes/1/zones/2/defaultOverlay",
181+
text=await async_load_fixture(hass, zone_def_overlay, DOMAIN),
182+
)
183+
m.get(
184+
"https://my.tado.com/api/v2/homes/1/zones/3/defaultOverlay",
185+
text=await async_load_fixture(hass, zone_def_overlay, DOMAIN),
186+
)
187+
m.get(
188+
"https://my.tado.com/api/v2/homes/1/zones/4/defaultOverlay",
189+
text=await async_load_fixture(hass, zone_def_overlay, DOMAIN),
190+
)
191+
m.get(
192+
"https://my.tado.com/api/v2/homes/1/zones/5/defaultOverlay",
193+
text=await async_load_fixture(hass, zone_def_overlay, DOMAIN),
194+
)
195+
m.get(
196+
"https://my.tado.com/api/v2/homes/1/zones/6/defaultOverlay",
197+
text=await async_load_fixture(hass, zone_def_overlay, DOMAIN),
198+
)
199+
m.get(
200+
"https://my.tado.com/api/v2/homes/1/zones/6/state",
201+
text=await async_load_fixture(hass, zone_6_state_fixture, DOMAIN),
202+
)
203+
m.get(
204+
"https://my.tado.com/api/v2/homes/1/zones/5/state",
205+
text=await async_load_fixture(hass, zone_5_state_fixture, DOMAIN),
206+
)
207+
m.get(
208+
"https://my.tado.com/api/v2/homes/1/zones/4/state",
209+
text=await async_load_fixture(hass, zone_4_state_fixture, DOMAIN),
210+
)
211+
m.get(
212+
"https://my.tado.com/api/v2/homes/1/zones/3/state",
213+
text=await async_load_fixture(hass, zone_3_state_fixture, DOMAIN),
214+
)
215+
m.get(
216+
"https://my.tado.com/api/v2/homes/1/zones/2/state",
217+
text=await async_load_fixture(hass, zone_2_state_fixture, DOMAIN),
218+
)
219+
m.get(
220+
"https://my.tado.com/api/v2/homes/1/zones/1/state",
221+
text=await async_load_fixture(hass, zone_1_state_fixture, DOMAIN),
222+
)
223+
m.post(
224+
"https://login.tado.com/oauth2/token",
225+
text=await async_load_fixture(hass, token_fixture, DOMAIN),
226+
)
227+
entry = MockConfigEntry(
228+
domain=DOMAIN,
229+
version=2,
230+
data={
231+
CONF_REFRESH_TOKEN: "mock-token",
232+
},
233+
options={"fallback": "NEXT_TIME_BLOCK"},
234+
)
235+
entry.add_to_hass(hass)
236+
237+
await hass.config_entries.async_setup(entry.entry_id)
238+
await hass.async_block_till_done()
239+
240+
# For a first refresh
241+
await entry.runtime_data.coordinator.async_refresh()
242+
await entry.runtime_data.mobile_coordinator.async_refresh()
243+
await hass.async_block_till_done()
244+
245+
yield
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""The binary sensor tests for the tado platform."""
22

3-
from collections.abc import AsyncGenerator
3+
from collections.abc import Generator
44
from unittest.mock import patch
55

66
import pytest
@@ -11,25 +11,22 @@
1111
from homeassistant.core import HomeAssistant
1212
from homeassistant.helpers import entity_registry as er
1313

14-
from .util import async_init_integration
15-
1614
from tests.common import MockConfigEntry, snapshot_platform
1715

1816

1917
@pytest.fixture(autouse=True)
20-
def setup_platforms() -> AsyncGenerator[None]:
18+
def setup_platforms() -> Generator[None]:
2119
"""Set up the platforms for the tests."""
2220
with patch("homeassistant.components.tado.PLATFORMS", [Platform.BINARY_SENSOR]):
2321
yield
2422

2523

24+
@pytest.mark.usefixtures("init_integration")
2625
async def test_entities(
2726
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
2827
) -> None:
2928
"""Test creation of binary sensor."""
3029

31-
await async_init_integration(hass)
32-
3330
config_entry: MockConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
3431

3532
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)

tests/components/tado/test_climate.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""The climate tests for the tado platform."""
22

3-
from collections.abc import AsyncGenerator
3+
from collections.abc import Generator
44
from unittest.mock import patch
55

66
from PyTado.interface.api.my_tado import TadoZone
@@ -19,45 +19,37 @@
1919
from homeassistant.core import HomeAssistant
2020
from homeassistant.helpers import entity_registry as er
2121

22-
from .util import async_init_integration
23-
2422
from tests.common import MockConfigEntry, snapshot_platform
2523

2624

2725
@pytest.fixture(autouse=True)
28-
def setup_platforms() -> AsyncGenerator[None]:
26+
def setup_platforms() -> Generator[None]:
2927
"""Set up the platforms for the tests."""
3028
with patch("homeassistant.components.tado.PLATFORMS", [Platform.CLIMATE]):
3129
yield
3230

3331

32+
@pytest.mark.usefixtures("init_integration")
3433
async def test_entities(
3534
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
3635
) -> None:
3736
"""Test creation of climate entities."""
3837

39-
await async_init_integration(hass)
40-
4138
config_entry: MockConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
4239

4340
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
4441

4542

43+
@pytest.mark.usefixtures("init_integration")
4644
async def test_heater_set_temperature(
4745
hass: HomeAssistant, snapshot: SnapshotAssertion
4846
) -> None:
4947
"""Test the set temperature of the heater."""
5048

51-
await async_init_integration(hass)
52-
5349
with (
5450
patch(
5551
"homeassistant.components.tado.PyTado.interface.api.Tado.set_zone_overlay"
5652
) as mock_set_state,
57-
patch(
58-
"homeassistant.components.tado.PyTado.interface.api.Tado.get_zone_state",
59-
return_value={"setting": {"temperature": {"celsius": 22.0}}},
60-
),
6153
):
6254
await hass.services.async_call(
6355
CLIMATE_DOMAIN,
@@ -80,6 +72,7 @@ async def test_heater_set_temperature(
8072
(HVACMode.OFF, "OFF"),
8173
],
8274
)
75+
@pytest.mark.usefixtures("init_integration")
8376
async def test_aircon_set_hvac_mode(
8477
hass: HomeAssistant,
8578
snapshot: SnapshotAssertion,
@@ -88,14 +81,12 @@ async def test_aircon_set_hvac_mode(
8881
) -> None:
8982
"""Test the set hvac mode of the air conditioning."""
9083

91-
await async_init_integration(hass)
92-
9384
with (
9485
patch(
95-
"homeassistant.components.tado.__init__.PyTado.interface.api.Tado.set_zone_overlay"
86+
"homeassistant.components.tado.PyTado.interface.api.Tado.set_zone_overlay"
9687
) as mock_set_state,
9788
patch(
98-
"homeassistant.components.tado.__init__.PyTado.interface.api.Tado.get_zone_state",
89+
"homeassistant.components.tado.PyTado.interface.api.Tado.get_zone_state",
9990
return_value=TadoZone(
10091
zone_id=1,
10192
current_temp=18.7,

tests/components/tado/test_diagnostics.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
"""Test the Tado component diagnostics."""
22

3+
import pytest
34
from syrupy.assertion import SnapshotAssertion
45
from syrupy.filters import props
56

67
from homeassistant.components.tado.const import DOMAIN
78
from homeassistant.core import HomeAssistant
89

9-
from .util import async_init_integration
10-
1110
from tests.common import MockConfigEntry
1211
from tests.components.diagnostics import get_diagnostics_for_config_entry
1312
from tests.typing import ClientSessionGenerator
1413

1514

15+
@pytest.mark.usefixtures("init_integration")
1616
async def test_get_config_entry_diagnostics(
1717
hass: HomeAssistant,
1818
snapshot: SnapshotAssertion,
1919
hass_client: ClientSessionGenerator,
2020
) -> None:
2121
"""Test if get_config_entry_diagnostics returns the correct data."""
22-
await async_init_integration(hass)
23-
2422
config_entry: MockConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
2523
diagnostics = await get_diagnostics_for_config_entry(
2624
hass, hass_client, config_entry
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""The sensor tests for the tado platform."""
22

3-
from collections.abc import AsyncGenerator
3+
from collections.abc import Generator
44
from unittest.mock import patch
55

66
import pytest
@@ -11,25 +11,22 @@
1111
from homeassistant.core import HomeAssistant
1212
from homeassistant.helpers import entity_registry as er
1313

14-
from .util import async_init_integration
15-
1614
from tests.common import MockConfigEntry, snapshot_platform
1715

1816

1917
@pytest.fixture(autouse=True)
20-
def setup_platforms() -> AsyncGenerator[None]:
18+
def setup_platforms() -> Generator[None]:
2119
"""Set up the platforms for the tests."""
2220
with patch("homeassistant.components.tado.PLATFORMS", [Platform.SENSOR]):
2321
yield
2422

2523

24+
@pytest.mark.usefixtures("init_integration")
2625
async def test_entities(
2726
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
2827
) -> None:
2928
"""Test creation of sensor entities."""
3029

31-
await async_init_integration(hass)
32-
3330
config_entry: MockConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]
3431

3532
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)

0 commit comments

Comments
 (0)