"""Define tests for the OpenUV config flow."""
from unittest.mock import patch

from homeassistant import data_entry_flow
from homeassistant.components.openuv import DOMAIN, config_flow
from homeassistant.const import (
    CONF_API_KEY, CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE)

from tests.common import MockConfigEntry, mock_coro


async def test_duplicate_error(hass):
    """Test that errors are shown when duplicates are added."""
    conf = {
        CONF_API_KEY: '12345abcde',
        CONF_ELEVATION: 59.1234,
        CONF_LATITUDE: 39.128712,
        CONF_LONGITUDE: -104.9812612,
    }

    MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass)
    flow = config_flow.OpenUvFlowHandler()
    flow.hass = hass

    result = await flow.async_step_user(user_input=conf)
    assert result['errors'] == {'base': 'identifier_exists'}


@patch('pyopenuv.util.validate_api_key', return_value=mock_coro(False))
async def test_invalid_api_key(hass):
    """Test that an invalid API key throws an error."""
    conf = {
        CONF_API_KEY: '12345abcde',
        CONF_ELEVATION: 59.1234,
        CONF_LATITUDE: 39.128712,
        CONF_LONGITUDE: -104.9812612,
    }

    flow = config_flow.OpenUvFlowHandler()
    flow.hass = hass

    result = await flow.async_step_user(user_input=conf)
    assert result['errors'] == {'base': 'invalid_api_key'}


async def test_show_form(hass):
    """Test that the form is served with no input."""
    flow = config_flow.OpenUvFlowHandler()
    flow.hass = hass

    result = await flow.async_step_user(user_input=None)

    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'


@patch('pyopenuv.util.validate_api_key', return_value=mock_coro(True))
async def test_step_import(hass):
    """Test that the import step works."""
    conf = {
        CONF_API_KEY: '12345abcde',
    }

    flow = config_flow.OpenUvFlowHandler()
    flow.hass = hass

    result = await flow.async_step_import(import_config=conf)

    assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result['title'] == '{0}, {1}'.format(
        hass.config.latitude, hass.config.longitude)
    assert result['data'] == conf


@patch('pyopenuv.util.validate_api_key', return_value=mock_coro(True))
async def test_step_user(hass):
    """Test that the user step works."""
    conf = {
        CONF_API_KEY: '12345abcde',
        CONF_ELEVATION: 59.1234,
        CONF_LATITUDE: 39.128712,
        CONF_LONGITUDE: -104.9812612,
    }

    flow = config_flow.OpenUvFlowHandler()
    flow.hass = hass

    result = await flow.async_step_user(user_input=conf)

    assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result['title'] == '{0}, {1}'.format(
        conf[CONF_LATITUDE], conf[CONF_LONGITUDE])
    assert result['data'] == conf

@@ -849,80 +849,8 @@
 %7D%0A%0A%0A
-@patch('pyopenuv.util.validate_api_key', return_value=mock_coro(False))%0A
 asyn
@@ -1157,32 +1157,132 @@
 ow.hass = hass%0A%0A
+    with patch('pyopenuv.util.validate_api_key',%0A               return_value=mock_coro(False)):%0A    
     result = awa
@@ -1314,32 +1314,36 @@
 ser_input=conf)%0A
+    
     assert resul
@@ -1703,79 +1703,8 @@
 '%0A%0A%0A
-@patch('pyopenuv.util.validate_api_key', return_value=mock_coro(True))%0A
 asyn
@@ -1889,32 +1889,131 @@
 ow.hass = hass%0A%0A
+    with patch('pyopenuv.util.validate_api_key',%0A               return_value=mock_coro(True)):%0A    
     result = awa
@@ -2051,32 +2051,36 @@
 t_config=conf)%0A%0A
+    
     assert resul
@@ -2129,32 +2129,36 @@
 REATE_ENTRY%0A    
+  
+  
 assert result%5B't
@@ -2194,16 +2194,20 @@
         
+   
+ 
 hass.con
@@ -2243,24 +2243,28 @@
 gitude)%0A    
+   
+ 
 assert resul
@@ -2287,79 +2287,8 @@
 f%0A%0A%0A
-@patch('pyopenuv.util.validate_api_key', return_value=mock_coro(True))%0A
 asyn
@@ -2574,32 +2574,131 @@
 ow.hass = hass%0A%0A
+    with patch('pyopenuv.util.validate_api_key',%0A               return_value=mock_coro(True)):%0A    
     result = awa
@@ -2735,24 +2735,28 @@
 nput=conf)%0A%0A
+    
     assert r
@@ -2813,24 +2813,28 @@
 E_ENTRY%0A    
+   
+ 
 assert resul
@@ -2870,24 +2870,28 @@
 at(%0A        
+    
 conf%5BCONF_LA
@@ -2921,16 +2921,20 @@
 ITUDE%5D)%0A
+    
     asse

Fix openuv.config_flow unit test (#16419)