From fff254e0dc79883e9f56446534f913948e399b1d Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 20 Jul 2023 14:45:07 +0200 Subject: [PATCH] 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 * Update tests/components/subaru/test_sensor.py Co-authored-by: G-Two <7310260+G-Two@users.noreply.github.com> --------- Co-authored-by: G Johansson Co-authored-by: G-Two <7310260+G-Two@users.noreply.github.com> --- homeassistant/components/subaru/sensor.py | 21 +++++++++++++-------- tests/components/subaru/test_sensor.py | 21 +++++++++++---------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/subaru/sensor.py b/homeassistant/components/subaru/sensor.py index 6c8e8fc100b..50e8f89716b 100644 --- a/homeassistant/components/subaru/sensor.py +++ b/homeassistant/components/subaru/sensor.py @@ -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: diff --git a/tests/components/subaru/test_sensor.py b/tests/components/subaru/test_sensor.py index aa351f7ccbd..fd03ed3044b 100644 --- a/tests/components/subaru/test_sensor.py +++ b/tests/components/subaru/test_sensor.py @@ -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)