Remove platform YAML from DWD Weather Warnings (#103379)
parent
8e8f2a2163
commit
d0603729f2
|
@ -8,11 +8,10 @@ from dwdwfsapi import DwdWeatherWarningsAPI
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import CONF_REGION_IDENTIFIER, CONF_REGION_NAME, DOMAIN, LOGGER
|
||||
from .const import CONF_REGION_IDENTIFIER, DOMAIN
|
||||
|
||||
|
||||
class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
@ -51,27 +50,3 @@ class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||
}
|
||||
),
|
||||
)
|
||||
|
||||
async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
|
||||
"""Import a config entry from configuration.yaml."""
|
||||
LOGGER.debug(
|
||||
"Starting import of sensor from configuration.yaml - %s", import_config
|
||||
)
|
||||
|
||||
# Extract the necessary data for the setup.
|
||||
region_identifier = import_config[CONF_REGION_NAME]
|
||||
name = import_config.get(CONF_NAME, region_identifier)
|
||||
|
||||
# Set the unique ID for this imported entry.
|
||||
await self.async_set_unique_id(region_identifier)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
# Validate region identifier using the API
|
||||
if not await self.hass.async_add_executor_job(
|
||||
DwdWeatherWarningsAPI, region_identifier
|
||||
):
|
||||
return self.async_abort(reason="invalid_identifier")
|
||||
|
||||
return self.async_create_entry(
|
||||
title=name, data={CONF_REGION_IDENTIFIER: region_identifier}
|
||||
)
|
||||
|
|
|
@ -11,23 +11,11 @@ Wetterwarnungen (Stufe 1)
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Final
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
|
@ -46,7 +34,6 @@ from .const import (
|
|||
ATTR_REGION_ID,
|
||||
ATTR_REGION_NAME,
|
||||
ATTR_WARNING_COUNT,
|
||||
CONF_REGION_NAME,
|
||||
CURRENT_WARNING_SENSOR,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
|
@ -66,49 +53,6 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||
),
|
||||
)
|
||||
|
||||
# Should be removed together with the old YAML configuration.
|
||||
YAML_MONITORED_CONDITIONS: Final = [CURRENT_WARNING_SENSOR, ADVANCE_WARNING_SENSOR]
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_REGION_NAME): cv.string,
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(
|
||||
CONF_MONITORED_CONDITIONS, default=YAML_MONITORED_CONDITIONS
|
||||
): vol.All(cv.ensure_list, [vol.In(YAML_MONITORED_CONDITIONS)]),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Import the configurations from YAML to config flows."""
|
||||
# Show issue as long as the YAML configuration exists.
|
||||
async_create_issue(
|
||||
hass,
|
||||
HOMEASSISTANT_DOMAIN,
|
||||
f"deprecated_yaml_{DOMAIN}",
|
||||
breaks_in_ha_version="2023.12.0",
|
||||
is_fixable=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "Deutscher Wetterdienst (DWD) Weather Warnings",
|
||||
},
|
||||
)
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
|
|
|
@ -12,7 +12,7 @@ from homeassistant.components.dwd_weather_warnings.const import (
|
|||
CURRENT_WARNING_SENSOR,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
@ -70,81 +70,6 @@ async def test_create_entry(hass: HomeAssistant) -> None:
|
|||
}
|
||||
|
||||
|
||||
async def test_import_flow_full_data(hass: HomeAssistant) -> None:
|
||||
"""Test import of a full YAML configuration with both success and failure."""
|
||||
# Test abort due to invalid identifier.
|
||||
with patch(
|
||||
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
|
||||
return_value=False,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=DEMO_YAML_CONFIGURATION.copy(),
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "invalid_identifier"
|
||||
|
||||
# Test successful import.
|
||||
with patch(
|
||||
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=DEMO_YAML_CONFIGURATION.copy(),
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Unit Test"
|
||||
assert result["data"] == {
|
||||
CONF_REGION_IDENTIFIER: "807111000",
|
||||
}
|
||||
|
||||
|
||||
async def test_import_flow_no_name(hass: HomeAssistant) -> None:
|
||||
"""Test a successful import of a YAML configuration with no name set."""
|
||||
data = DEMO_YAML_CONFIGURATION.copy()
|
||||
data.pop(CONF_NAME)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=data
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "807111000"
|
||||
assert result["data"] == {
|
||||
CONF_REGION_IDENTIFIER: "807111000",
|
||||
}
|
||||
|
||||
|
||||
async def test_import_flow_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test aborting, if the warncell ID / name is already configured during the import."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=DEMO_CONFIG_ENTRY.copy(),
|
||||
unique_id=DEMO_CONFIG_ENTRY[CONF_REGION_IDENTIFIER],
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=DEMO_YAML_CONFIGURATION.copy()
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_config_flow_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test aborting, if the warncell ID / name is already configured during the config."""
|
||||
entry = MockConfigEntry(
|
||||
|
|
Loading…
Reference in New Issue