@@ -551,6 +551,9 @@ async def test_async_config_entry_first_refresh_failure(
551551 a decreasing level of logging once the first message is logged.
552552 """
553553 entry = MockConfigEntry ()
554+ entry ._async_set_state (
555+ hass , config_entries .ConfigEntryState .SETUP_IN_PROGRESS , None
556+ )
554557 crd = get_crd (hass , DEFAULT_UPDATE_INTERVAL , entry )
555558 setattr (crd , method , AsyncMock (side_effect = err_msg [0 ]))
556559
@@ -586,6 +589,9 @@ async def test_async_config_entry_first_refresh_failure_passed_through(
586589 a decreasing level of logging once the first message is logged.
587590 """
588591 entry = MockConfigEntry ()
592+ entry ._async_set_state (
593+ hass , config_entries .ConfigEntryState .SETUP_IN_PROGRESS , None
594+ )
589595 crd = get_crd (hass , DEFAULT_UPDATE_INTERVAL , entry )
590596 setattr (crd , method , AsyncMock (side_effect = err_msg [0 ]))
591597
@@ -600,6 +606,9 @@ async def test_async_config_entry_first_refresh_failure_passed_through(
600606async def test_async_config_entry_first_refresh_success (hass : HomeAssistant ) -> None :
601607 """Test first refresh successfully."""
602608 entry = MockConfigEntry ()
609+ entry ._async_set_state (
610+ hass , config_entries .ConfigEntryState .SETUP_IN_PROGRESS , None
611+ )
603612 crd = get_crd (hass , DEFAULT_UPDATE_INTERVAL , entry )
604613 crd .setup_method = AsyncMock ()
605614 await crd .async_config_entry_first_refresh ()
@@ -608,6 +617,46 @@ async def test_async_config_entry_first_refresh_success(hass: HomeAssistant) ->
608617 crd .setup_method .assert_called_once ()
609618
610619
620+ async def test_async_config_entry_first_refresh_invalid_state (
621+ hass : HomeAssistant ,
622+ ) -> None :
623+ """Test first refresh fails due to invalid state."""
624+ entry = MockConfigEntry ()
625+ crd = get_crd (hass , DEFAULT_UPDATE_INTERVAL , entry )
626+ crd .setup_method = AsyncMock ()
627+ with pytest .raises (
628+ RuntimeError ,
629+ match = "Detected code that uses `async_config_entry_first_refresh`, which "
630+ "is only supported when entry state is ConfigEntryState.SETUP_IN_PROGRESS, "
631+ "but it is in state ConfigEntryState.NOT_LOADED. This will stop working "
632+ "in Home Assistant 2025.11. Please report this issue." ,
633+ ):
634+ await crd .async_config_entry_first_refresh ()
635+
636+ assert crd .last_update_success is True
637+ crd .setup_method .assert_not_called ()
638+
639+
640+ @pytest .mark .usefixtures ("mock_integration_frame" )
641+ async def test_async_config_entry_first_refresh_invalid_state_in_integration (
642+ hass : HomeAssistant , caplog : pytest .LogCaptureFixture
643+ ) -> None :
644+ """Test first refresh successfully, despite wrong state."""
645+ entry = MockConfigEntry ()
646+ crd = get_crd (hass , DEFAULT_UPDATE_INTERVAL , entry )
647+ crd .setup_method = AsyncMock ()
648+
649+ await crd .async_config_entry_first_refresh ()
650+ assert crd .last_update_success is True
651+ crd .setup_method .assert_called ()
652+ assert (
653+ "Detected that integration 'hue' uses `async_config_entry_first_refresh`, which "
654+ "is only supported when entry state is ConfigEntryState.SETUP_IN_PROGRESS, "
655+ "but it is in state ConfigEntryState.NOT_LOADED, This will stop working "
656+ "in Home Assistant 2025.11"
657+ ) in caplog .text
658+
659+
611660async def test_async_config_entry_first_refresh_no_entry (hass : HomeAssistant ) -> None :
612661 """Test first refresh successfully."""
613662 crd = get_crd (hass , DEFAULT_UPDATE_INTERVAL , None )
0 commit comments