Skip to content

Commit

Permalink
add migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Aug 16, 2022
1 parent 6bb6d27 commit f49e5af
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
7 changes: 7 additions & 0 deletions homeassistant/components/ipma/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def __init__(self):
"""Init IpmaFlowHandler."""
self._errors = {}

async def async_step_import(self, config):
"""Import a configuration from config.yaml."""

self._async_abort_entries_match(config)
config[CONF_MODE] = "daily"
return await self.async_step_user(user_input=config)

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
self._errors = {}
Expand Down
53 changes: 52 additions & 1 deletion tests/components/ipma/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

from unittest.mock import Mock, patch

from homeassistant import config_entries
from homeassistant.components.ipma import DOMAIN, config_flow
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE, CONF_NAME
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component

from .test_weather import MockLocation

from tests.common import MockConfigEntry, mock_registry

ENTRY_CONFIG = {
CONF_NAME: "Home Town",
CONF_LATITUDE: "1",
CONF_LONGITUDE: "2",
CONF_MODE: "hourly",
}


async def test_show_config_form():
"""Test show configuration form."""
Expand Down Expand Up @@ -172,3 +181,45 @@ async def test_config_entry_migration(hass):

weather_home2 = ent_reg.async_get("weather.hometown_2")
assert weather_home2.unique_id == "0, 0, hourly"


async def test_import_flow_success(hass):
"""Test a successful import of yaml."""

with patch(
"homeassistant.components.ipma.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=ENTRY_CONFIG,
)
await hass.async_block_till_done()

assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Home Town"
assert len(mock_setup_entry.mock_calls) == 1


async def test_import_flow_already_exist(hass):
"""Test import of yaml already exist."""

MockConfigEntry(
domain=DOMAIN,
data=ENTRY_CONFIG,
).add_to_hass(hass)

with patch(
"homeassistant.components.ipma.async_setup_entry",
return_value=True,
):
result3 = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=ENTRY_CONFIG,
)
await hass.async_block_till_done()

assert result3["type"] == FlowResultType.ABORT
assert result3["reason"] == "already_configured"

0 comments on commit f49e5af

Please sign in to comment.