Skip to content

Commit 71eefdc

Browse files
johanzanderclaude
andauthored
Migrate async_migrate_entry test calls to async_setup in growatt_server (home-assistant#172587)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f5819d4 commit 71eefdc

1 file changed

Lines changed: 30 additions & 41 deletions

File tree

tests/components/growatt_server/test_init.py

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import requests
1010
from syrupy.assertion import SnapshotAssertion
1111

12-
from homeassistant.components.growatt_server import async_migrate_entry
1312
from homeassistant.components.growatt_server.const import (
1413
AUTH_API_TOKEN,
1514
AUTH_PASSWORD,
@@ -332,6 +331,7 @@ async def test_classic_api_setup(
332331
),
333332
],
334333
)
334+
@pytest.mark.usefixtures("mock_growatt_v1_api", "mock_growatt_classic_api")
335335
async def test_migrate_config_without_auth_type(
336336
hass: HomeAssistant,
337337
config_data: dict[str, str],
@@ -353,11 +353,8 @@ async def test_migrate_config_without_auth_type(
353353
)
354354

355355
mock_config_entry.add_to_hass(hass)
356-
357-
# Execute migration
358-
# pylint: disable-next=home-assistant-tests-direct-async-migrate-entry
359-
migration_result = await async_migrate_entry(hass, mock_config_entry)
360-
assert migration_result is True
356+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
357+
await hass.async_block_till_done()
361358

362359
# Verify version was updated to 1.1
363360
assert mock_config_entry.version == 1
@@ -366,6 +363,9 @@ async def test_migrate_config_without_auth_type(
366363
# Verify auth_type field was added during migration
367364
assert mock_config_entry.data[CONF_AUTH_TYPE] == expected_auth_type
368365

366+
# Verify setup completed successfully after migration
367+
assert mock_config_entry.state is ConfigEntryState.LOADED
368+
369369

370370
async def test_migrate_legacy_config_no_auth_fields(
371371
hass: HomeAssistant,
@@ -385,17 +385,13 @@ async def test_migrate_legacy_config_no_auth_fields(
385385
)
386386

387387
mock_config_entry.add_to_hass(hass)
388+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
389+
await hass.async_block_till_done()
388390

389-
# Migration should succeed (only updates version)
390-
# pylint: disable-next=home-assistant-tests-direct-async-migrate-entry
391-
migration_result = await async_migrate_entry(hass, mock_config_entry)
392-
assert migration_result is True
393-
394-
# Verify version was updated
391+
# Migration succeeds (version bumped) but setup fails due to missing auth fields
395392
assert mock_config_entry.version == 1
396393
assert mock_config_entry.minor_version == 1
397-
398-
# Note: Setup will fail later due to missing auth fields in async_setup_entry
394+
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
399395

400396

401397
@pytest.mark.parametrize(
@@ -613,7 +609,6 @@ async def test_migrate_version_bump(
613609
This test verifies that:
614610
- Migration successfully resolves DEFAULT_PLANT_ID ("0") to actual plant_id
615611
- Config entry version is bumped from 1.0 to 1.1
616-
- API instance is cached for setup to reuse (rate limit optimization)
617612
"""
618613
# Create a version 1.0 config entry with DEFAULT_PLANT_ID
619614
mock_config_entry = MockConfigEntry(
@@ -631,21 +626,21 @@ async def test_migrate_version_bump(
631626
minor_version=0,
632627
)
633628

634-
# Mock successful API responses for migration
629+
# Mock successful API responses for migration and setup
635630
mock_growatt_classic_api.login.return_value = {
636631
"success": True,
637632
"user": {"id": 123456},
638633
}
639634
mock_growatt_classic_api.plant_list.return_value = {
640635
"data": [{"plantId": "RESOLVED_PLANT_789", "plantName": "My Plant"}]
641636
}
637+
mock_growatt_classic_api.device_list.return_value = [
638+
{"deviceSn": "TLX123456", "deviceType": "tlx"}
639+
]
642640

643641
mock_config_entry.add_to_hass(hass)
644-
645-
# Execute migration
646-
# pylint: disable-next=home-assistant-tests-direct-async-migrate-entry
647-
migration_result = await async_migrate_entry(hass, mock_config_entry)
648-
assert migration_result is True
642+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
643+
await hass.async_block_till_done()
649644

650645
# Verify version was updated to 1.1
651646
assert mock_config_entry.version == 1
@@ -654,8 +649,8 @@ async def test_migrate_version_bump(
654649
# Verify plant_id was resolved to actual plant_id (not DEFAULT_PLANT_ID)
655650
assert mock_config_entry.data[CONF_PLANT_ID] == "RESOLVED_PLANT_789"
656651

657-
# Verify API instance was cached for setup to reuse
658-
assert f"{CACHED_API_KEY}{mock_config_entry.entry_id}" in hass.data[DOMAIN]
652+
# Verify setup completed successfully after migration
653+
assert mock_config_entry.state is ConfigEntryState.LOADED
659654

660655

661656
async def test_setup_reuses_cached_api_from_migration(
@@ -718,17 +713,12 @@ async def test_setup_reuses_cached_api_from_migration(
718713
}
719714

720715
mock_config_entry.add_to_hass(hass)
721-
722-
# Run migration first (resolves plant_id and caches authenticated API)
723-
# pylint: disable-next=home-assistant-tests-direct-async-migrate-entry
724-
await async_migrate_entry(hass, mock_config_entry)
716+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
717+
await hass.async_block_till_done()
725718

726719
# Verify migration successfully resolved plant_id
727720
assert mock_config_entry.data[CONF_PLANT_ID] == "RESOLVED_PLANT_789"
728721

729-
# Now setup the integration (should reuse cached API from migration)
730-
await setup_integration(hass, mock_config_entry)
731-
732722
# Verify integration loaded successfully
733723
assert mock_config_entry.state is ConfigEntryState.LOADED
734724

@@ -783,13 +773,11 @@ async def test_migrate_failure_returns_false(
783773
)
784774

785775
mock_config_entry.add_to_hass(hass)
776+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
777+
await hass.async_block_till_done()
786778

787-
# Execute migration (should fail gracefully)
788-
# pylint: disable-next=home-assistant-tests-direct-async-migrate-entry
789-
migration_result = await async_migrate_entry(hass, mock_config_entry)
790-
791-
# Verify migration returned False (will retry on next restart)
792-
assert migration_result is False
779+
# Verify migration failed (entry is in migration error state)
780+
assert mock_config_entry.state is ConfigEntryState.MIGRATION_ERROR
793781

794782
# Verify version was NOT bumped (remains 1.0)
795783
assert mock_config_entry.version == 1
@@ -803,6 +791,7 @@ async def test_migrate_failure_returns_false(
803791
assert "Migration will retry on next restart" in caplog.text
804792

805793

794+
@pytest.mark.usefixtures("mock_growatt_classic_api")
806795
async def test_migrate_already_migrated(
807796
hass: HomeAssistant,
808797
) -> None:
@@ -823,11 +812,8 @@ async def test_migrate_already_migrated(
823812
)
824813

825814
mock_config_entry.add_to_hass(hass)
826-
827-
# Call migration function
828-
# pylint: disable-next=home-assistant-tests-direct-async-migrate-entry
829-
migration_result = await async_migrate_entry(hass, mock_config_entry)
830-
assert migration_result is True
815+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
816+
await hass.async_block_till_done()
831817

832818
# Verify version remains 1.1 (no change)
833819
assert mock_config_entry.version == 1
@@ -836,6 +822,9 @@ async def test_migrate_already_migrated(
836822
# Plant ID should remain unchanged
837823
assert mock_config_entry.data[CONF_PLANT_ID] == "specific_plant_123"
838824

825+
# Verify setup completed successfully
826+
assert mock_config_entry.state is ConfigEntryState.LOADED
827+
839828

840829
@pytest.mark.usefixtures("init_integration")
841830
async def test_dynamic_device_added(

0 commit comments

Comments
 (0)