mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Remove config entry unique id from trafikverket_train (#130989)
This commit is contained in:
parent
1020d75b94
commit
3c96c559dc
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
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
|
||||||
@ -11,6 +13,8 @@ from .coordinator import TVDataUpdateCoordinator
|
|||||||
|
|
||||||
TVTrainConfigEntry = ConfigEntry[TVDataUpdateCoordinator]
|
TVTrainConfigEntry = ConfigEntry[TVDataUpdateCoordinator]
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: TVTrainConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: TVTrainConfigEntry) -> bool:
|
||||||
"""Set up Trafikverket Train from a config entry."""
|
"""Set up Trafikverket Train from a config entry."""
|
||||||
@ -42,3 +46,24 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||||
"""Handle options update."""
|
"""Handle options update."""
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_migrate_entry(hass: HomeAssistant, entry: TVTrainConfigEntry) -> bool:
|
||||||
|
"""Migrate config entry."""
|
||||||
|
_LOGGER.debug("Migrating from version %s", entry.version)
|
||||||
|
|
||||||
|
if entry.version > 1:
|
||||||
|
# This means the user has downgraded from a future version
|
||||||
|
return False
|
||||||
|
|
||||||
|
if entry.version == 1 and entry.minor_version == 1:
|
||||||
|
# Remove unique id
|
||||||
|
hass.config_entries.async_update_entry(entry, unique_id=None, minor_version=2)
|
||||||
|
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Migration to version %s.%s successful",
|
||||||
|
entry.version,
|
||||||
|
entry.minor_version,
|
||||||
|
)
|
||||||
|
|
||||||
|
return True
|
||||||
|
@ -37,7 +37,7 @@ from homeassistant.helpers.selector import (
|
|||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import CONF_FILTER_PRODUCT, CONF_FROM, CONF_TIME, CONF_TO, DOMAIN
|
from .const import CONF_FILTER_PRODUCT, CONF_FROM, CONF_TIME, CONF_TO, DOMAIN
|
||||||
from .util import create_unique_id, next_departuredate
|
from .util import next_departuredate
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -125,6 +125,7 @@ class TVTrainConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Handle a config flow for Trafikverket Train integration."""
|
"""Handle a config flow for Trafikverket Train integration."""
|
||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
MINOR_VERSION = 2
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
@ -202,11 +203,16 @@ class TVTrainConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
filter_product,
|
filter_product,
|
||||||
)
|
)
|
||||||
if not errors:
|
if not errors:
|
||||||
unique_id = create_unique_id(
|
self._async_abort_entries_match(
|
||||||
train_from, train_to, train_time, train_days
|
{
|
||||||
|
CONF_API_KEY: api_key,
|
||||||
|
CONF_FROM: train_from,
|
||||||
|
CONF_TO: train_to,
|
||||||
|
CONF_TIME: train_time,
|
||||||
|
CONF_WEEKDAY: train_days,
|
||||||
|
CONF_FILTER_PRODUCT: filter_product,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
await self.async_set_unique_id(unique_id)
|
|
||||||
self._abort_if_unique_id_configured()
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=name,
|
title=name,
|
||||||
data={
|
data={
|
||||||
|
@ -2,22 +2,11 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import date, time, timedelta
|
from datetime import date, timedelta
|
||||||
|
|
||||||
from homeassistant.const import WEEKDAYS
|
from homeassistant.const import WEEKDAYS
|
||||||
|
|
||||||
|
|
||||||
def create_unique_id(
|
|
||||||
from_station: str, to_station: str, depart_time: time | str | None, weekdays: list
|
|
||||||
) -> str:
|
|
||||||
"""Create unique id."""
|
|
||||||
timestr = str(depart_time) if depart_time else ""
|
|
||||||
return (
|
|
||||||
f"{from_station.casefold().replace(' ', '')}-{to_station.casefold().replace(' ', '')}"
|
|
||||||
f"-{timestr.casefold().replace(' ', '')}-{weekdays!s}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def next_weekday(fromdate: date, weekday: int) -> date:
|
def next_weekday(fromdate: date, weekday: int) -> date:
|
||||||
"""Return the date of the next time a specific weekday happen."""
|
"""Return the date of the next time a specific weekday happen."""
|
||||||
days_ahead = weekday - fromdate.weekday()
|
days_ahead = weekday - fromdate.weekday()
|
||||||
|
@ -50,7 +50,8 @@ async def load_integration_from_entry(
|
|||||||
data=ENTRY_CONFIG,
|
data=ENTRY_CONFIG,
|
||||||
options=OPTIONS_CONFIG,
|
options=OPTIONS_CONFIG,
|
||||||
entry_id="1",
|
entry_id="1",
|
||||||
unique_id="stockholmc-uppsalac--['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
await setup_config_entry_with_mocked_data(config_entry.entry_id)
|
await setup_config_entry_with_mocked_data(config_entry.entry_id)
|
||||||
@ -60,7 +61,8 @@ async def load_integration_from_entry(
|
|||||||
source=SOURCE_USER,
|
source=SOURCE_USER,
|
||||||
data=ENTRY_CONFIG2,
|
data=ENTRY_CONFIG2,
|
||||||
entry_id="2",
|
entry_id="2",
|
||||||
unique_id="stockholmc-uppsalac-1100-['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
config_entry2.add_to_hass(hass)
|
config_entry2.add_to_hass(hass)
|
||||||
await setup_config_entry_with_mocked_data(config_entry2.entry_id)
|
await setup_config_entry_with_mocked_data(config_entry2.entry_id)
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
'title_placeholders': dict({
|
'title_placeholders': dict({
|
||||||
'name': 'Mock Title',
|
'name': 'Mock Title',
|
||||||
}),
|
}),
|
||||||
'unique_id': '321',
|
'unique_id': None,
|
||||||
}),
|
}),
|
||||||
'flow_id': <ANY>,
|
'flow_id': <ANY>,
|
||||||
'handler': 'trafikverket_train',
|
'handler': 'trafikverket_train',
|
||||||
|
@ -222,7 +222,7 @@
|
|||||||
'title_placeholders': dict({
|
'title_placeholders': dict({
|
||||||
'name': 'Mock Title',
|
'name': 'Mock Title',
|
||||||
}),
|
}),
|
||||||
'unique_id': "stockholmc-uppsalac--['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']",
|
'unique_id': None,
|
||||||
}),
|
}),
|
||||||
'flow_id': <ANY>,
|
'flow_id': <ANY>,
|
||||||
'handler': 'trafikverket_train',
|
'handler': 'trafikverket_train',
|
||||||
|
@ -16,6 +16,7 @@ from pytrafikverket.models import TrainStopModel
|
|||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.trafikverket_train.const import (
|
from homeassistant.components.trafikverket_train.const import (
|
||||||
|
CONF_FILTER_PRODUCT,
|
||||||
CONF_FROM,
|
CONF_FROM,
|
||||||
CONF_TIME,
|
CONF_TIME,
|
||||||
CONF_TO,
|
CONF_TO,
|
||||||
@ -73,7 +74,6 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||||||
}
|
}
|
||||||
assert result["options"] == {"filter_product": None}
|
assert result["options"] == {"filter_product": None}
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
assert result["result"].unique_id == "stockholmc-uppsalac-10:00-['mon', 'fri']"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_entry_already_exist(hass: HomeAssistant) -> None:
|
async def test_form_entry_already_exist(hass: HomeAssistant) -> None:
|
||||||
@ -88,8 +88,10 @@ async def test_form_entry_already_exist(hass: HomeAssistant) -> None:
|
|||||||
CONF_TO: "Uppsala C",
|
CONF_TO: "Uppsala C",
|
||||||
CONF_TIME: "10:00",
|
CONF_TIME: "10:00",
|
||||||
CONF_WEEKDAY: WEEKDAYS,
|
CONF_WEEKDAY: WEEKDAYS,
|
||||||
|
CONF_FILTER_PRODUCT: None,
|
||||||
},
|
},
|
||||||
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -240,7 +242,8 @@ async def test_reauth_flow(hass: HomeAssistant) -> None:
|
|||||||
CONF_TIME: "10:00",
|
CONF_TIME: "10:00",
|
||||||
CONF_WEEKDAY: WEEKDAYS,
|
CONF_WEEKDAY: WEEKDAYS,
|
||||||
},
|
},
|
||||||
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -314,7 +317,8 @@ async def test_reauth_flow_error(
|
|||||||
CONF_TIME: "10:00",
|
CONF_TIME: "10:00",
|
||||||
CONF_WEEKDAY: WEEKDAYS,
|
CONF_WEEKDAY: WEEKDAYS,
|
||||||
},
|
},
|
||||||
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -396,7 +400,8 @@ async def test_reauth_flow_error_departures(
|
|||||||
CONF_TIME: "10:00",
|
CONF_TIME: "10:00",
|
||||||
CONF_WEEKDAY: WEEKDAYS,
|
CONF_WEEKDAY: WEEKDAYS,
|
||||||
},
|
},
|
||||||
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -467,7 +472,8 @@ async def test_options_flow(
|
|||||||
CONF_TIME: "10:00",
|
CONF_TIME: "10:00",
|
||||||
CONF_WEEKDAY: WEEKDAYS,
|
CONF_WEEKDAY: WEEKDAYS,
|
||||||
},
|
},
|
||||||
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
@ -28,7 +28,8 @@ async def test_unload_entry(
|
|||||||
data=ENTRY_CONFIG,
|
data=ENTRY_CONFIG,
|
||||||
options=OPTIONS_CONFIG,
|
options=OPTIONS_CONFIG,
|
||||||
entry_id="1",
|
entry_id="1",
|
||||||
unique_id="321",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -64,7 +65,8 @@ async def test_auth_failed(
|
|||||||
data=ENTRY_CONFIG,
|
data=ENTRY_CONFIG,
|
||||||
options=OPTIONS_CONFIG,
|
options=OPTIONS_CONFIG,
|
||||||
entry_id="1",
|
entry_id="1",
|
||||||
unique_id="321",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -94,7 +96,8 @@ async def test_no_stations(
|
|||||||
data=ENTRY_CONFIG,
|
data=ENTRY_CONFIG,
|
||||||
options=OPTIONS_CONFIG,
|
options=OPTIONS_CONFIG,
|
||||||
entry_id="1",
|
entry_id="1",
|
||||||
unique_id="321",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -121,7 +124,8 @@ async def test_migrate_entity_unique_id(
|
|||||||
data=ENTRY_CONFIG,
|
data=ENTRY_CONFIG,
|
||||||
options=OPTIONS_CONFIG,
|
options=OPTIONS_CONFIG,
|
||||||
entry_id="1",
|
entry_id="1",
|
||||||
unique_id="321",
|
version=1,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -149,3 +153,69 @@ async def test_migrate_entity_unique_id(
|
|||||||
|
|
||||||
entity = entity_registry.async_get(entity.entity_id)
|
entity = entity_registry.async_get(entity.entity_id)
|
||||||
assert entity.unique_id == f"{entry.entry_id}-departure_time"
|
assert entity.unique_id == f"{entry.entry_id}-departure_time"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_migrate_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
get_trains: list[TrainStopModel],
|
||||||
|
) -> None:
|
||||||
|
"""Test migrate entry unique id."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
source=SOURCE_USER,
|
||||||
|
data=ENTRY_CONFIG,
|
||||||
|
options=OPTIONS_CONFIG,
|
||||||
|
version=1,
|
||||||
|
minor_version=1,
|
||||||
|
entry_id="1",
|
||||||
|
unique_id="321",
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.trafikverket_train.coordinator.TrafikverketTrain.async_get_train_station",
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.trafikverket_train.coordinator.TrafikverketTrain.async_get_next_train_stops",
|
||||||
|
return_value=get_trains,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
assert entry.version == 1
|
||||||
|
assert entry.minor_version == 2
|
||||||
|
assert entry.unique_id is None
|
||||||
|
|
||||||
|
|
||||||
|
async def test_migrate_entry_from_future_version_fails(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
get_trains: list[TrainStopModel],
|
||||||
|
) -> None:
|
||||||
|
"""Test migrate entry from future version fails."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
source=SOURCE_USER,
|
||||||
|
data=ENTRY_CONFIG,
|
||||||
|
options=OPTIONS_CONFIG,
|
||||||
|
version=2,
|
||||||
|
entry_id="1",
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.trafikverket_train.coordinator.TrafikverketTrain.async_get_train_station",
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.trafikverket_train.coordinator.TrafikverketTrain.async_get_next_train_stops",
|
||||||
|
return_value=get_trains,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert entry.state is ConfigEntryState.MIGRATION_ERROR
|
||||||
|
Loading…
x
Reference in New Issue
Block a user