mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Remove not needed name from config flow in SMHI (#134841)
This commit is contained in:
parent
235fda55fe
commit
2ec971ad9d
@ -32,6 +32,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Migrate old entry."""
|
"""Migrate old entry."""
|
||||||
|
|
||||||
|
if entry.version > 3:
|
||||||
|
# Downgrade from future version
|
||||||
|
return False
|
||||||
|
|
||||||
if entry.version == 1:
|
if entry.version == 1:
|
||||||
new_data = {
|
new_data = {
|
||||||
CONF_NAME: entry.data[CONF_NAME],
|
CONF_NAME: entry.data[CONF_NAME],
|
||||||
@ -40,8 +45,11 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
CONF_LONGITUDE: entry.data[CONF_LONGITUDE],
|
CONF_LONGITUDE: entry.data[CONF_LONGITUDE],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
hass.config_entries.async_update_entry(entry, data=new_data, version=2)
|
||||||
|
|
||||||
if not hass.config_entries.async_update_entry(entry, data=new_data, version=2):
|
if entry.version == 2:
|
||||||
return False
|
new_data = entry.data.copy()
|
||||||
|
new_data.pop(CONF_NAME)
|
||||||
|
hass.config_entries.async_update_entry(entry, data=new_data, version=3)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -9,7 +9,7 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
|
from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE, CONF_NAME
|
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
aiohttp_client,
|
aiohttp_client,
|
||||||
@ -38,7 +38,7 @@ async def async_check_location(
|
|||||||
class SmhiFlowHandler(ConfigFlow, domain=DOMAIN):
|
class SmhiFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
"""Config flow for SMHI component."""
|
"""Config flow for SMHI component."""
|
||||||
|
|
||||||
VERSION = 2
|
VERSION = 3
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
@ -58,10 +58,6 @@ class SmhiFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
):
|
):
|
||||||
name = HOME_LOCATION_NAME
|
name = HOME_LOCATION_NAME
|
||||||
|
|
||||||
user_input[CONF_NAME] = (
|
|
||||||
HOME_LOCATION_NAME if name == HOME_LOCATION_NAME else DEFAULT_NAME
|
|
||||||
)
|
|
||||||
|
|
||||||
await self.async_set_unique_id(f"{lat}-{lon}")
|
await self.async_set_unique_id(f"{lat}-{lon}")
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_create_entry(title=name, data=user_input)
|
return self.async_create_entry(title=name, data=user_input)
|
||||||
|
@ -48,7 +48,6 @@ from homeassistant.const import (
|
|||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
CONF_LOCATION,
|
CONF_LOCATION,
|
||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
CONF_NAME,
|
|
||||||
UnitOfLength,
|
UnitOfLength,
|
||||||
UnitOfPrecipitationDepth,
|
UnitOfPrecipitationDepth,
|
||||||
UnitOfPressure,
|
UnitOfPressure,
|
||||||
@ -60,7 +59,7 @@ from homeassistant.helpers import aiohttp_client, sun
|
|||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.util import Throttle, dt as dt_util, slugify
|
from homeassistant.util import Throttle, dt as dt_util
|
||||||
|
|
||||||
from .const import ATTR_SMHI_THUNDER_PROBABILITY, DOMAIN, ENTITY_ID_SENSOR_FORMAT
|
from .const import ATTR_SMHI_THUNDER_PROBABILITY, DOMAIN, ENTITY_ID_SENSOR_FORMAT
|
||||||
|
|
||||||
@ -103,17 +102,15 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Add a weather entity from map location."""
|
"""Add a weather entity from map location."""
|
||||||
location = config_entry.data
|
location = config_entry.data
|
||||||
name = slugify(location[CONF_NAME])
|
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
|
|
||||||
entity = SmhiWeather(
|
entity = SmhiWeather(
|
||||||
location[CONF_NAME],
|
|
||||||
location[CONF_LOCATION][CONF_LATITUDE],
|
location[CONF_LOCATION][CONF_LATITUDE],
|
||||||
location[CONF_LOCATION][CONF_LONGITUDE],
|
location[CONF_LOCATION][CONF_LONGITUDE],
|
||||||
session=session,
|
session=session,
|
||||||
)
|
)
|
||||||
entity.entity_id = ENTITY_ID_SENSOR_FORMAT.format(name)
|
entity.entity_id = ENTITY_ID_SENSOR_FORMAT.format(config_entry.title)
|
||||||
|
|
||||||
async_add_entities([entity], True)
|
async_add_entities([entity], True)
|
||||||
|
|
||||||
@ -136,7 +133,6 @@ class SmhiWeather(WeatherEntity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str,
|
|
||||||
latitude: str,
|
latitude: str,
|
||||||
longitude: str,
|
longitude: str,
|
||||||
session: aiohttp.ClientSession,
|
session: aiohttp.ClientSession,
|
||||||
@ -152,7 +148,6 @@ class SmhiWeather(WeatherEntity):
|
|||||||
identifiers={(DOMAIN, f"{latitude}, {longitude}")},
|
identifiers={(DOMAIN, f"{latitude}, {longitude}")},
|
||||||
manufacturer="SMHI",
|
manufacturer="SMHI",
|
||||||
model="v2",
|
model="v2",
|
||||||
name=name,
|
|
||||||
configuration_url="http://opendata.smhi.se/apidocs/metfcst/parameters.html",
|
configuration_url="http://opendata.smhi.se/apidocs/metfcst/parameters.html",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
ENTITY_ID = "weather.smhi_test"
|
ENTITY_ID = "weather.smhi_test"
|
||||||
TEST_CONFIG = {
|
TEST_CONFIG = {
|
||||||
"name": "test",
|
|
||||||
"location": {
|
"location": {
|
||||||
"longitude": "17.84197",
|
"longitude": "17.84197",
|
||||||
"latitude": "59.32624",
|
"latitude": "59.32624",
|
||||||
@ -11,5 +10,5 @@ TEST_CONFIG = {
|
|||||||
TEST_CONFIG_MIGRATE = {
|
TEST_CONFIG_MIGRATE = {
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"longitude": "17.84197",
|
"longitude": "17.84197",
|
||||||
"latitude": "17.84197",
|
"latitude": "59.32624",
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,6 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||||||
"latitude": 0.0,
|
"latitude": 0.0,
|
||||||
"longitude": 0.0,
|
"longitude": 0.0,
|
||||||
},
|
},
|
||||||
"name": "Home",
|
|
||||||
}
|
}
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
@ -93,7 +92,6 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||||||
"latitude": 1.0,
|
"latitude": 1.0,
|
||||||
"longitude": 1.0,
|
"longitude": 1.0,
|
||||||
},
|
},
|
||||||
"name": "Weather",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -150,7 +148,6 @@ async def test_form_invalid_coordinates(hass: HomeAssistant) -> None:
|
|||||||
"latitude": 2.0,
|
"latitude": 2.0,
|
||||||
"longitude": 2.0,
|
"longitude": 2.0,
|
||||||
},
|
},
|
||||||
"name": "Weather",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -201,8 +198,8 @@ async def test_reconfigure_flow(
|
|||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
title="Home",
|
title="Home",
|
||||||
unique_id="57.2898-13.6304",
|
unique_id="57.2898-13.6304",
|
||||||
data={"location": {"latitude": 57.2898, "longitude": 13.6304}, "name": "Home"},
|
data={"location": {"latitude": 57.2898, "longitude": 13.6304}},
|
||||||
version=2,
|
version=3,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -269,7 +266,6 @@ async def test_reconfigure_flow(
|
|||||||
"latitude": 58.2898,
|
"latitude": 58.2898,
|
||||||
"longitude": 14.6304,
|
"longitude": 14.6304,
|
||||||
},
|
},
|
||||||
"name": "Home",
|
|
||||||
}
|
}
|
||||||
entity = entity_registry.async_get(entity.entity_id)
|
entity = entity_registry.async_get(entity.entity_id)
|
||||||
assert entity
|
assert entity
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
"""Test SMHI component setup process."""
|
"""Test SMHI component setup process."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from smhi.smhi_lib import APIURL_TEMPLATE
|
from smhi.smhi_lib import APIURL_TEMPLATE
|
||||||
|
|
||||||
from homeassistant.components.smhi.const import DOMAIN
|
from homeassistant.components.smhi.const import DOMAIN
|
||||||
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
@ -22,7 +21,7 @@ async def test_setup_entry(
|
|||||||
TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"]
|
TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"]
|
||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain=DOMAIN, title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -40,7 +39,7 @@ async def test_remove_entry(
|
|||||||
TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"]
|
TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"]
|
||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain=DOMAIN, title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -77,7 +76,7 @@ async def test_migrate_entry(
|
|||||||
original_name="Weather",
|
original_name="Weather",
|
||||||
platform="smhi",
|
platform="smhi",
|
||||||
supported_features=0,
|
supported_features=0,
|
||||||
unique_id="17.84197, 17.84197",
|
unique_id="59.32624, 17.84197",
|
||||||
)
|
)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -86,30 +85,27 @@ async def test_migrate_entry(
|
|||||||
state = hass.states.get(entity.entity_id)
|
state = hass.states.get(entity.entity_id)
|
||||||
assert state
|
assert state
|
||||||
|
|
||||||
assert entry.version == 2
|
assert entry.version == 3
|
||||||
assert entry.unique_id == "17.84197-17.84197"
|
assert entry.unique_id == "59.32624-17.84197"
|
||||||
|
assert entry.data == TEST_CONFIG
|
||||||
|
|
||||||
entity_get = entity_registry.async_get(entity.entity_id)
|
entity_get = entity_registry.async_get(entity.entity_id)
|
||||||
assert entity_get.unique_id == "17.84197, 17.84197"
|
assert entity_get.unique_id == "59.32624, 17.84197"
|
||||||
|
|
||||||
|
|
||||||
async def test_migrate_entry_failed(
|
async def test_migrate_from_future_version(
|
||||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test migrate entry data that fails."""
|
"""Test migrate entry not possible from future version."""
|
||||||
uri = APIURL_TEMPLATE.format(
|
uri = APIURL_TEMPLATE.format(
|
||||||
TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"]
|
TEST_CONFIG_MIGRATE["longitude"], TEST_CONFIG_MIGRATE["latitude"]
|
||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG_MIGRATE)
|
entry = MockConfigEntry(domain=DOMAIN, data=TEST_CONFIG_MIGRATE, version=4)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
assert entry.version == 1
|
assert entry.version == 4
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.config_entries.ConfigEntries.async_update_entry",
|
|
||||||
return_value=False,
|
|
||||||
):
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert entry.version == 1
|
assert entry.state == ConfigEntryState.MIGRATION_ERROR
|
||||||
|
@ -49,7 +49,7 @@ async def test_setup_hass(
|
|||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -80,7 +80,7 @@ async def test_clear_night(
|
|||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response_night)
|
aioclient_mock.get(uri, text=api_response_night)
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -105,7 +105,7 @@ async def test_clear_night(
|
|||||||
|
|
||||||
async def test_properties_no_data(hass: HomeAssistant) -> None:
|
async def test_properties_no_data(hass: HomeAssistant) -> None:
|
||||||
"""Test properties when no API data available."""
|
"""Test properties when no API data available."""
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
@ -193,7 +193,7 @@ async def test_properties_unknown_symbol(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
testdata = [data, data2, data3]
|
testdata = [data, data2, data3]
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
with (
|
with (
|
||||||
@ -232,7 +232,7 @@ async def test_refresh_weather_forecast_retry(
|
|||||||
hass: HomeAssistant, error: Exception
|
hass: HomeAssistant, error: Exception
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the refresh weather forecast function."""
|
"""Test the refresh weather forecast function."""
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
|
|
||||||
@ -357,7 +357,7 @@ async def test_custom_speed_unit(
|
|||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -394,7 +394,7 @@ async def test_forecast_services(
|
|||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -458,7 +458,7 @@ async def test_forecast_services_lack_of_data(
|
|||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response_lack_data)
|
aioclient_mock.get(uri, text=api_response_lack_data)
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
@ -503,7 +503,7 @@ async def test_forecast_service(
|
|||||||
)
|
)
|
||||||
aioclient_mock.get(uri, text=api_response)
|
aioclient_mock.get(uri, text=api_response)
|
||||||
|
|
||||||
entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2)
|
entry = MockConfigEntry(domain="smhi", title="test", data=TEST_CONFIG, version=3)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user