mirror of
https://github.com/home-assistant/core
synced 2024-10-05 22:37:29 +00:00
Improve dwd_weather_warnings code quality (#92738)
Improve code quality by removing unnecessary data
This commit is contained in:
parent
160fce781d
commit
fa366e59e0
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue