async def test_migration_ti_cc_to_znp(old_type, new_type, hass, config_entry): """Test zigpy-cc to zigpy-znp config migration.""" config_entry = MockConfigEntry( domain=DOMAIN, unique_id=old_type + new_type, data={ CONF_RADIO_TYPE: old_type, CONF_DEVICE: { CONF_DEVICE_PATH: "/dev/ttyUSB1", CONF_BAUDRATE: 115200, CONF_FLOWCONTROL: None, }, }, ) config_entry.version = 2 config_entry.add_to_hass(hass) with patch("homeassistant.components.zha.async_setup_entry", return_value=True): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.version > 2 assert config_entry.data[CONF_RADIO_TYPE] == new_type
async def test_call_async_migrate_entry_failure_not_supported(hass): """Test migration fails if async_migrate_entry not implemented.""" entry = MockConfigEntry(domain='comp') entry.version = 2 entry.add_to_hass(hass) mock_setup_entry = MagicMock(return_value=mock_coro(True)) loader.set_component( hass, 'comp', MockModule('comp', async_setup_entry=mock_setup_entry)) result = await async_setup_component(hass, 'comp', {}) assert result assert len(mock_setup_entry.mock_calls) == 0 assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
async def test_call_async_migrate_entry_failure_not_supported(hass): """Test migration fails if async_migrate_entry not implemented.""" entry = MockConfigEntry(domain='comp') entry.version = 2 entry.add_to_hass(hass) mock_setup_entry = MagicMock(return_value=mock_coro(True)) loader.set_component( hass, 'comp', MockModule('comp', async_setup_entry=mock_setup_entry)) result = await async_setup_component(hass, 'comp', {}) assert result assert len(mock_setup_entry.mock_calls) == 0 assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
async def test_call_async_migrate_entry_failure_not_supported(hass): """Test migration fails if async_migrate_entry not implemented.""" entry = MockConfigEntry(domain="comp") entry.version = 2 entry.add_to_hass(hass) mock_setup_entry = MagicMock(return_value=mock_coro(True)) mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry)) mock_entity_platform(hass, "config_flow.comp", None) result = await async_setup_component(hass, "comp", {}) assert result assert len(mock_setup_entry.mock_calls) == 0 assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
async def test_call_async_migrate_entry(hass): """Test we call <component>.async_migrate_entry when version mismatch.""" entry = MockConfigEntry(domain='comp') entry.version = 2 entry.add_to_hass(hass) mock_migrate_entry = MagicMock(return_value=mock_coro(True)) mock_setup_entry = MagicMock(return_value=mock_coro(True)) loader.set_component( hass, 'comp', MockModule('comp', async_setup_entry=mock_setup_entry, async_migrate_entry=mock_migrate_entry)) result = await async_setup_component(hass, 'comp', {}) assert result assert len(mock_migrate_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 assert entry.state == config_entries.ENTRY_STATE_LOADED
async def test_call_async_migrate_entry_failure_false(hass): """Test migration fails if returns false.""" entry = MockConfigEntry(domain='comp') entry.version = 2 entry.add_to_hass(hass) mock_migrate_entry = MagicMock(return_value=mock_coro(False)) mock_setup_entry = MagicMock(return_value=mock_coro(True)) mock_integration( hass, MockModule('comp', async_setup_entry=mock_setup_entry, async_migrate_entry=mock_migrate_entry)) result = await async_setup_component(hass, 'comp', {}) assert result assert len(mock_migrate_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 0 assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
async def test_call_async_migrate_entry(hass): """Test we call <component>.async_migrate_entry when version mismatch.""" entry = MockConfigEntry(domain='comp') entry.version = 2 entry.add_to_hass(hass) mock_migrate_entry = MagicMock(return_value=mock_coro(True)) mock_setup_entry = MagicMock(return_value=mock_coro(True)) mock_integration( hass, MockModule('comp', async_setup_entry=mock_setup_entry, async_migrate_entry=mock_migrate_entry)) result = await async_setup_component(hass, 'comp', {}) assert result assert len(mock_migrate_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 assert entry.state == config_entries.ENTRY_STATE_LOADED