Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dwd_weather_warnings code quality #92738

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions homeassistant/components/dwd_weather_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 16 additions & 27 deletions homeassistant/components/dwd_weather_warnings/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,19 +12,7 @@
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):
Expand Down Expand Up @@ -52,13 +40,16 @@ async def async_step_user(
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:
Expand All @@ -67,12 +58,12 @@ async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
"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
Expand All @@ -81,8 +72,6 @@ async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
):
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)
12 changes: 6 additions & 6 deletions homeassistant/components/dwd_weather_warnings/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
ATTR_WARNING_COUNT,
CONF_REGION_NAME,
CURRENT_WARNING_SENSOR,
DEFAULT_NAME,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
LOGGER,
Expand Down Expand Up @@ -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,
Expand All @@ -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}"
andarotajo marked this conversation as resolved.
Show resolved Hide resolved
self._attr_unique_id = f"{entry.unique_id}-{description.key}"
self._api = api

@property
def native_value(self):
Expand Down
29 changes: 2 additions & 27 deletions tests/components/dwd_weather_warnings/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down Expand Up @@ -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],
}


Expand All @@ -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",
}
Expand Down