From 62cce2d50cd212327a5a071e3a283f49b6a84a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Costa?= Date: Mon, 30 Nov 2026 20:22:45 +0000 Subject: [PATCH] Use the shared infrared config flow tests in Samsung Infrared (#195633) Co-authored-by: Claude --- .../samsung_infrared/test_config_flow.py | 149 ++---------------- 1 file changed, 15 insertions(+), 134 deletions(-) diff --git a/tests/components/samsung_infrared/test_config_flow.py b/tests/components/samsung_infrared/test_config_flow.py index 55fac96d2928..40db10566860 100644 --- a/tests/components/samsung_infrared/test_config_flow.py +++ b/tests/components/samsung_infrared/test_config_flow.py @@ -7,6 +7,9 @@ from pytest_homeassistant.components.infrared import ( EMITTER_ENTITY_ID as MOCK_INFRARED_ENTITY_ID, ) from pytest_homeassistant.components.infrared.common import MockInfraredEmitterEntity +from pytest_homeassistant.components.infrared.config_flow import ( + InfraredConsumerConfigFlowTests, +) from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, ButtonService from homeassistant.components.infrared import DATA_COMPONENT @@ -25,87 +28,35 @@ from homeassistant.helpers import entity_registry as er SECOND_EMITTER_ENTITY_ID = "infrared.second_ir_emitter" -@pytest.mark.usefixtures("mock_infrared_emitter_entity") -async def test_user_flow_success( - hass: HomeAssistant, -) -> None: - """Test successful user config flow.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) +class TestInfraredConsumerConfigFlow(InfraredConsumerConfigFlowTests): + """Test the infrared config flow steps.""" - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_DEVICE_TYPE: SamsungDeviceType.TV, - CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_ENTITY_ID, - }, - ) - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "Samsung TV via Test IR emitter" - assert result["data"] == { - CONF_DEVICE_TYPE: SamsungDeviceType.TV, - CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_ENTITY_ID, - } - assert result["result"].unique_id is None + DOMAIN = DOMAIN + EMITTER_KEY = CONF_INFRARED_EMITTER_ENTITY_ID + ENTRY_TITLE = "Samsung TV via Test IR emitter" + USER_INPUT = {CONF_DEVICE_TYPE: SamsungDeviceType.TV} @pytest.mark.usefixtures("mock_infrared_emitter_entity") -@pytest.mark.parametrize( - ("device_type", "expected_type", "expected_reason"), - [ - pytest.param( - SamsungDeviceType.TV, - FlowResultType.ABORT, - "already_configured", - id="same_type", - ), - pytest.param( - SamsungDeviceType.AC, FlowResultType.CREATE_ENTRY, None, id="other_type" - ), - ], -) -async def test_user_flow_duplicate_check( - hass: HomeAssistant, - mock_config_entry: MockConfigEntry, - device_type: SamsungDeviceType, - expected_type: FlowResultType, - expected_reason: str | None, +async def test_user_flow_same_emitter_other_device_type( + hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> None: - """Test only the same device type on the same emitter counts as a duplicate.""" + """Test another device type can use the emitter of an existing entry.""" mock_config_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) - - assert result["type"] is FlowResultType.FORM - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ - CONF_DEVICE_TYPE: device_type, + CONF_DEVICE_TYPE: SamsungDeviceType.AC, CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_ENTITY_ID, }, ) - assert result["type"] is expected_type - assert result.get("reason") == expected_reason - - -@pytest.mark.usefixtures("init_infrared") -async def test_user_flow_no_emitters(hass: HomeAssistant) -> None: - """Test user flow aborts when no infrared emitters exist.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "no_infrared_emitters" + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Samsung AC via Test IR emitter" @pytest.mark.usefixtures("mock_infrared_emitter_entity") @@ -140,36 +91,6 @@ async def test_user_flow_title_from_entity_name( assert result["title"] == expected_title -@pytest.mark.usefixtures("init_integration") -async def test_reconfigure_flow( - hass: HomeAssistant, - mock_config_entry: MockConfigEntry, - entity_registry: er.EntityRegistry, -) -> None: - """Test reconfigure keeps the device type and entry title.""" - original_title = mock_config_entry.title - entity_registry.async_update_entity(MOCK_INFRARED_ENTITY_ID, name="Lounge emitter") - - result = await mock_config_entry.start_reconfigure_flow(hass) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure" - assert CONF_DEVICE_TYPE not in result["data_schema"].schema - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_ENTITY_ID}, - ) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reconfigure_successful" - assert mock_config_entry.title == original_title - assert mock_config_entry.data == { - CONF_DEVICE_TYPE: SamsungDeviceType.TV, - CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_ENTITY_ID, - } - - @pytest.mark.usefixtures("init_integration") async def test_reconfigure_flow_different_emitter( hass: HomeAssistant, @@ -211,43 +132,3 @@ async def test_reconfigure_flow_different_emitter( assert second_emitter.send_command_calls == [SamsungTVCode.POWER.to_command()] assert mock_infrared_emitter_entity.send_command_calls == [] - - -@pytest.mark.usefixtures("mock_infrared_emitter_entity") -async def test_reconfigure_flow_already_configured( - hass: HomeAssistant, mock_config_entry: MockConfigEntry -) -> None: - """Test reconfigure aborts when another entry uses the same transmitter.""" - mock_config_entry.add_to_hass(hass) - other_entry = MockConfigEntry( - domain=DOMAIN, - data={ - CONF_DEVICE_TYPE: SamsungDeviceType.TV, - CONF_INFRARED_EMITTER_ENTITY_ID: "infrared.old_emitter", - }, - unique_id="samsung_infrared_tv_infrared.old_emitter", - ) - other_entry.add_to_hass(hass) - - result = await other_entry.start_reconfigure_flow(hass) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={CONF_INFRARED_EMITTER_ENTITY_ID: MOCK_INFRARED_ENTITY_ID}, - ) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "already_configured" - assert other_entry.data[CONF_INFRARED_EMITTER_ENTITY_ID] == "infrared.old_emitter" - - -@pytest.mark.usefixtures("init_infrared") -async def test_reconfigure_flow_no_emitters( - hass: HomeAssistant, mock_config_entry: MockConfigEntry -) -> None: - """Test reconfigure flow aborts when no infrared emitters exist.""" - mock_config_entry.add_to_hass(hass) - - result = await mock_config_entry.start_reconfigure_flow(hass) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "no_infrared_emitters"