|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from unittest.mock import AsyncMock |
| 6 | + |
5 | 7 | from pysaunum import SaunumConnectionError, SaunumException |
6 | 8 | import pytest |
7 | 9 |
|
|
14 | 16 | from tests.common import MockConfigEntry |
15 | 17 |
|
16 | 18 | TEST_USER_INPUT = {CONF_HOST: "192.168.1.100"} |
| 19 | +TEST_RECONFIGURE_INPUT = {CONF_HOST: "192.168.1.200"} |
17 | 20 |
|
18 | 21 |
|
19 | 22 | @pytest.mark.usefixtures("mock_saunum_client") |
20 | | -async def test_full_flow(hass: HomeAssistant) -> None: |
| 23 | +async def test_full_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: |
21 | 24 | """Test full flow.""" |
22 | 25 | result = await hass.config_entries.flow.async_init( |
23 | 26 | DOMAIN, context={"source": SOURCE_USER} |
@@ -49,6 +52,7 @@ async def test_form_errors( |
49 | 52 | mock_saunum_client, |
50 | 53 | side_effect: Exception, |
51 | 54 | error_base: str, |
| 55 | + mock_setup_entry: AsyncMock, |
52 | 56 | ) -> None: |
53 | 57 | """Test error handling and recovery.""" |
54 | 58 | mock_saunum_client.connect.side_effect = side_effect |
@@ -96,3 +100,105 @@ async def test_form_duplicate( |
96 | 100 |
|
97 | 101 | assert result["type"] is FlowResultType.ABORT |
98 | 102 | assert result["reason"] == "already_configured" |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.usefixtures("mock_saunum_client") |
| 106 | +@pytest.mark.parametrize("user_input", [TEST_RECONFIGURE_INPUT, TEST_USER_INPUT]) |
| 107 | +async def test_reconfigure_flow( |
| 108 | + hass: HomeAssistant, |
| 109 | + mock_config_entry: MockConfigEntry, |
| 110 | + user_input: dict[str, str], |
| 111 | + mock_setup_entry: AsyncMock, |
| 112 | +) -> None: |
| 113 | + """Test reconfigure flow.""" |
| 114 | + mock_config_entry.add_to_hass(hass) |
| 115 | + |
| 116 | + result = await mock_config_entry.start_reconfigure_flow(hass) |
| 117 | + |
| 118 | + assert result["type"] is FlowResultType.FORM |
| 119 | + assert result["step_id"] == "user" |
| 120 | + |
| 121 | + result = await hass.config_entries.flow.async_configure( |
| 122 | + result["flow_id"], |
| 123 | + user_input, |
| 124 | + ) |
| 125 | + |
| 126 | + assert result["type"] is FlowResultType.ABORT |
| 127 | + assert result["reason"] == "reconfigure_successful" |
| 128 | + assert mock_config_entry.data == user_input |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.parametrize( |
| 132 | + ("side_effect", "error_base"), |
| 133 | + [ |
| 134 | + (SaunumConnectionError("Connection failed"), "cannot_connect"), |
| 135 | + (SaunumException("Read error"), "cannot_connect"), |
| 136 | + (Exception("Unexpected error"), "unknown"), |
| 137 | + ], |
| 138 | +) |
| 139 | +async def test_reconfigure_errors( |
| 140 | + hass: HomeAssistant, |
| 141 | + mock_config_entry: MockConfigEntry, |
| 142 | + mock_saunum_client, |
| 143 | + side_effect: Exception, |
| 144 | + error_base: str, |
| 145 | + mock_setup_entry: AsyncMock, |
| 146 | +) -> None: |
| 147 | + """Test reconfigure flow error handling.""" |
| 148 | + mock_config_entry.add_to_hass(hass) |
| 149 | + mock_saunum_client.connect.side_effect = side_effect |
| 150 | + |
| 151 | + result = await mock_config_entry.start_reconfigure_flow(hass) |
| 152 | + |
| 153 | + assert result["type"] is FlowResultType.FORM |
| 154 | + assert result["step_id"] == "user" |
| 155 | + |
| 156 | + result = await hass.config_entries.flow.async_configure( |
| 157 | + result["flow_id"], |
| 158 | + TEST_RECONFIGURE_INPUT, |
| 159 | + ) |
| 160 | + |
| 161 | + assert result["type"] is FlowResultType.FORM |
| 162 | + assert result["errors"] == {"base": error_base} |
| 163 | + |
| 164 | + # Test recovery - clear the error and try again |
| 165 | + mock_saunum_client.connect.side_effect = None |
| 166 | + |
| 167 | + result = await hass.config_entries.flow.async_configure( |
| 168 | + result["flow_id"], |
| 169 | + TEST_RECONFIGURE_INPUT, |
| 170 | + ) |
| 171 | + |
| 172 | + assert result["type"] is FlowResultType.ABORT |
| 173 | + assert result["reason"] == "reconfigure_successful" |
| 174 | + assert mock_config_entry.data == TEST_RECONFIGURE_INPUT |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.usefixtures("mock_saunum_client") |
| 178 | +async def test_reconfigure_to_existing_host( |
| 179 | + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_setup_entry: AsyncMock |
| 180 | +) -> None: |
| 181 | + """Test reconfigure flow aborts when changing to a host used by another entry.""" |
| 182 | + mock_config_entry.add_to_hass(hass) |
| 183 | + |
| 184 | + # Create a second entry with a different host |
| 185 | + second_entry = MockConfigEntry( |
| 186 | + domain=DOMAIN, |
| 187 | + data=TEST_RECONFIGURE_INPUT, |
| 188 | + title="Saunum 2", |
| 189 | + ) |
| 190 | + second_entry.add_to_hass(hass) |
| 191 | + |
| 192 | + result = await mock_config_entry.start_reconfigure_flow(hass) |
| 193 | + |
| 194 | + # Try to reconfigure first entry to use the same host as second entry |
| 195 | + result = await hass.config_entries.flow.async_configure( |
| 196 | + result["flow_id"], |
| 197 | + TEST_RECONFIGURE_INPUT, # Same host as second_entry |
| 198 | + ) |
| 199 | + |
| 200 | + assert result["type"] is FlowResultType.ABORT |
| 201 | + assert result["reason"] == "already_configured" |
| 202 | + |
| 203 | + # Verify the original entry was not changed |
| 204 | + assert mock_config_entry.data == TEST_USER_INPUT |
0 commit comments