mirror of
https://github.com/home-assistant/core.git
synced 2026-09-06 13:32:08 +01:00
Remove Subaru unique_id migration for HA<=2022.10 (#180944)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
ffbdca81ec
commit
6cd5e22533
@@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
EntityCategory,
|
||||
@@ -23,8 +22,7 @@ from homeassistant.const import (
|
||||
UnitOfPressure,
|
||||
UnitOfVolume,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util.unit_conversion import DistanceConverter, VolumeConverter
|
||||
@@ -226,7 +224,6 @@ async def async_setup_entry(
|
||||
coordinator = config_entry.runtime_data.coordinator
|
||||
vehicle_info = config_entry.runtime_data.vehicles
|
||||
entities = []
|
||||
await _async_migrate_entries(hass, config_entry)
|
||||
for info in vehicle_info.values():
|
||||
entities.extend(create_vehicle_sensors(info, coordinator))
|
||||
async_add_entities(entities)
|
||||
@@ -305,54 +302,3 @@ class SubaruSensor(SubaruCoordinatorEntity, SensorEntity):
|
||||
):
|
||||
return FUEL_CONSUMPTION_LITERS_PER_HUNDRED_KILOMETERS
|
||||
return self.entity_description.native_unit_of_measurement
|
||||
|
||||
|
||||
async def _async_migrate_entries(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
) -> None:
|
||||
"""Migrate sensor entries from HA<=2022.10 to use preferred unique_id."""
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
replacements = {
|
||||
"ODOMETER": sc.ODOMETER,
|
||||
"AVG FUEL CONSUMPTION": sc.AVG_FUEL_CONSUMPTION,
|
||||
"RANGE": sc.DIST_TO_EMPTY,
|
||||
"TIRE PRESSURE FL": sc.TIRE_PRESSURE_FL,
|
||||
"TIRE PRESSURE FR": sc.TIRE_PRESSURE_FR,
|
||||
"TIRE PRESSURE RL": sc.TIRE_PRESSURE_RL,
|
||||
"TIRE PRESSURE RR": sc.TIRE_PRESSURE_RR,
|
||||
"FUEL LEVEL": sc.REMAINING_FUEL_PERCENT,
|
||||
"EV RANGE": sc.EV_DISTANCE_TO_EMPTY,
|
||||
"EV BATTERY LEVEL": sc.EV_STATE_OF_CHARGE_PERCENT,
|
||||
"EV TIME TO FULL CHARGE": sc.EV_TIME_TO_FULLY_CHARGED_UTC,
|
||||
}
|
||||
|
||||
@callback
|
||||
def update_unique_id(entry: er.RegistryEntry) -> dict[str, Any] | None:
|
||||
id_split = entry.unique_id.split("_")
|
||||
key = id_split[1].upper() if len(id_split) == 2 else None
|
||||
|
||||
if key not in replacements or id_split[1] == replacements[key]:
|
||||
return None
|
||||
|
||||
new_unique_id = entry.unique_id.replace(id_split[1], replacements[key])
|
||||
_LOGGER.debug(
|
||||
"Migrating entity '%s' unique_id from '%s' to '%s'",
|
||||
entry.entity_id,
|
||||
entry.unique_id,
|
||||
new_unique_id,
|
||||
)
|
||||
if existing_entity_id := entity_registry.async_get_entity_id(
|
||||
entry.domain, entry.platform, new_unique_id
|
||||
):
|
||||
_LOGGER.debug(
|
||||
"Cannot migrate to unique_id '%s', already exists for '%s'",
|
||||
new_unique_id,
|
||||
existing_entity_id,
|
||||
)
|
||||
return None
|
||||
return {
|
||||
"new_unique_id": new_unique_id,
|
||||
}
|
||||
|
||||
await er.async_migrate_entries(hass, config_entry.entry_id, update_unique_id)
|
||||
|
||||
@@ -47,92 +47,6 @@ async def test_sensors_missing_vin_data(hass: HomeAssistant, ev_entry) -> None:
|
||||
_assert_data(hass, EXPECTED_STATE_EV_UNAVAILABLE)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entitydata", "old_unique_id", "new_unique_id"),
|
||||
[
|
||||
(
|
||||
{
|
||||
"domain": SENSOR_DOMAIN,
|
||||
"platform": DOMAIN,
|
||||
"unique_id": f"{TEST_VIN_2_EV}_Avg fuel consumption",
|
||||
},
|
||||
f"{TEST_VIN_2_EV}_Avg fuel consumption",
|
||||
f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].key}",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_sensor_migrate_unique_ids(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
entitydata,
|
||||
old_unique_id,
|
||||
new_unique_id,
|
||||
subaru_config_entry,
|
||||
) -> None:
|
||||
"""Test successful migration of entity unique_ids."""
|
||||
entity: er.RegistryEntry = entity_registry.async_get_or_create(
|
||||
**entitydata,
|
||||
config_entry=subaru_config_entry,
|
||||
)
|
||||
assert entity.unique_id == old_unique_id
|
||||
|
||||
await setup_subaru_config_entry(hass, subaru_config_entry)
|
||||
|
||||
entity_migrated = entity_registry.async_get(entity.entity_id)
|
||||
assert entity_migrated
|
||||
assert entity_migrated.unique_id == new_unique_id
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entitydata", "old_unique_id", "new_unique_id"),
|
||||
[
|
||||
(
|
||||
{
|
||||
"domain": SENSOR_DOMAIN,
|
||||
"platform": DOMAIN,
|
||||
"unique_id": f"{TEST_VIN_2_EV}_Avg fuel consumption",
|
||||
},
|
||||
f"{TEST_VIN_2_EV}_Avg fuel consumption",
|
||||
f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].key}",
|
||||
)
|
||||
],
|
||||
)
|
||||
async def test_sensor_migrate_unique_ids_duplicate(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
entitydata,
|
||||
old_unique_id,
|
||||
new_unique_id,
|
||||
subaru_config_entry,
|
||||
) -> None:
|
||||
"""Test unsuccessful migration of entity unique_ids due to duplicate."""
|
||||
entity: er.RegistryEntry = entity_registry.async_get_or_create(
|
||||
**entitydata,
|
||||
config_entry=subaru_config_entry,
|
||||
)
|
||||
assert entity.unique_id == old_unique_id
|
||||
|
||||
# create existing entry with new_unique_id that conflicts with migrate
|
||||
existing_entity = entity_registry.async_get_or_create(
|
||||
SENSOR_DOMAIN,
|
||||
DOMAIN,
|
||||
unique_id=new_unique_id,
|
||||
config_entry=subaru_config_entry,
|
||||
)
|
||||
|
||||
await setup_subaru_config_entry(hass, subaru_config_entry)
|
||||
|
||||
entity_migrated = entity_registry.async_get(entity.entity_id)
|
||||
assert entity_migrated
|
||||
assert entity_migrated.unique_id == old_unique_id
|
||||
|
||||
entity_not_changed = entity_registry.async_get(existing_entity.entity_id)
|
||||
assert entity_not_changed
|
||||
assert entity_not_changed.unique_id == new_unique_id
|
||||
|
||||
assert entity_migrated != entity_not_changed
|
||||
|
||||
|
||||
def _assert_data(hass: HomeAssistant, expected_state: dict[str, Any]) -> None:
|
||||
sensor_list = EV_SENSORS
|
||||
sensor_list.extend(API_GEN_2_SENSORS)
|
||||
|
||||
Reference in New Issue
Block a user