mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Remove deprecated yaml config from trafikverket_weatherstation (#68336)
This commit is contained in:
parent
ffcc02e93d
commit
619c1f014b
@ -1,8 +1,6 @@
|
|||||||
"""Adds config flow for Trafikverket Weather integration."""
|
"""Adds config flow for Trafikverket Weather integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from pytrafikverket.trafikverket_weather import TrafikverketWeather
|
from pytrafikverket.trafikverket_weather import TrafikverketWeather
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -14,13 +12,6 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
|
|
||||||
from .const import CONF_STATION, DOMAIN
|
from .const import CONF_STATION, DOMAIN
|
||||||
|
|
||||||
DATA_SCHEMA = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
|
||||||
vol.Required(CONF_STATION): cv.string,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TVWeatherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class TVWeatherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for Trafikverket Weatherstation integration."""
|
"""Handle a config flow for Trafikverket Weatherstation integration."""
|
||||||
@ -39,18 +30,8 @@ class TVWeatherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
return str(err)
|
return str(err)
|
||||||
return "connected"
|
return "connected"
|
||||||
|
|
||||||
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
|
|
||||||
"""Import a configuration from config.yaml."""
|
|
||||||
|
|
||||||
self.context.update(
|
|
||||||
{"title_placeholders": {CONF_STATION: f"YAML import {DOMAIN}"}}
|
|
||||||
)
|
|
||||||
|
|
||||||
self._async_abort_entries_match({CONF_STATION: config[CONF_STATION]})
|
|
||||||
return await self.async_step_user(user_input=config)
|
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, str] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
errors = {}
|
errors = {}
|
||||||
@ -80,6 +61,11 @@ class TVWeatherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=DATA_SCHEMA,
|
data_schema=vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
|
vol.Required(CONF_STATION): cv.string,
|
||||||
|
}
|
||||||
|
),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
@ -8,20 +8,16 @@ import logging
|
|||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from pytrafikverket.trafikverket_weather import TrafikverketWeather, WeatherStationInfo
|
from pytrafikverket.trafikverket_weather import TrafikverketWeather, WeatherStationInfo
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
CONF_MONITORED_CONDITIONS,
|
|
||||||
CONF_NAME,
|
|
||||||
DEGREE,
|
DEGREE,
|
||||||
LENGTH_MILLIMETERS,
|
LENGTH_MILLIMETERS,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -30,11 +26,9 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
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.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -151,37 +145,6 @@ SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
SENSOR_KEYS = [desc.key for desc in SENSOR_TYPES]
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
|
||||||
{
|
|
||||||
vol.Required(CONF_NAME): cv.string,
|
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
|
||||||
vol.Required(CONF_STATION): cv.string,
|
|
||||||
vol.Required(CONF_MONITORED_CONDITIONS, default=[]): [vol.In(SENSOR_KEYS)],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
config: ConfigType,
|
|
||||||
async_add_entities: AddEntitiesCallback,
|
|
||||||
discovery_info: DiscoveryInfoType | None = None,
|
|
||||||
) -> None:
|
|
||||||
"""Import Trafikverket Weather configuration from YAML."""
|
|
||||||
_LOGGER.warning(
|
|
||||||
# Config flow added in Home Assistant Core 2021.12, remove import flow in 2022.4
|
|
||||||
"Loading Trafikverket Weather via platform setup is deprecated; Please remove it from your configuration"
|
|
||||||
)
|
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": SOURCE_IMPORT},
|
|
||||||
data=config,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
@ -6,12 +6,10 @@ from unittest.mock import patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_NAME
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import RESULT_TYPE_FORM
|
from homeassistant.data_entry_flow import RESULT_TYPE_FORM
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
|
||||||
|
|
||||||
DOMAIN = "trafikverket_weatherstation"
|
DOMAIN = "trafikverket_weatherstation"
|
||||||
CONF_STATION = "station"
|
CONF_STATION = "station"
|
||||||
|
|
||||||
@ -49,67 +47,6 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_import_flow_success(hass: HomeAssistant) -> None:
|
|
||||||
"""Test a successful import of yaml."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
|
|
||||||
), patch(
|
|
||||||
"homeassistant.components.trafikverket_weatherstation.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_NAME: "Vallby",
|
|
||||||
CONF_API_KEY: "1234567890",
|
|
||||||
CONF_STATION: "Vallby",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] == "create_entry"
|
|
||||||
assert result2["title"] == "Vallby"
|
|
||||||
assert result2["data"] == {
|
|
||||||
"api_key": "1234567890",
|
|
||||||
"station": "Vallby",
|
|
||||||
}
|
|
||||||
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_STATION: "Vallby",
|
|
||||||
},
|
|
||||||
).add_to_hass(hass)
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.trafikverket_weatherstation.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
), patch(
|
|
||||||
"homeassistant.components.trafikverket_weatherstation.config_flow.TrafikverketWeather.async_get_weather",
|
|
||||||
):
|
|
||||||
result3 = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={
|
|
||||||
CONF_NAME: "Vallby",
|
|
||||||
CONF_API_KEY: "1234567890",
|
|
||||||
CONF_STATION: "Vallby",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result3["type"] == "abort"
|
|
||||||
assert result3["reason"] == "already_configured"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"error_message,base_error",
|
"error_message,base_error",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user