mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Remove yaml import trafikverket_train (#71410)
This commit is contained in:
parent
f1733236bb
commit
e5619f4af1
@ -102,11 +102,6 @@ class TVTrainConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def async_step_import(self, config: dict[str, Any] | None) -> FlowResult:
|
||||
"""Import a configuration from config.yaml."""
|
||||
|
||||
return await self.async_step_user(user_input=config)
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
|
@ -5,7 +5,6 @@ DOMAIN = "trafikverket_train"
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
ATTRIBUTION = "Data provided by Trafikverket"
|
||||
|
||||
CONF_TRAINS = "trains"
|
||||
CONF_FROM = "from"
|
||||
CONF_TO = "to"
|
||||
CONF_TIME = "time"
|
||||
|
@ -7,26 +7,19 @@ from typing import Any
|
||||
|
||||
from pytrafikverket import TrafikverketTrain
|
||||
from pytrafikverket.trafikverket_train import StationInfo, TrainStop
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_WEEKDAY, WEEKDAYS
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import dt
|
||||
|
||||
from .const import CONF_FROM, CONF_TIME, CONF_TO, CONF_TRAINS, DOMAIN
|
||||
from .const import CONF_FROM, CONF_TIME, CONF_TO, DOMAIN
|
||||
from .util import create_unique_id
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -43,53 +36,6 @@ ATTR_DEVIATIONS = "deviations"
|
||||
ICON = "mdi:train"
|
||||
SCAN_INTERVAL = timedelta(minutes=5)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_TRAINS): [
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_TO): cv.string,
|
||||
vol.Required(CONF_FROM): cv.string,
|
||||
vol.Optional(CONF_TIME): cv.time,
|
||||
vol.Optional(CONF_WEEKDAY, default=WEEKDAYS): vol.All(
|
||||
cv.ensure_list, [vol.In(WEEKDAYS)]
|
||||
),
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Import Trafikverket Train configuration from YAML."""
|
||||
_LOGGER.warning(
|
||||
# Config flow added in Home Assistant Core 2022.3, remove import flow in 2022.7
|
||||
"Loading Trafikverket Train via platform setup is deprecated; Please remove it from your configuration"
|
||||
)
|
||||
|
||||
for train in config[CONF_TRAINS]:
|
||||
|
||||
new_config = {
|
||||
CONF_API_KEY: config[CONF_API_KEY],
|
||||
CONF_FROM: train[CONF_FROM],
|
||||
CONF_TO: train[CONF_TO],
|
||||
CONF_TIME: str(train.get(CONF_TIME)),
|
||||
CONF_WEEKDAY: train.get(CONF_WEEKDAY, WEEKDAYS),
|
||||
}
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=new_config,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
|
@ -66,81 +66,6 @@ async def test_form(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
|
||||
async def test_import_flow_success(hass: HomeAssistant) -> None:
|
||||
"""Test a successful import of yaml."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.trafikverket_train.config_flow.TrafikverketTrain.async_get_train_station",
|
||||
), patch(
|
||||
"homeassistant.components.trafikverket_train.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_API_KEY: "1234567890",
|
||||
CONF_FROM: "Stockholm C",
|
||||
CONF_TO: "Uppsala C",
|
||||
CONF_TIME: "10:00",
|
||||
CONF_WEEKDAY: ["mon", "fri"],
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result2["title"] == "Stockholm C to Uppsala C at 10:00"
|
||||
assert result2["data"] == {
|
||||
"api_key": "1234567890",
|
||||
"name": "Stockholm C to Uppsala C at 10:00",
|
||||
"from": "Stockholm C",
|
||||
"to": "Uppsala C",
|
||||
"time": "10:00",
|
||||
"weekday": ["mon", "fri"],
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import_flow_already_exist(hass: HomeAssistant) -> None:
|
||||
"""Test import of yaml already exist."""
|
||||
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_API_KEY: "1234567890",
|
||||
CONF_NAME: "Stockholm C to Uppsala C",
|
||||
CONF_FROM: "Stockholm C",
|
||||
CONF_TO: "Uppsala C",
|
||||
CONF_TIME: "10:00",
|
||||
CONF_WEEKDAY: WEEKDAYS,
|
||||
},
|
||||
unique_id=f"stockholmc-uppsalac-10:00-{WEEKDAYS}",
|
||||
).add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.trafikverket_train.async_setup_entry",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.trafikverket_train.config_flow.TrafikverketTrain.async_get_train_station",
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_NAME: "Stockholm C to Uppsala C",
|
||||
CONF_API_KEY: "1234567890",
|
||||
CONF_FROM: "Stockholm C",
|
||||
CONF_TO: "Uppsala C",
|
||||
CONF_TIME: "10:00",
|
||||
CONF_WEEKDAY: WEEKDAYS,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3["type"] == RESULT_TYPE_ABORT
|
||||
assert result3["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error_message,base_error",
|
||||
[
|
||||
|
Loading…
x
Reference in New Issue
Block a user