Skip to content

Commit 768c3e1

Browse files
authored
Create and use config_entry fixture for Axis integration tests (home-assistant#85865)
1 parent 8fbcb93 commit 768c3e1

File tree

9 files changed

+153
-117
lines changed

9 files changed

+153
-117
lines changed

tests/components/axis/conftest.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,83 @@
66
from axis.rtsp import Signal, State
77
import pytest
88

9+
from homeassistant.components.axis.const import CONF_EVENTS, DOMAIN as AXIS_DOMAIN
10+
from homeassistant.const import (
11+
CONF_HOST,
12+
CONF_MODEL,
13+
CONF_NAME,
14+
CONF_PASSWORD,
15+
CONF_PORT,
16+
CONF_USERNAME,
17+
)
18+
19+
from tests.common import MockConfigEntry
920
from tests.components.light.conftest import mock_light_profiles # noqa: F401
1021

22+
MAC = "00408C123456"
23+
FORMATTED_MAC = "00:40:8c:12:34:56"
24+
MODEL = "model"
25+
NAME = "name"
26+
27+
DEFAULT_HOST = "1.2.3.4"
28+
29+
ENTRY_OPTIONS = {CONF_EVENTS: True}
30+
31+
ENTRY_CONFIG = {
32+
CONF_HOST: DEFAULT_HOST,
33+
CONF_USERNAME: "root",
34+
CONF_PASSWORD: "pass",
35+
CONF_PORT: 80,
36+
CONF_MODEL: MODEL,
37+
CONF_NAME: NAME,
38+
}
39+
40+
41+
@pytest.fixture(name="config_entry")
42+
def config_entry_fixture(hass, config, options, config_entry_version):
43+
"""Define a config entry fixture."""
44+
entry = MockConfigEntry(
45+
domain=AXIS_DOMAIN,
46+
unique_id=FORMATTED_MAC,
47+
data=config,
48+
options=options,
49+
version=config_entry_version,
50+
)
51+
entry.add_to_hass(hass)
52+
return entry
53+
54+
55+
@pytest.fixture(name="config_entry_version")
56+
def config_entry_version_fixture(request):
57+
"""Define a config entry version fixture.
58+
59+
@pytest.mark.config_entry_version(int)
60+
"""
61+
marker = request.node.get_closest_marker("config_entry_version")
62+
version = 3
63+
if marker:
64+
version = marker.args[0]
65+
return version
66+
67+
68+
@pytest.fixture(name="config")
69+
def config_fixture():
70+
"""Define a config entry data fixture."""
71+
return ENTRY_CONFIG.copy()
72+
73+
74+
@pytest.fixture(name="options")
75+
def options_fixture(request):
76+
"""Define a config entry options fixture.
77+
78+
@pytest.mark.config_entry_options(dict)
79+
"""
80+
marker = request.node.get_closest_marker("config_entry_options")
81+
options = ENTRY_OPTIONS.copy()
82+
if marker:
83+
options = marker.args[0]
84+
return options
85+
1186

1287
@pytest.fixture(autouse=True)
1388
def mock_axis_rtspclient():

tests/components/axis/test_binary_sensor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from homeassistant.const import STATE_OFF, STATE_ON
99
from homeassistant.setup import async_setup_component
1010

11-
from .test_device import NAME, setup_axis_integration
11+
from .conftest import NAME
12+
from .test_device import setup_axis_integration
1213

1314

1415
async def test_platform_manually_configured(hass):
@@ -25,16 +26,16 @@ async def test_platform_manually_configured(hass):
2526
assert AXIS_DOMAIN not in hass.data
2627

2728

28-
async def test_no_binary_sensors(hass):
29+
async def test_no_binary_sensors(hass, config_entry):
2930
"""Test that no sensors in Axis results in no sensor entities."""
30-
await setup_axis_integration(hass)
31+
await setup_axis_integration(hass, config_entry)
3132

3233
assert not hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)
3334

3435

35-
async def test_binary_sensors(hass, mock_rtsp_event):
36+
async def test_binary_sensors(hass, config_entry, mock_rtsp_event):
3637
"""Test that sensors are loaded properly."""
37-
await setup_axis_integration(hass)
38+
await setup_axis_integration(hass, config_entry)
3839

3940
mock_rtsp_event(
4041
topic="tns1:Device/tnsaxis:Sensor/PIR",

tests/components/axis/test_camera.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from unittest.mock import patch
44

5+
import pytest
6+
57
from homeassistant.components import camera
68
from homeassistant.components.axis.const import (
79
CONF_STREAM_PROFILE,
@@ -11,7 +13,8 @@
1113
from homeassistant.const import STATE_IDLE
1214
from homeassistant.setup import async_setup_component
1315

14-
from .test_device import ENTRY_OPTIONS, NAME, setup_axis_integration
16+
from .conftest import NAME
17+
from .test_device import setup_axis_integration
1518

1619

1720
async def test_platform_manually_configured(hass):
@@ -26,9 +29,9 @@ async def test_platform_manually_configured(hass):
2629
assert AXIS_DOMAIN not in hass.data
2730

2831

29-
async def test_camera(hass):
32+
async def test_camera(hass, config_entry):
3033
"""Test that Axis camera platform is loaded properly."""
31-
await setup_axis_integration(hass)
34+
await setup_axis_integration(hass, config_entry)
3235

3336
assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 1
3437

@@ -47,10 +50,10 @@ async def test_camera(hass):
4750
)
4851

4952

50-
async def test_camera_with_stream_profile(hass):
53+
@pytest.mark.config_entry_options({CONF_STREAM_PROFILE: "profile_1"})
54+
async def test_camera_with_stream_profile(hass, config_entry):
5155
"""Test that Axis camera entity is using the correct path with stream profike."""
52-
with patch.dict(ENTRY_OPTIONS, {CONF_STREAM_PROFILE: "profile_1"}):
53-
await setup_axis_integration(hass)
56+
await setup_axis_integration(hass, config_entry)
5457

5558
assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 1
5659

@@ -72,9 +75,9 @@ async def test_camera_with_stream_profile(hass):
7275
)
7376

7477

75-
async def test_camera_disabled(hass):
78+
async def test_camera_disabled(hass, config_entry):
7679
"""Test that Axis camera platform is loaded properly but does not create camera entity."""
7780
with patch("axis.vapix.vapix.Params.image_format", new=None):
78-
await setup_axis_integration(hass)
81+
await setup_axis_integration(hass, config_entry)
7982

8083
assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 0

tests/components/axis/test_config_flow.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,8 @@
3232
)
3333
from homeassistant.data_entry_flow import FlowResultType
3434

35-
from .test_device import (
36-
DEFAULT_HOST,
37-
MAC,
38-
MODEL,
39-
NAME,
40-
mock_default_vapix_requests,
41-
setup_axis_integration,
42-
)
35+
from .conftest import DEFAULT_HOST, MAC, MODEL, NAME
36+
from .test_device import mock_default_vapix_requests, setup_axis_integration
4337

4438
from tests.common import MockConfigEntry
4539

@@ -79,9 +73,9 @@ async def test_flow_manual_configuration(hass):
7973
}
8074

8175

82-
async def test_manual_configuration_update_configuration(hass):
76+
async def test_manual_configuration_update_configuration(hass, config_entry):
8377
"""Test that config flow fails on already configured device."""
84-
config_entry = await setup_axis_integration(hass)
78+
await setup_axis_integration(hass, config_entry)
8579
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
8680

8781
result = await hass.config_entries.flow.async_init(
@@ -211,9 +205,9 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(hass):
211205
assert result["data"][CONF_NAME] == "M1065-LW 2"
212206

213207

214-
async def test_reauth_flow_update_configuration(hass):
208+
async def test_reauth_flow_update_configuration(hass, config_entry):
215209
"""Test that config flow fails on already configured device."""
216-
config_entry = await setup_axis_integration(hass)
210+
await setup_axis_integration(hass, config_entry)
217211
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
218212

219213
result = await hass.config_entries.flow.async_init(
@@ -383,10 +377,10 @@ async def test_discovery_flow(hass, source: str, discovery_info: dict):
383377
],
384378
)
385379
async def test_discovered_device_already_configured(
386-
hass, source: str, discovery_info: dict
380+
hass, config_entry, source: str, discovery_info: dict
387381
):
388382
"""Test that discovery doesn't setup already configured devices."""
389-
config_entry = await setup_axis_integration(hass)
383+
await setup_axis_integration(hass, config_entry)
390384
assert config_entry.data[CONF_HOST] == DEFAULT_HOST
391385

392386
result = await hass.config_entries.flow.async_init(
@@ -439,10 +433,10 @@ async def test_discovered_device_already_configured(
439433
],
440434
)
441435
async def test_discovery_flow_updated_configuration(
442-
hass, source: str, discovery_info: dict, expected_port: int
436+
hass, config_entry, source: str, discovery_info: dict, expected_port: int
443437
):
444438
"""Test that discovery flow update configuration with new parameters."""
445-
config_entry = await setup_axis_integration(hass)
439+
await setup_axis_integration(hass, config_entry)
446440
assert config_entry.data == {
447441
CONF_HOST: DEFAULT_HOST,
448442
CONF_PORT: 80,
@@ -573,9 +567,9 @@ async def test_discovery_flow_ignore_link_local_address(
573567
assert result["reason"] == "link_local_address"
574568

575569

576-
async def test_option_flow(hass):
570+
async def test_option_flow(hass, config_entry):
577571
"""Test config flow options."""
578-
config_entry = await setup_axis_integration(hass)
572+
await setup_axis_integration(hass, config_entry)
579573
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
580574
assert device.option_stream_profile == DEFAULT_STREAM_PROFILE
581575
assert device.option_video_source == DEFAULT_VIDEO_SOURCE

0 commit comments

Comments
 (0)