diff --git a/homeassistant/components/dwd_weather_warnings/__init__.py b/homeassistant/components/dwd_weather_warnings/__init__.py index 99c3a110caa..55309110caf 100644 --- a/homeassistant/components/dwd_weather_warnings/__init__.py +++ b/homeassistant/components/dwd_weather_warnings/__init__.py @@ -17,9 +17,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Initialize the API. api = await hass.async_add_executor_job(DwdWeatherWarningsAPI, region_identifier) - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][entry.entry_id] = api - + hass.data.setdefault(DOMAIN, {})[entry.entry_id] = api await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True diff --git a/homeassistant/components/dwd_weather_warnings/config_flow.py b/homeassistant/components/dwd_weather_warnings/config_flow.py index 653812632c0..e806db7ec91 100644 --- a/homeassistant/components/dwd_weather_warnings/config_flow.py +++ b/homeassistant/components/dwd_weather_warnings/config_flow.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Any, Final +from typing import Any from dwdwfsapi import DwdWeatherWarningsAPI import voluptuous as vol @@ -12,19 +12,7 @@ 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, - DEFAULT_NAME, - DOMAIN, - LOGGER, -) - -CONFIG_SCHEMA: Final = vol.Schema( - { - vol.Required(CONF_REGION_IDENTIFIER): cv.string, - } -) +from .const import CONF_REGION_IDENTIFIER, CONF_REGION_NAME, DOMAIN, LOGGER class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN): @@ -52,13 +40,16 @@ class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN): await self.async_set_unique_id(region_identifier) self._abort_if_unique_id_configured() - # Set the name for this config entry. - name = f"{DEFAULT_NAME} {region_identifier}" - - return self.async_create_entry(title=name, data=user_input) + return self.async_create_entry(title=region_identifier, data=user_input) return self.async_show_form( - step_id="user", errors=errors, data_schema=CONFIG_SCHEMA + step_id="user", + errors=errors, + data_schema=vol.Schema( + { + vol.Required(CONF_REGION_IDENTIFIER): cv.string, + } + ), ) async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult: @@ -67,12 +58,12 @@ class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN): "Starting import of sensor from configuration.yaml - %s", import_config ) - # Adjust data to new format. - region_identifier = import_config.pop(CONF_REGION_NAME) - import_config[CONF_REGION_IDENTIFIER] = region_identifier + # 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(import_config[CONF_REGION_IDENTIFIER]) + await self.async_set_unique_id(region_identifier) self._abort_if_unique_id_configured() # Validate region identifier using the API @@ -81,8 +72,6 @@ class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN): ): return self.async_abort(reason="invalid_identifier") - name = import_config.get( - CONF_NAME, f"{DEFAULT_NAME} {import_config[CONF_REGION_IDENTIFIER]}" + return self.async_create_entry( + title=name, data={CONF_REGION_IDENTIFIER: region_identifier} ) - - return self.async_create_entry(title=name, data=import_config) diff --git a/homeassistant/components/dwd_weather_warnings/sensor.py b/homeassistant/components/dwd_weather_warnings/sensor.py index de96b3e9e08..26aa0ef1ba0 100644 --- a/homeassistant/components/dwd_weather_warnings/sensor.py +++ b/homeassistant/components/dwd_weather_warnings/sensor.py @@ -47,6 +47,7 @@ from .const import ( ATTR_WARNING_COUNT, CONF_REGION_NAME, CURRENT_WARNING_SENSOR, + DEFAULT_NAME, DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, @@ -112,7 +113,7 @@ async def async_setup_entry( async_add_entities( [ - DwdWeatherWarningsSensor(api, entry.title, entry.unique_id, description) + DwdWeatherWarningsSensor(api, entry, description) for description in SENSOR_TYPES ], True, @@ -127,15 +128,14 @@ class DwdWeatherWarningsSensor(SensorEntity): def __init__( self, api, - name, - unique_id, + entry: ConfigEntry, description: SensorEntityDescription, ) -> None: """Initialize a DWD-Weather-Warnings sensor.""" - self._api = api self.entity_description = description - self._attr_name = f"{name} {description.name}" - self._attr_unique_id = f"{unique_id}-{description.key}" + self._attr_name = f"{DEFAULT_NAME} {entry.title} {description.name}" + self._attr_unique_id = f"{entry.unique_id}-{description.key}" + self._api = api @property def native_value(self): diff --git a/tests/components/dwd_weather_warnings/test_config_flow.py b/tests/components/dwd_weather_warnings/test_config_flow.py index 9c730eacffd..819d98e25ef 100644 --- a/tests/components/dwd_weather_warnings/test_config_flow.py +++ b/tests/components/dwd_weather_warnings/test_config_flow.py @@ -64,7 +64,7 @@ async def test_create_entry(hass: HomeAssistant) -> None: # Test for successfully created entry. await hass.async_block_till_done() assert result["type"] == FlowResultType.CREATE_ENTRY - assert result["title"] == "DWD Weather Warnings 807111000" + assert result["title"] == "807111000" assert result["data"] == { CONF_REGION_IDENTIFIER: "807111000", } @@ -102,9 +102,7 @@ async def test_import_flow_full_data(hass: HomeAssistant) -> None: assert result["type"] == FlowResultType.CREATE_ENTRY assert result["title"] == "Unit Test" assert result["data"] == { - CONF_NAME: "Unit Test", CONF_REGION_IDENTIFIER: "807111000", - CONF_MONITORED_CONDITIONS: [CURRENT_WARNING_SENSOR, ADVANCE_WARNING_SENSOR], } @@ -123,30 +121,7 @@ async def test_import_flow_no_name(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result["type"] == FlowResultType.CREATE_ENTRY - assert result["title"] == "DWD Weather Warnings 807111000" - assert result["data"] == { - CONF_REGION_IDENTIFIER: "807111000", - CONF_MONITORED_CONDITIONS: [CURRENT_WARNING_SENSOR, ADVANCE_WARNING_SENSOR], - } - - -async def test_import_flow_only_required(hass: HomeAssistant) -> None: - """Test a successful import of a YAML configuration with only required properties.""" - data = DEMO_YAML_CONFIGURATION.copy() - data.pop(CONF_NAME) - data.pop(CONF_MONITORED_CONDITIONS) - - 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"] == "DWD Weather Warnings 807111000" + assert result["title"] == "807111000" assert result["data"] == { CONF_REGION_IDENTIFIER: "807111000", }