mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +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.
|
# Initialize the API.
|
||||||
api = await hass.async_add_executor_job(DwdWeatherWarningsAPI, region_identifier)
|
api = await hass.async_add_executor_job(DwdWeatherWarningsAPI, region_identifier)
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = api
|
||||||
hass.data[DOMAIN][entry.entry_id] = api
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Final
|
from typing import Any
|
||||||
|
|
||||||
from dwdwfsapi import DwdWeatherWarningsAPI
|
from dwdwfsapi import DwdWeatherWarningsAPI
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -12,19 +12,7 @@ from homeassistant.const import CONF_NAME
|
|||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from .const import (
|
from .const import CONF_REGION_IDENTIFIER, CONF_REGION_NAME, DOMAIN, LOGGER
|
||||||
CONF_REGION_IDENTIFIER,
|
|
||||||
CONF_REGION_NAME,
|
|
||||||
DEFAULT_NAME,
|
|
||||||
DOMAIN,
|
|
||||||
LOGGER,
|
|
||||||
)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA: Final = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(CONF_REGION_IDENTIFIER): cv.string,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN):
|
class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
@ -52,13 +40,16 @@ class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
await self.async_set_unique_id(region_identifier)
|
await self.async_set_unique_id(region_identifier)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
# Set the name for this config entry.
|
return self.async_create_entry(title=region_identifier, data=user_input)
|
||||||
name = f"{DEFAULT_NAME} {region_identifier}"
|
|
||||||
|
|
||||||
return self.async_create_entry(title=name, data=user_input)
|
|
||||||
|
|
||||||
return self.async_show_form(
|
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:
|
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
|
"Starting import of sensor from configuration.yaml - %s", import_config
|
||||||
)
|
)
|
||||||
|
|
||||||
# Adjust data to new format.
|
# Extract the necessary data for the setup.
|
||||||
region_identifier = import_config.pop(CONF_REGION_NAME)
|
region_identifier = import_config[CONF_REGION_NAME]
|
||||||
import_config[CONF_REGION_IDENTIFIER] = region_identifier
|
name = import_config.get(CONF_NAME, region_identifier)
|
||||||
|
|
||||||
# Set the unique ID for this imported entry.
|
# 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()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
# Validate region identifier using the API
|
# Validate region identifier using the API
|
||||||
@ -81,8 +72,6 @@ class DwdWeatherWarningsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
):
|
):
|
||||||
return self.async_abort(reason="invalid_identifier")
|
return self.async_abort(reason="invalid_identifier")
|
||||||
|
|
||||||
name = import_config.get(
|
return self.async_create_entry(
|
||||||
CONF_NAME, f"{DEFAULT_NAME} {import_config[CONF_REGION_IDENTIFIER]}"
|
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,
|
ATTR_WARNING_COUNT,
|
||||||
CONF_REGION_NAME,
|
CONF_REGION_NAME,
|
||||||
CURRENT_WARNING_SENSOR,
|
CURRENT_WARNING_SENSOR,
|
||||||
|
DEFAULT_NAME,
|
||||||
DEFAULT_SCAN_INTERVAL,
|
DEFAULT_SCAN_INTERVAL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
@ -112,7 +113,7 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
DwdWeatherWarningsSensor(api, entry.title, entry.unique_id, description)
|
DwdWeatherWarningsSensor(api, entry, description)
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
],
|
],
|
||||||
True,
|
True,
|
||||||
@ -127,15 +128,14 @@ class DwdWeatherWarningsSensor(SensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
api,
|
api,
|
||||||
name,
|
entry: ConfigEntry,
|
||||||
unique_id,
|
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a DWD-Weather-Warnings sensor."""
|
"""Initialize a DWD-Weather-Warnings sensor."""
|
||||||
self._api = api
|
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_name = f"{name} {description.name}"
|
self._attr_name = f"{DEFAULT_NAME} {entry.title} {description.name}"
|
||||||
self._attr_unique_id = f"{unique_id}-{description.key}"
|
self._attr_unique_id = f"{entry.unique_id}-{description.key}"
|
||||||
|
self._api = api
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
|
@ -64,7 +64,7 @@ async def test_create_entry(hass: HomeAssistant) -> None:
|
|||||||
# Test for successfully created entry.
|
# Test for successfully created entry.
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "DWD Weather Warnings 807111000"
|
assert result["title"] == "807111000"
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
CONF_REGION_IDENTIFIER: "807111000",
|
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["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "Unit Test"
|
assert result["title"] == "Unit Test"
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
CONF_NAME: "Unit Test",
|
|
||||||
CONF_REGION_IDENTIFIER: "807111000",
|
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()
|
await hass.async_block_till_done()
|
||||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "DWD Weather Warnings 807111000"
|
assert result["title"] == "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["data"] == {
|
assert result["data"] == {
|
||||||
CONF_REGION_IDENTIFIER: "807111000",
|
CONF_REGION_IDENTIFIER: "807111000",
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user