Skip to content

Commit 42cfe3a

Browse files
Add config flow tests
1 parent d0e3859 commit 42cfe3a

File tree

3 files changed

+109
-112
lines changed

3 files changed

+109
-112
lines changed

tests/conftest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import pytest
1010
from homeassistant.config_entries import SOURCE_USER
1111
from homeassistant.const import (
12-
CONF_ENTITY_ID,
1312
CONF_NAME,
14-
CONF_TYPE,
1513
)
1614
from homeassistant.core import HomeAssistant
1715
from pytest_homeassistant_custom_component.common import MockConfigEntry
1816

19-
from custom_components.calendar_event.const import DOMAIN
17+
from custom_components.calendar_event.const import (
18+
CONF_CALENDAR_ENTITY_ID,
19+
CONF_COMPARISON_METHOD,
20+
CONF_SUMMARY,
21+
DOMAIN,
22+
)
2023

2124
pytest_plugins = "pytest_homeassistant_custom_component"
2225

@@ -48,8 +51,9 @@ async def get_config_to_integration_load() -> dict[str, Any]:
4851
"""
4952
return {
5053
CONF_NAME: "My calendar_event sensor",
51-
CONF_ENTITY_ID: "sensor.test_monitored",
52-
CONF_TYPE: "max",
54+
CONF_CALENDAR_ENTITY_ID: "calendar.my_calendar",
55+
CONF_SUMMARY: "Test Event",
56+
CONF_COMPARISON_METHOD: "contains",
5357
}
5458

5559

tests/test_config_flow.py

Lines changed: 92 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,135 +11,131 @@
1111
from homeassistant.data_entry_flow import FlowResultType
1212

1313
from custom_components.calendar_event.const import (
14-
CONF_LABEL,
15-
CONF_STATE_LOWER_LIMIT,
16-
CONF_STATE_NOT,
17-
CONF_STATE_TO,
18-
CONF_STATE_TYPE,
19-
CONF_STATE_UPPER_LIMIT,
14+
CONF_CALENDAR_ENTITY_ID,
15+
CONF_COMPARISON_METHOD,
16+
CONF_SUMMARY,
2017
DOMAIN,
2118
)
2219

2320

2421
@pytest.mark.parametrize(
2522
(
2623
"name",
27-
"state_type",
28-
"label",
29-
"state_to",
30-
"state_not",
31-
"state_lower_limit",
32-
"state_upper_limit",
24+
"calendar_entity_id",
25+
"summary",
26+
"comparison_method",
3327
),
3428
[
3529
(
36-
"Unavailable",
37-
"state",
38-
"my_label",
39-
"unavailable",
40-
None,
41-
None,
42-
None,
30+
"Test Calendar Event",
31+
"calendar.my_calendar",
32+
"Meeting",
33+
"contains",
4334
),
4435
(
45-
"On",
46-
"state_not",
47-
"my_label",
48-
None,
49-
"on",
50-
None,
51-
None,
36+
"Another Test",
37+
"calendar.work_calendar",
38+
"Doctor",
39+
"starts_with",
5240
),
5341
(
54-
"Numeric State",
55-
"numeric_state",
56-
"my_label",
57-
None,
58-
None,
59-
10,
60-
20,
42+
"Third Test",
43+
"calendar.personal",
44+
"Appointment",
45+
"ends_with",
46+
),
47+
(
48+
"Exact Match Test",
49+
"calendar.events",
50+
"Birthday Party",
51+
"exactly",
6152
),
6253
],
6354
)
6455
async def test_config_flow(
6556
hass: HomeAssistant,
6657
name: str,
67-
state_type: str,
68-
label: str,
69-
state_to: str | None,
70-
state_not: str | None,
71-
state_lower_limit: float | None,
72-
state_upper_limit: float | None,
58+
calendar_entity_id: str,
59+
summary: str,
60+
comparison_method: str,
7361
mock_setup_entry: AsyncMock,
7462
) -> None:
7563
"""Test the config flow."""
7664

77-
menu_step = await hass.config_entries.flow.async_init(
65+
result = await hass.config_entries.flow.async_init(
7866
DOMAIN, context={"source": config_entries.SOURCE_USER}
7967
)
80-
assert menu_step.get("type") is FlowResultType.MENU
81-
assert menu_step.get("step_id") == "user"
68+
assert result.get("type") is FlowResultType.FORM
69+
assert result.get("step_id") == "user"
8270

83-
form_step = await hass.config_entries.flow.async_configure(
84-
menu_step["flow_id"],
85-
{"next_step_id": state_type},
71+
result = await hass.config_entries.flow.async_configure(
72+
result["flow_id"],
73+
{
74+
CONF_NAME: name,
75+
CONF_CALENDAR_ENTITY_ID: calendar_entity_id,
76+
CONF_SUMMARY: summary,
77+
CONF_COMPARISON_METHOD: comparison_method,
78+
},
8679
)
8780

88-
if state_type == "numeric_state":
89-
result = await hass.config_entries.flow.async_configure(
90-
form_step["flow_id"],
91-
{
92-
CONF_NAME: name,
93-
CONF_LABEL: label,
94-
CONF_STATE_LOWER_LIMIT: state_lower_limit,
95-
CONF_STATE_UPPER_LIMIT: state_upper_limit,
96-
},
97-
)
98-
elif state_type == "state":
99-
result = await hass.config_entries.flow.async_configure(
100-
form_step["flow_id"],
101-
{
102-
CONF_NAME: name,
103-
CONF_LABEL: label,
104-
CONF_STATE_TO: state_to,
105-
},
106-
)
107-
elif state_type == "state_not":
108-
result = await hass.config_entries.flow.async_configure(
109-
form_step["flow_id"],
110-
{
111-
CONF_NAME: name,
112-
CONF_LABEL: label,
113-
CONF_STATE_NOT: state_not,
114-
},
115-
)
116-
11781
await hass.async_block_till_done()
11882

11983
assert result.get("type") is FlowResultType.CREATE_ENTRY
12084
assert result.get("version") == 1
85+
assert result.get("title") == name
12186

122-
if state_type == "numeric_state":
123-
assert result.get("options") == {
124-
CONF_NAME: name,
125-
CONF_STATE_TYPE: state_type,
126-
CONF_LABEL: label,
127-
CONF_STATE_LOWER_LIMIT: state_lower_limit,
128-
CONF_STATE_UPPER_LIMIT: state_upper_limit,
129-
}
130-
elif state_type == "state":
131-
assert result.get("options") == {
132-
CONF_NAME: name,
133-
CONF_STATE_TYPE: state_type,
134-
CONF_LABEL: label,
135-
CONF_STATE_TO: state_to,
136-
}
137-
elif state_type == "state_not":
138-
assert result.get("options") == {
139-
CONF_NAME: name,
140-
CONF_STATE_TYPE: state_type,
141-
CONF_LABEL: label,
142-
CONF_STATE_NOT: state_not,
143-
}
87+
assert result.get("options") == {
88+
CONF_NAME: name,
89+
CONF_CALENDAR_ENTITY_ID: calendar_entity_id,
90+
CONF_SUMMARY: summary,
91+
CONF_COMPARISON_METHOD: comparison_method,
92+
}
14493

14594
assert len(mock_setup_entry.mock_calls) == 1
95+
96+
97+
async def test_options_flow(
98+
hass: HomeAssistant,
99+
mock_setup_entry: AsyncMock,
100+
) -> None:
101+
"""Test the options flow."""
102+
from pytest_homeassistant_custom_component.common import MockConfigEntry
103+
104+
# Create a config entry
105+
config_entry = MockConfigEntry(
106+
domain=DOMAIN,
107+
data={},
108+
options={
109+
CONF_NAME: "Original Name",
110+
CONF_CALENDAR_ENTITY_ID: "calendar.original",
111+
CONF_SUMMARY: "Original Summary",
112+
CONF_COMPARISON_METHOD: "contains",
113+
},
114+
title="Original Name",
115+
)
116+
config_entry.add_to_hass(hass)
117+
118+
# Start the options flow
119+
result = await hass.config_entries.options.async_init(config_entry.entry_id)
120+
assert result.get("type") is FlowResultType.FORM
121+
assert result.get("step_id") == "init"
122+
123+
# Configure the options
124+
result = await hass.config_entries.options.async_configure(
125+
result["flow_id"],
126+
{
127+
CONF_CALENDAR_ENTITY_ID: "calendar.updated",
128+
CONF_SUMMARY: "Updated Summary",
129+
CONF_COMPARISON_METHOD: "starts_with",
130+
},
131+
)
132+
133+
await hass.async_block_till_done()
134+
135+
assert result.get("type") is FlowResultType.CREATE_ENTRY
136+
assert result.get("data") == {
137+
CONF_NAME: "Original Name",
138+
CONF_CALENDAR_ENTITY_ID: "calendar.updated",
139+
CONF_SUMMARY: "Updated Summary",
140+
CONF_COMPARISON_METHOD: "starts_with",
141+
}

tests/test_init.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from homeassistant.helpers import label_registry as lr
1010
from pytest_homeassistant_custom_component.common import MockConfigEntry
1111

12-
from custom_components.calendar_event.const import DOMAIN
12+
from custom_components.calendar_event.const import (
13+
DOMAIN,
14+
)
1315

1416
from .const import DEFAULT_NAME
1517

@@ -41,32 +43,27 @@ async def test_setup(
4143
identifiers={("sensor", "test_source")},
4244
)
4345

44-
test_label = label_registry.async_create(
45-
"test",
46-
)
47-
4846
# Source entity registry
4947
source_entity = entity_registry.async_get_or_create(
50-
"sensor",
48+
"calendar",
5149
"test",
5250
"source",
5351
config_entry=source_config_entry,
5452
device_id=source_device_entry.id,
5553
)
56-
source_entity.labels.add(test_label.label_id)
5754

5855
await hass.async_block_till_done()
59-
assert entity_registry.async_get("sensor.test_source") is not None
56+
assert entity_registry.async_get("calendar.my_calendar") is not None
6057

6158
# Configure the configuration entry for PeriodicMinMax
6259
calendar_event_config_entry = MockConfigEntry(
6360
data={},
6461
domain=DOMAIN,
6562
options={
6663
"name": DEFAULT_NAME,
67-
"label": test_label.label_id,
68-
"state_type": "state",
69-
"state_to": "on",
64+
"calendar_entity_id": "calendar.my_calendar",
65+
"summary": "Test Event",
66+
"comparison_method": "contains",
7067
},
7168
title=DEFAULT_NAME,
7269
)

0 commit comments

Comments
 (0)