diff --git a/homeassistant/components/tplink/__init__.py b/homeassistant/components/tplink/__init__.py index 9f3658cf2cd..ff0526490f5 100644 --- a/homeassistant/components/tplink/__init__.py +++ b/homeassistant/components/tplink/__init__.py @@ -120,6 +120,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async_migrate_legacy_entries( hass, hosts_by_mac, config_entries_by_mac, legacy_entry ) + # Migrate the yaml entry that was previously imported + async_migrate_yaml_entries(hass, legacy_entry.data) if conf is not None: async_migrate_yaml_entries(hass, conf) diff --git a/homeassistant/components/tplink/migration.py b/homeassistant/components/tplink/migration.py index af81323d39f..9344dd1532f 100644 --- a/homeassistant/components/tplink/migration.py +++ b/homeassistant/components/tplink/migration.py @@ -2,6 +2,8 @@ from __future__ import annotations from datetime import datetime +from types import MappingProxyType +from typing import Any from homeassistant import config_entries from homeassistant.config_entries import ConfigEntry @@ -65,7 +67,9 @@ def async_migrate_legacy_entries( @callback -def async_migrate_yaml_entries(hass: HomeAssistant, conf: ConfigType) -> None: +def async_migrate_yaml_entries( + hass: HomeAssistant, conf: ConfigType | MappingProxyType[str, Any] +) -> None: """Migrate yaml to config entries.""" for device_type in (CONF_LIGHT, CONF_SWITCH, CONF_STRIP, CONF_DIMMER): for device in conf.get(device_type, []): diff --git a/tests/components/tplink/test_migration.py b/tests/components/tplink/test_migration.py index 6cd82448ca2..a1cd581e211 100644 --- a/tests/components/tplink/test_migration.py +++ b/tests/components/tplink/test_migration.py @@ -239,3 +239,25 @@ async def test_migrate_from_yaml(hass: HomeAssistant): assert migrated_entry is not None assert migrated_entry.data[CONF_HOST] == IP_ADDRESS + + +async def test_migrate_from_legacy_entry(hass: HomeAssistant): + """Test migrate from legacy entry that was already imported from yaml.""" + data = { + CONF_DISCOVERY: False, + CONF_SWITCH: [{CONF_HOST: IP_ADDRESS}], + } + config_entry = MockConfigEntry(domain=DOMAIN, data=data, unique_id=DOMAIN) + config_entry.add_to_hass(hass) + with _patch_discovery(), _patch_single_discovery(): + await setup.async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() + + migrated_entry = None + for entry in hass.config_entries.async_entries(DOMAIN): + if entry.unique_id == MAC_ADDRESS: + migrated_entry = entry + break + + assert migrated_entry is not None + assert migrated_entry.data[CONF_HOST] == IP_ADDRESS