|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | from homeassistant.components.pyload.const import DEFAULT_NAME, DOMAIN |
9 | | -from homeassistant.config_entries import SOURCE_USER |
| 9 | +from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_IGNORE, SOURCE_USER |
| 10 | +from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL |
10 | 11 | from homeassistant.core import HomeAssistant |
11 | 12 | from homeassistant.data_entry_flow import FlowResultType |
12 | 13 |
|
13 | | -from .conftest import NEW_INPUT, REAUTH_INPUT, USER_INPUT |
| 14 | +from .conftest import ( |
| 15 | + ADDON_DISCOVERY_INFO, |
| 16 | + ADDON_SERVICE_INFO, |
| 17 | + NEW_INPUT, |
| 18 | + REAUTH_INPUT, |
| 19 | + USER_INPUT, |
| 20 | +) |
14 | 21 |
|
15 | 22 | from tests.common import MockConfigEntry |
16 | 23 |
|
@@ -245,3 +252,183 @@ async def test_reconfigure_errors( |
245 | 252 | assert result["reason"] == "reconfigure_successful" |
246 | 253 | assert config_entry.data == USER_INPUT |
247 | 254 | assert len(hass.config_entries.async_entries()) == 1 |
| 255 | + |
| 256 | + |
| 257 | +async def test_hassio_discovery( |
| 258 | + hass: HomeAssistant, |
| 259 | + mock_setup_entry: AsyncMock, |
| 260 | + mock_pyloadapi: AsyncMock, |
| 261 | +) -> None: |
| 262 | + """Test flow started from Supervisor discovery.""" |
| 263 | + |
| 264 | + mock_pyloadapi.login.side_effect = InvalidAuth |
| 265 | + |
| 266 | + result = await hass.config_entries.flow.async_init( |
| 267 | + DOMAIN, |
| 268 | + data=ADDON_SERVICE_INFO, |
| 269 | + context={"source": SOURCE_HASSIO}, |
| 270 | + ) |
| 271 | + |
| 272 | + assert result["type"] is FlowResultType.FORM |
| 273 | + assert result["step_id"] == "hassio_confirm" |
| 274 | + assert result["errors"] is None |
| 275 | + |
| 276 | + mock_pyloadapi.login.side_effect = None |
| 277 | + |
| 278 | + result = await hass.config_entries.flow.async_configure( |
| 279 | + result["flow_id"], {CONF_USERNAME: "pyload", CONF_PASSWORD: "pyload"} |
| 280 | + ) |
| 281 | + assert result["type"] is FlowResultType.CREATE_ENTRY |
| 282 | + assert result["title"] == "p539df76c_pyload-ng" |
| 283 | + assert result["data"] == {**ADDON_DISCOVERY_INFO, CONF_VERIFY_SSL: False} |
| 284 | + assert len(mock_setup_entry.mock_calls) == 1 |
| 285 | + |
| 286 | + |
| 287 | +@pytest.mark.usefixtures("mock_pyloadapi") |
| 288 | +async def test_hassio_discovery_confirm_only( |
| 289 | + hass: HomeAssistant, |
| 290 | + mock_setup_entry: AsyncMock, |
| 291 | +) -> None: |
| 292 | + """Test flow started from Supervisor discovery. Abort with confirm only.""" |
| 293 | + |
| 294 | + result = await hass.config_entries.flow.async_init( |
| 295 | + DOMAIN, |
| 296 | + data=ADDON_SERVICE_INFO, |
| 297 | + context={"source": SOURCE_HASSIO}, |
| 298 | + ) |
| 299 | + |
| 300 | + assert result["type"] is FlowResultType.FORM |
| 301 | + assert result["step_id"] == "hassio_confirm" |
| 302 | + |
| 303 | + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) |
| 304 | + |
| 305 | + assert result["type"] is FlowResultType.CREATE_ENTRY |
| 306 | + assert result["title"] == "p539df76c_pyload-ng" |
| 307 | + assert result["data"] == {**ADDON_DISCOVERY_INFO, CONF_VERIFY_SSL: False} |
| 308 | + assert len(mock_setup_entry.mock_calls) == 1 |
| 309 | + |
| 310 | + |
| 311 | +@pytest.mark.parametrize( |
| 312 | + ("side_effect", "error_text"), |
| 313 | + [ |
| 314 | + (InvalidAuth, "invalid_auth"), |
| 315 | + (CannotConnect, "cannot_connect"), |
| 316 | + (IndexError, "unknown"), |
| 317 | + ], |
| 318 | +) |
| 319 | +async def test_hassio_discovery_errors( |
| 320 | + hass: HomeAssistant, |
| 321 | + mock_setup_entry: AsyncMock, |
| 322 | + mock_pyloadapi: AsyncMock, |
| 323 | + side_effect: Exception, |
| 324 | + error_text: str, |
| 325 | +) -> None: |
| 326 | + """Test flow started from Supervisor discovery.""" |
| 327 | + |
| 328 | + mock_pyloadapi.login.side_effect = side_effect |
| 329 | + |
| 330 | + result = await hass.config_entries.flow.async_init( |
| 331 | + DOMAIN, |
| 332 | + data=ADDON_SERVICE_INFO, |
| 333 | + context={"source": SOURCE_HASSIO}, |
| 334 | + ) |
| 335 | + |
| 336 | + assert result["type"] is FlowResultType.FORM |
| 337 | + assert result["step_id"] == "hassio_confirm" |
| 338 | + assert result["errors"] is None |
| 339 | + |
| 340 | + result = await hass.config_entries.flow.async_configure( |
| 341 | + result["flow_id"], {CONF_USERNAME: "pyload", CONF_PASSWORD: "pyload"} |
| 342 | + ) |
| 343 | + |
| 344 | + assert result["type"] is FlowResultType.FORM |
| 345 | + assert result["errors"] == {"base": error_text} |
| 346 | + |
| 347 | + mock_pyloadapi.login.side_effect = None |
| 348 | + |
| 349 | + result = await hass.config_entries.flow.async_configure( |
| 350 | + result["flow_id"], {CONF_USERNAME: "pyload", CONF_PASSWORD: "pyload"} |
| 351 | + ) |
| 352 | + |
| 353 | + assert result["type"] is FlowResultType.CREATE_ENTRY |
| 354 | + assert result["title"] == "p539df76c_pyload-ng" |
| 355 | + assert result["data"] == {**ADDON_DISCOVERY_INFO, CONF_VERIFY_SSL: False} |
| 356 | + assert len(mock_setup_entry.mock_calls) == 1 |
| 357 | + |
| 358 | + |
| 359 | +@pytest.mark.usefixtures("mock_pyloadapi") |
| 360 | +async def test_hassio_discovery_already_configured( |
| 361 | + hass: HomeAssistant, |
| 362 | +) -> None: |
| 363 | + """Test we abort discovery flow if already configured.""" |
| 364 | + |
| 365 | + MockConfigEntry( |
| 366 | + domain=DOMAIN, |
| 367 | + data={ |
| 368 | + CONF_URL: "http://539df76c-pyload-ng:8000/", |
| 369 | + CONF_USERNAME: "pyload", |
| 370 | + CONF_PASSWORD: "pyload", |
| 371 | + }, |
| 372 | + ).add_to_hass(hass) |
| 373 | + |
| 374 | + result = await hass.config_entries.flow.async_init( |
| 375 | + DOMAIN, |
| 376 | + data=ADDON_SERVICE_INFO, |
| 377 | + context={"source": SOURCE_HASSIO}, |
| 378 | + ) |
| 379 | + |
| 380 | + assert result["type"] is FlowResultType.ABORT |
| 381 | + assert result["reason"] == "already_configured" |
| 382 | + |
| 383 | + |
| 384 | +@pytest.mark.usefixtures("mock_pyloadapi") |
| 385 | +async def test_hassio_discovery_data_update( |
| 386 | + hass: HomeAssistant, |
| 387 | +) -> None: |
| 388 | + """Test we abort discovery flow if already configured and we update entry from discovery data.""" |
| 389 | + |
| 390 | + entry = MockConfigEntry( |
| 391 | + domain=DOMAIN, |
| 392 | + data={ |
| 393 | + CONF_URL: "http://localhost:8000/", |
| 394 | + CONF_USERNAME: "pyload", |
| 395 | + CONF_PASSWORD: "pyload", |
| 396 | + }, |
| 397 | + unique_id="1234", |
| 398 | + ) |
| 399 | + |
| 400 | + entry.add_to_hass(hass) |
| 401 | + |
| 402 | + result = await hass.config_entries.flow.async_init( |
| 403 | + DOMAIN, |
| 404 | + data=ADDON_SERVICE_INFO, |
| 405 | + context={"source": SOURCE_HASSIO}, |
| 406 | + ) |
| 407 | + |
| 408 | + assert result["type"] is FlowResultType.ABORT |
| 409 | + assert result["reason"] == "already_configured" |
| 410 | + |
| 411 | + assert entry.data[CONF_URL] == "http://539df76c-pyload-ng:8000/" |
| 412 | + |
| 413 | + |
| 414 | +@pytest.mark.usefixtures("mock_pyloadapi") |
| 415 | +async def test_hassio_discovery_ignored( |
| 416 | + hass: HomeAssistant, |
| 417 | +) -> None: |
| 418 | + """Test we abort discovery flow if discovery was ignored.""" |
| 419 | + |
| 420 | + MockConfigEntry( |
| 421 | + domain=DOMAIN, |
| 422 | + source=SOURCE_IGNORE, |
| 423 | + data={}, |
| 424 | + unique_id="1234", |
| 425 | + ).add_to_hass(hass) |
| 426 | + |
| 427 | + result = await hass.config_entries.flow.async_init( |
| 428 | + DOMAIN, |
| 429 | + data=ADDON_SERVICE_INFO, |
| 430 | + context={"source": SOURCE_HASSIO}, |
| 431 | + ) |
| 432 | + |
| 433 | + assert result["type"] is FlowResultType.ABORT |
| 434 | + assert result["reason"] == "already_configured" |
0 commit comments