Deduplicate group preview tests (#98883)

This commit is contained in:
Erik Montnemery 2023-08-23 19:13:24 +02:00 committed by GitHub
parent 4a417c7dcc
commit ee1b6a60a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
"""Test the Switch config flow.""" """Test the Switch config flow."""
from typing import Any
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -449,13 +450,32 @@ async def test_options_flow_hides_members(
assert registry.async_get(f"{group_type}.three").hidden_by == hidden_by assert registry.async_get(f"{group_type}.three").hidden_by == hidden_by
async def test_config_flow_binary_sensor_preview( @pytest.mark.parametrize(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator ("domain", "extra_user_input", "input_states", "group_state", "extra_attributes"),
[
("binary_sensor", {"all": True}, ["on", "off"], "off", [{}, {}]),
(
"sensor",
{"type": "max"},
["10", "20"],
"20.0",
[{"icon": "mdi:calculator"}, {"max_entity_id": "sensor.input_two"}],
),
],
)
async def test_config_flow_preview(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
domain: str,
extra_user_input: dict[str, Any],
input_states: list[str],
group_state: str,
extra_attributes: list[dict[str, Any]],
) -> None: ) -> None:
"""Test the config flow preview.""" """Test the config flow preview."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
input_entities = ["binary_sensor.input_one", "binary_sensor.input_two"] input_entities = [f"{domain}.input_one", f"{domain}.input_two"]
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -464,24 +484,21 @@ async def test_config_flow_binary_sensor_preview(
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{"next_step_id": "binary_sensor"}, {"next_step_id": domain},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "binary_sensor" assert result["step_id"] == domain
assert result["errors"] is None assert result["errors"] is None
assert result["preview"] == "group_binary_sensor" assert result["preview"] == f"group_{domain}"
await client.send_json_auto_id( await client.send_json_auto_id(
{ {
"type": "group/binary_sensor/start_preview", "type": f"group/{domain}/start_preview",
"flow_id": result["flow_id"], "flow_id": result["flow_id"],
"flow_type": "config_flow", "flow_type": "config_flow",
"user_input": { "user_input": {"name": "My group", "entities": input_entities}
"name": "My binary sensor group", | extra_user_input,
"entities": input_entities,
"all": True,
},
} }
) )
msg = await client.receive_json() msg = await client.receive_json()
@ -490,151 +507,60 @@ async def test_config_flow_binary_sensor_preview(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["event"] == { assert msg["event"] == {
"attributes": {"friendly_name": "My binary sensor group"}, "attributes": {"friendly_name": "My group"} | extra_attributes[0],
"state": "unavailable", "state": "unavailable",
} }
hass.states.async_set("binary_sensor.input_one", "on") hass.states.async_set(input_entities[0], input_states[0])
hass.states.async_set("binary_sensor.input_two", "off") hass.states.async_set(input_entities[1], input_states[1])
msg = await client.receive_json()
assert msg["event"] == {
"attributes": {
"entity_id": ["binary_sensor.input_one", "binary_sensor.input_two"],
"friendly_name": "My binary sensor group",
},
"state": "off",
}
async def test_option_flow_binary_sensor_preview(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None:
"""Test the option flow preview."""
client = await hass_ws_client(hass)
input_entities = ["binary_sensor.input_one", "binary_sensor.input_two"]
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"all": True,
"entities": input_entities,
"group_type": "binary_sensor",
"hide_members": False,
"name": "My group",
},
title="My min_max",
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
assert result["preview"] == "group_binary_sensor"
hass.states.async_set("binary_sensor.input_one", "on")
hass.states.async_set("binary_sensor.input_two", "off")
await client.send_json_auto_id(
{
"type": "group/binary_sensor/start_preview",
"flow_id": result["flow_id"],
"flow_type": "options_flow",
"user_input": {
"entities": input_entities,
"all": False,
},
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
msg = await client.receive_json() msg = await client.receive_json()
assert msg["event"] == { assert msg["event"] == {
"attributes": { "attributes": {
"entity_id": input_entities, "entity_id": input_entities,
"friendly_name": "My group", "friendly_name": "My group",
}, }
"state": "on", | extra_attributes[0]
| extra_attributes[1],
"state": group_state,
} }
async def test_config_flow_sensor_preview( @pytest.mark.parametrize(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator (
) -> None: "domain",
"""Test the config flow preview.""" "extra_config_flow_data",
client = await hass_ws_client(hass) "extra_user_input",
"input_states",
input_entities = ["sensor.input_one", "sensor.input_two"] "group_state",
"extra_attributes",
result = await hass.config_entries.flow.async_init( ),
DOMAIN, context={"source": config_entries.SOURCE_USER} [
) ("binary_sensor", {"all": True}, {"all": False}, ["on", "off"], "on", {}),
assert result["type"] == FlowResultType.MENU (
"sensor",
result = await hass.config_entries.flow.async_configure( {"type": "min"},
result["flow_id"], {"type": "max"},
{"next_step_id": "sensor"}, ["10", "20"],
) "20.0",
await hass.async_block_till_done() {"icon": "mdi:calculator", "max_entity_id": "sensor.input_two"},
assert result["type"] == FlowResultType.FORM ),
assert result["step_id"] == "sensor" ],
assert result["errors"] is None )
assert result["preview"] == "group_sensor" async def test_option_flow_preview(
hass: HomeAssistant,
await client.send_json_auto_id( hass_ws_client: WebSocketGenerator,
{ domain: str,
"type": "group/sensor/start_preview", extra_config_flow_data: dict[str, Any],
"flow_id": result["flow_id"], extra_user_input: dict[str, Any],
"flow_type": "config_flow", input_states: list[str],
"user_input": { group_state: str,
"name": "My sensor group", extra_attributes: dict[str, Any],
"entities": input_entities,
"type": "max",
},
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
msg = await client.receive_json()
assert msg["event"] == {
"attributes": {
"friendly_name": "My sensor group",
"icon": "mdi:calculator",
},
"state": "unavailable",
}
hass.states.async_set("sensor.input_one", "10")
hass.states.async_set("sensor.input_two", "20")
msg = await client.receive_json()
assert msg["event"] == {
"attributes": {
"entity_id": input_entities,
"friendly_name": "My sensor group",
"icon": "mdi:calculator",
"max_entity_id": "sensor.input_two",
},
"state": "20.0",
}
async def test_option_flow_sensor_preview(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None: ) -> None:
"""Test the option flow preview.""" """Test the option flow preview."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
input_entities = ["sensor.input_one", "sensor.input_two"] input_entities = [f"{domain}.input_one", f"{domain}.input_two"]
# Setup the config entry # Setup the config entry
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
@ -642,12 +568,12 @@ async def test_option_flow_sensor_preview(
domain=DOMAIN, domain=DOMAIN,
options={ options={
"entities": input_entities, "entities": input_entities,
"group_type": "sensor", "group_type": domain,
"hide_members": False, "hide_members": False,
"name": "My sensor group", "name": "My group",
"type": "min", }
}, | extra_config_flow_data,
title="My min_max", title="My group",
) )
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -656,20 +582,17 @@ async def test_option_flow_sensor_preview(
result = await hass.config_entries.options.async_init(config_entry.entry_id) result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result["errors"] is None assert result["errors"] is None
assert result["preview"] == "group_sensor" assert result["preview"] == f"group_{domain}"
hass.states.async_set("sensor.input_one", "10") hass.states.async_set(input_entities[0], input_states[0])
hass.states.async_set("sensor.input_two", "20") hass.states.async_set(input_entities[1], input_states[1])
await client.send_json_auto_id( await client.send_json_auto_id(
{ {
"type": "group/sensor/start_preview", "type": f"group/{domain}/start_preview",
"flow_id": result["flow_id"], "flow_id": result["flow_id"],
"flow_type": "options_flow", "flow_type": "options_flow",
"user_input": { "user_input": {"entities": input_entities} | extra_user_input,
"entities": input_entities,
"type": "min",
},
} }
) )
msg = await client.receive_json() msg = await client.receive_json()
@ -678,13 +601,9 @@ async def test_option_flow_sensor_preview(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["event"] == { assert msg["event"] == {
"attributes": { "attributes": {"entity_id": input_entities, "friendly_name": "My group"}
"entity_id": input_entities, | extra_attributes,
"friendly_name": "My sensor group", "state": group_state,
"icon": "mdi:calculator",
"min_entity_id": "sensor.input_one",
},
"state": "10.0",
} }