Avoid using name in Subaru migrations (#96221)

* Avoid using name in Subaru migrations

* Add feedback

* Update tests/components/subaru/test_sensor.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update tests/components/subaru/test_sensor.py

Co-authored-by: G-Two <7310260+G-Two@users.noreply.github.com>

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
Co-authored-by: G-Two <7310260+G-Two@users.noreply.github.com>
This commit is contained in:
Joost Lekkerkerker 2023-07-20 14:45:07 +02:00 committed by GitHub
parent a381ceed86
commit fff254e0dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 18 deletions

View file

@ -276,14 +276,19 @@ async def _async_migrate_entries(
"""Migrate sensor entries from HA<=2022.10 to use preferred unique_id."""
entity_registry = er.async_get(hass)
all_sensors = []
all_sensors.extend(EV_SENSORS)
all_sensors.extend(API_GEN_2_SENSORS)
all_sensors.extend(SAFETY_SENSORS)
# Old unique_id is (previously title-cased) sensor name
# (e.g. "VIN_Avg Fuel Consumption")
replacements = {str(s.name).upper(): s.key for s in all_sensors}
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:

View file

@ -1,4 +1,5 @@
"""Test Subaru sensors."""
from typing import Any
from unittest.mock import patch
import pytest
@ -12,7 +13,6 @@ from homeassistant.components.subaru.sensor import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util import slugify
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from .api_responses import (
@ -25,7 +25,6 @@ from .api_responses import (
from .conftest import (
MOCK_API_FETCH,
MOCK_API_GET_DATA,
TEST_DEVICE_NAME,
advance_time_to_next_fetch,
setup_subaru_config_entry,
)
@ -65,9 +64,9 @@ async def test_sensors_missing_vin_data(hass: HomeAssistant, ev_entry) -> None:
{
"domain": SENSOR_DOMAIN,
"platform": SUBARU_DOMAIN,
"unique_id": f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].name}",
"unique_id": f"{TEST_VIN_2_EV}_Avg fuel consumption",
},
f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].name}",
f"{TEST_VIN_2_EV}_Avg fuel consumption",
f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].key}",
),
],
@ -97,9 +96,9 @@ async def test_sensor_migrate_unique_ids(
{
"domain": SENSOR_DOMAIN,
"platform": SUBARU_DOMAIN,
"unique_id": f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].name}",
"unique_id": f"{TEST_VIN_2_EV}_Avg fuel consumption",
},
f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].name}",
f"{TEST_VIN_2_EV}_Avg fuel consumption",
f"{TEST_VIN_2_EV}_{API_GEN_2_SENSORS[0].key}",
)
],
@ -136,15 +135,17 @@ async def test_sensor_migrate_unique_ids_duplicate(
assert entity_migrated != entity_not_changed
def _assert_data(hass, expected_state):
def _assert_data(hass: HomeAssistant, expected_state: dict[str, Any]) -> None:
sensor_list = EV_SENSORS
sensor_list.extend(API_GEN_2_SENSORS)
sensor_list.extend(SAFETY_SENSORS)
expected_states = {}
entity_registry = er.async_get(hass)
for item in sensor_list:
expected_states[
f"sensor.{slugify(f'{TEST_DEVICE_NAME} {item.name}')}"
] = expected_state[item.key]
entity = entity_registry.async_get_entity_id(
SENSOR_DOMAIN, SUBARU_DOMAIN, f"{TEST_VIN_2_EV}_{item.key}"
)
expected_states[entity] = expected_state[item.key]
for sensor, value in expected_states.items():
actual = hass.states.get(sensor)