1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Clean up OpenUV config flow (#17349)

* Cleaned up OpenUV config flow

* Added proper listener removal

* Added proper exception

* Unnecessary exception message

* Moved API key error to correct place

* Member-requested changes (part 1)

* Hound

* Member-requested changes (part 2)

* Cleanup

* Fixed tests
This commit is contained in:
Aaron Bach
2018-10-15 13:21:21 -06:00
committed by GitHub
parent 73197c9a6c
commit 29c2b2fe79
6 changed files with 158 additions and 125 deletions

View File

@@ -1,10 +1,12 @@
"""Define tests for the OpenUV config flow."""
from datetime import timedelta
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)
CONF_API_KEY, CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE,
CONF_SCAN_INTERVAL)
from tests.common import MockConfigEntry, mock_coro
@@ -23,7 +25,7 @@ async def test_duplicate_error(hass):
flow.hass = hass
result = await flow.async_step_user(user_input=conf)
assert result['errors'] == {'base': 'identifier_exists'}
assert result['errors'] == {CONF_LATITUDE: 'identifier_exists'}
async def test_invalid_api_key(hass):
@@ -41,7 +43,7 @@ async def test_invalid_api_key(hass):
with patch('pyopenuv.util.validate_api_key',
return_value=mock_coro(False)):
result = await flow.async_step_user(user_input=conf)
assert result['errors'] == {'base': 'invalid_api_key'}
assert result['errors'] == {CONF_API_KEY: 'invalid_api_key'}
async def test_show_form(hass):
@@ -57,25 +59,6 @@ async def test_show_form(hass):
async def test_step_import(hass):
"""Test that the import step works."""
conf = {
CONF_API_KEY: '12345abcde',
}
flow = config_flow.OpenUvFlowHandler()
flow.hass = hass
with patch('pyopenuv.util.validate_api_key',
return_value=mock_coro(True)):
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
async def test_step_user(hass):
"""Test that the user step works."""
conf = {
CONF_API_KEY: '12345abcde',
CONF_ELEVATION: 59.1234,
@@ -86,11 +69,44 @@ async def test_step_user(hass):
flow = config_flow.OpenUvFlowHandler()
flow.hass = hass
with patch('pyopenuv.util.validate_api_key',
return_value=mock_coro(True)):
result = await flow.async_step_import(import_config=conf)
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['title'] == '39.128712, -104.9812612'
assert result['data'] == {
CONF_API_KEY: '12345abcde',
CONF_ELEVATION: 59.1234,
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_SCAN_INTERVAL: 1800,
}
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,
CONF_SCAN_INTERVAL: timedelta(minutes=5)
}
flow = config_flow.OpenUvFlowHandler()
flow.hass = hass
with patch('pyopenuv.util.validate_api_key',
return_value=mock_coro(True)):
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
assert result['title'] == '39.128712, -104.9812612'
assert result['data'] == {
CONF_API_KEY: '12345abcde',
CONF_ELEVATION: 59.1234,
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
CONF_SCAN_INTERVAL: 300,
}