Smhi add reconfigure step to config flow (#114044)

* Add reconfigure step to SMHI

* Check location

* Add test

* Add test entity and device
This commit is contained in:
G Johansson
2024-03-23 11:01:59 +01:00
committed by GitHub
parent 900e0c07bf
commit ebe6c35b4c
4 changed files with 171 additions and 3 deletions

View File

@@ -8,9 +8,11 @@ from smhi.smhi_lib import SmhiForecastException
from homeassistant import config_entries
from homeassistant.components.smhi.const import DOMAIN
from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
@@ -178,3 +180,95 @@ async def test_form_unique_id_exist(hass: HomeAssistant) -> None:
assert result2["type"] == FlowResultType.ABORT
assert result2["reason"] == "already_configured"
async def test_reconfigure_flow(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test re-configuration flow."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Home",
unique_id="57.2898-13.6304",
data={"location": {"latitude": 57.2898, "longitude": 13.6304}, "name": "Home"},
version=2,
)
entry.add_to_hass(hass)
entity = entity_registry.async_get_or_create(
WEATHER_DOMAIN, DOMAIN, "57.2898, 13.6304"
)
device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, "57.2898, 13.6304")},
manufacturer="SMHI",
model="v2",
name=entry.title,
)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
)
assert result["type"] == FlowResultType.FORM
with patch(
"homeassistant.components.smhi.config_flow.Smhi.async_get_forecast",
side_effect=SmhiForecastException,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_LOCATION: {
CONF_LATITUDE: 0.0,
CONF_LONGITUDE: 0.0,
}
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["errors"] == {"base": "wrong_location"}
with patch(
"homeassistant.components.smhi.config_flow.Smhi.async_get_forecast",
return_value={"test": "something", "test2": "something else"},
), patch(
"homeassistant.components.smhi.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_LOCATION: {
CONF_LATITUDE: 58.2898,
CONF_LONGITUDE: 14.6304,
}
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
entry = hass.config_entries.async_get_entry(entry.entry_id)
assert entry.title == "Home"
assert entry.unique_id == "58.2898-14.6304"
assert entry.data == {
"location": {
"latitude": 58.2898,
"longitude": 14.6304,
},
"name": "Home",
}
entity = entity_registry.async_get(entity.entity_id)
assert entity
assert entity.unique_id == "58.2898, 14.6304"
device = device_registry.async_get(device.id)
assert device
assert device.identifiers == {(DOMAIN, "58.2898, 14.6304")}
assert len(mock_setup_entry.mock_calls) == 1