Enable config flow for Luftdaten (#17700)

* Move file to new location

* Update requirement

* Enable config flow

* Add luftdaten

* Add tests

* Update

* Add constants

* Changes according to the review comments

* Remove wrong entry from flows

* Fix dict handling

* Add callback and use OrderedDict

* Remve leftover

* Fix

* Remove await
This commit is contained in:
Fabian Affolter authored and GitHub committed 2018-11-06 14:27:52 +01:00
1 parent 7933bd7f91
commit 2e517ab6bc
13 files changed
+508 -108

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Define tests for the Luftdaten component."""
@@ -0,0 +1,114 @@
"""Define tests for the Luftdaten config flow."""
from datetime import timedelta
from unittest.mock import patch
from homeassistant import data_entry_flow
from homeassistant.components.luftdaten import DOMAIN, config_flow
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP
from tests.common import MockConfigEntry, mock_coro
async def test_duplicate_error(hass):
"""Test that errors are shown when duplicates are added."""
conf = {
CONF_SENSOR_ID: '12345abcde',
}
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass)
flow = config_flow.LuftDatenFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=conf)
assert result['errors'] == {CONF_SENSOR_ID: 'sensor_exists'}
async def test_communication_error(hass):
"""Test that no sensor is added while unable to communicate with API."""
conf = {
CONF_SENSOR_ID: '12345abcde',
}
flow = config_flow.LuftDatenFlowHandler()
flow.hass = hass
with patch('luftdaten.Luftdaten.get_data', return_value=mock_coro(None)):
result = await flow.async_step_user(user_input=conf)
assert result['errors'] == {CONF_SENSOR_ID: 'invalid_sensor'}
async def test_invalid_sensor(hass):
"""Test that an invalid sensor throws an error."""
conf = {
CONF_SENSOR_ID: '12345abcde',
}
flow = config_flow.LuftDatenFlowHandler()
flow.hass = hass
with patch('luftdaten.Luftdaten.get_data', return_value=mock_coro(False)),\
patch('luftdaten.Luftdaten.validate_sensor',
return_value=mock_coro(False)):
result = await flow.async_step_user(user_input=conf)
assert result['errors'] == {CONF_SENSOR_ID: 'invalid_sensor'}
async def test_show_form(hass):
"""Test that the form is served with no input."""
flow = config_flow.LuftDatenFlowHandler()
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'
async def test_step_import(hass):
"""Test that the import step works."""
conf = {
CONF_SENSOR_ID: '12345abcde',
CONF_SHOW_ON_MAP: False,
}
flow = config_flow.LuftDatenFlowHandler()
flow.hass = hass
with patch('luftdaten.Luftdaten.get_data', return_value=mock_coro(True)), \
patch('luftdaten.Luftdaten.validate_sensor',
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'] == '12345abcde'
assert result['data'] == {
CONF_SENSOR_ID: '12345abcde',
CONF_SHOW_ON_MAP: False,
CONF_SCAN_INTERVAL: 600,
}
async def test_step_user(hass):
"""Test that the user step works."""
conf = {
CONF_SENSOR_ID: '12345abcde',
CONF_SHOW_ON_MAP: False,
CONF_SCAN_INTERVAL: timedelta(minutes=5),
}
flow = config_flow.LuftDatenFlowHandler()
flow.hass = hass
with patch('luftdaten.Luftdaten.get_data', return_value=mock_coro(True)), \
patch('luftdaten.Luftdaten.validate_sensor',
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'] == '12345abcde'
assert result['data'] == {
CONF_SENSOR_ID: '12345abcde',
CONF_SHOW_ON_MAP: False,
CONF_SCAN_INTERVAL: 300,
}
+36
View File
@@ -0,0 +1,36 @@
"""Test the Luftdaten component setup."""
from unittest.mock import patch
from homeassistant.components import luftdaten
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID, DOMAIN
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP
from homeassistant.setup import async_setup_component
async def test_config_with_sensor_passed_to_config_entry(hass):
"""Test that configured options for a sensor are loaded."""
conf = {
CONF_SENSOR_ID: '12345abcde',
CONF_SHOW_ON_MAP: False,
CONF_SCAN_INTERVAL: 600,
}
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(luftdaten, 'configured_sensors', return_value=[]):
assert await async_setup_component(hass, DOMAIN, conf) is True
assert len(mock_config_entries.flow.mock_calls) == 0
async def test_config_already_registered_not_passed_to_config_entry(hass):
"""Test that an already registered sensor does not initiate an import."""
conf = {
CONF_SENSOR_ID: '12345abcde',
}
with patch.object(hass, 'config_entries') as mock_config_entries, \
patch.object(luftdaten, 'configured_sensors',
return_value=['12345abcde']):
assert await async_setup_component(hass, DOMAIN, conf) is True
assert len(mock_config_entries.flow.mock_calls) == 0