mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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:
parent
a381ceed86
commit
fff254e0dc
@ -276,14 +276,19 @@ async def _async_migrate_entries(
|
|||||||
"""Migrate sensor entries from HA<=2022.10 to use preferred unique_id."""
|
"""Migrate sensor entries from HA<=2022.10 to use preferred unique_id."""
|
||||||
entity_registry = er.async_get(hass)
|
entity_registry = er.async_get(hass)
|
||||||
|
|
||||||
all_sensors = []
|
replacements = {
|
||||||
all_sensors.extend(EV_SENSORS)
|
"ODOMETER": sc.ODOMETER,
|
||||||
all_sensors.extend(API_GEN_2_SENSORS)
|
"AVG FUEL CONSUMPTION": sc.AVG_FUEL_CONSUMPTION,
|
||||||
all_sensors.extend(SAFETY_SENSORS)
|
"RANGE": sc.DIST_TO_EMPTY,
|
||||||
|
"TIRE PRESSURE FL": sc.TIRE_PRESSURE_FL,
|
||||||
# Old unique_id is (previously title-cased) sensor name
|
"TIRE PRESSURE FR": sc.TIRE_PRESSURE_FR,
|
||||||
# (e.g. "VIN_Avg Fuel Consumption")
|
"TIRE PRESSURE RL": sc.TIRE_PRESSURE_RL,
|
||||||
replacements = {str(s.name).upper(): s.key for s in all_sensors}
|
"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
|
@callback
|
||||||
def update_unique_id(entry: er.RegistryEntry) -> dict[str, Any] | None:
|
def update_unique_id(entry: er.RegistryEntry) -> dict[str, Any] | None:
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""Test Subaru sensors."""
|
"""Test Subaru sensors."""
|
||||||
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -12,7 +13,6 @@ from homeassistant.components.subaru.sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.util import slugify
|
|
||||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
from .api_responses import (
|
from .api_responses import (
|
||||||
@ -25,7 +25,6 @@ from .api_responses import (
|
|||||||
from .conftest import (
|
from .conftest import (
|
||||||
MOCK_API_FETCH,
|
MOCK_API_FETCH,
|
||||||
MOCK_API_GET_DATA,
|
MOCK_API_GET_DATA,
|
||||||
TEST_DEVICE_NAME,
|
|
||||||
advance_time_to_next_fetch,
|
advance_time_to_next_fetch,
|
||||||
setup_subaru_config_entry,
|
setup_subaru_config_entry,
|
||||||
)
|
)
|
||||||
@ -65,9 +64,9 @@ async def test_sensors_missing_vin_data(hass: HomeAssistant, ev_entry) -> None:
|
|||||||
{
|
{
|
||||||
"domain": SENSOR_DOMAIN,
|
"domain": SENSOR_DOMAIN,
|
||||||
"platform": SUBARU_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}",
|
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,
|
"domain": SENSOR_DOMAIN,
|
||||||
"platform": SUBARU_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}",
|
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
|
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 = EV_SENSORS
|
||||||
sensor_list.extend(API_GEN_2_SENSORS)
|
sensor_list.extend(API_GEN_2_SENSORS)
|
||||||
sensor_list.extend(SAFETY_SENSORS)
|
sensor_list.extend(SAFETY_SENSORS)
|
||||||
expected_states = {}
|
expected_states = {}
|
||||||
|
entity_registry = er.async_get(hass)
|
||||||
for item in sensor_list:
|
for item in sensor_list:
|
||||||
expected_states[
|
entity = entity_registry.async_get_entity_id(
|
||||||
f"sensor.{slugify(f'{TEST_DEVICE_NAME} {item.name}')}"
|
SENSOR_DOMAIN, SUBARU_DOMAIN, f"{TEST_VIN_2_EV}_{item.key}"
|
||||||
] = expected_state[item.key]
|
)
|
||||||
|
expected_states[entity] = expected_state[item.key]
|
||||||
|
|
||||||
for sensor, value in expected_states.items():
|
for sensor, value in expected_states.items():
|
||||||
actual = hass.states.get(sensor)
|
actual = hass.states.get(sensor)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user