@@ -363,20 +363,24 @@ async def test_component_failing_setup(hass: HomeAssistant) -> None:
363
363
364
364
async def test_component_exception_setup (hass : HomeAssistant ) -> None :
365
365
"""Test component that raises exception during setup."""
366
- setup .async_set_domains_to_be_loaded (hass , {"comp" })
366
+ domain = "comp"
367
+ setup .async_set_domains_to_be_loaded (hass , {domain })
367
368
368
369
def exception_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
369
370
"""Raise exception."""
370
371
raise Exception ("fail!" ) # noqa: TRY002
371
372
372
- mock_integration (hass , MockModule ("comp" , setup = exception_setup ))
373
+ mock_integration (hass , MockModule (domain , setup = exception_setup ))
373
374
374
- assert not await setup .async_setup_component (hass , "comp" , {})
375
- assert "comp" not in hass .config .components
375
+ assert not await setup .async_setup_component (hass , domain , {})
376
+ assert domain in hass .data [setup .DATA_SETUP ]
377
+ assert domain not in hass .data [setup .DATA_SETUP_DONE ]
378
+ assert domain not in hass .config .components
376
379
377
380
378
381
async def test_component_base_exception_setup (hass : HomeAssistant ) -> None :
379
382
"""Test component that raises exception during setup."""
383
+ domain = "comp"
380
384
setup .async_set_domains_to_be_loaded (hass , {"comp" })
381
385
382
386
def exception_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
@@ -389,7 +393,69 @@ def exception_setup(hass: HomeAssistant, config: ConfigType) -> bool:
389
393
await setup .async_setup_component (hass , "comp" , {})
390
394
assert str (exc_info .value ) == "fail!"
391
395
392
- assert "comp" not in hass .config .components
396
+ assert domain in hass .data [setup .DATA_SETUP ]
397
+ assert domain not in hass .data [setup .DATA_SETUP_DONE ]
398
+ assert domain not in hass .config .components
399
+
400
+
401
+ async def test_set_domains_to_be_loaded (hass : HomeAssistant ) -> None :
402
+ """Test async_set_domains_to_be_loaded."""
403
+ domain_good = "comp_good"
404
+ domain_bad = "comp_bad"
405
+ domain_base_exception = "comp_base_exception"
406
+ domain_exception = "comp_exception"
407
+ domains = {domain_good , domain_bad , domain_exception , domain_base_exception }
408
+ setup .async_set_domains_to_be_loaded (hass , domains )
409
+
410
+ assert set (hass .data [setup .DATA_SETUP_DONE ]) == domains
411
+ setup_done = dict (hass .data [setup .DATA_SETUP_DONE ])
412
+
413
+ # Calling async_set_domains_to_be_loaded again should not create new futures
414
+ setup .async_set_domains_to_be_loaded (hass , domains )
415
+ assert setup_done == hass .data [setup .DATA_SETUP_DONE ]
416
+
417
+ def good_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
418
+ """Success."""
419
+ return True
420
+
421
+ def bad_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
422
+ """Fail."""
423
+ return False
424
+
425
+ def base_exception_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
426
+ """Raise exception."""
427
+ raise BaseException ("fail!" ) # noqa: TRY002
428
+
429
+ def exception_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
430
+ """Raise exception."""
431
+ raise Exception ("fail!" ) # noqa: TRY002
432
+
433
+ mock_integration (hass , MockModule (domain_good , setup = good_setup ))
434
+ mock_integration (hass , MockModule (domain_bad , setup = bad_setup ))
435
+ mock_integration (
436
+ hass , MockModule (domain_base_exception , setup = base_exception_setup )
437
+ )
438
+ mock_integration (hass , MockModule (domain_exception , setup = exception_setup ))
439
+
440
+ # Set up the four components
441
+ assert await setup .async_setup_component (hass , domain_good , {})
442
+ assert not await setup .async_setup_component (hass , domain_bad , {})
443
+ assert not await setup .async_setup_component (hass , domain_exception , {})
444
+ with pytest .raises (BaseException , match = "fail!" ):
445
+ await setup .async_setup_component (hass , domain_base_exception , {})
446
+
447
+ # Check the result of the setup
448
+ assert not hass .data [setup .DATA_SETUP_DONE ]
449
+ assert set (hass .data [setup .DATA_SETUP ]) == {
450
+ domain_bad ,
451
+ domain_exception ,
452
+ domain_base_exception ,
453
+ }
454
+ assert set (hass .config .components ) == {domain_good }
455
+
456
+ # Calling async_set_domains_to_be_loaded again should not create any new futures
457
+ setup .async_set_domains_to_be_loaded (hass , domains )
458
+ assert not hass .data [setup .DATA_SETUP_DONE ]
393
459
394
460
395
461
async def test_component_setup_with_validation_and_dependency (
0 commit comments