Set up Met during onboarding (#24622)

* Set up Met during onboarding

* Lint

* Add pyMetNo to test reqs
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-06-19 14:41:27 -07:00
1 parent 6ea92f86a5
commit f2962a0d16
12 files changed
+242 -54

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for Met.no."""
+24
View File
@@ -0,0 +1,24 @@
"""Fixtures for Met weather testing."""
from unittest.mock import patch
import pytest
from tests.common import mock_coro
@pytest.fixture
def mock_weather():
"""Mock weather data."""
with patch('metno.MetWeatherData') as mock_data:
mock_data = mock_data.return_value
mock_data.fetching_data.side_effect = lambda: mock_coro(True)
mock_data.get_current_weather.return_value = {
'condition': 'cloudy',
'temperature': 15,
'pressure': 100,
'humidity': 50,
'wind_speed': 10,
'wind_bearing': 'NE',
}
mock_data.get_forecast.return_value = {}
yield mock_data
+16
View File
@@ -131,3 +131,19 @@ async def test_flow_entry_config_entry_already_exists():
assert len(config_form.mock_calls) == 1
assert len(config_entries.mock_calls) == 1
assert len(flow._errors) == 1
async def test_onboarding_step(hass, mock_weather):
"""Test initializing via onboarding step."""
hass = Mock()
flow = config_flow.MetFlowHandler()
flow.hass = hass
result = await flow.async_step_onboarding({})
assert result['type'] == 'create_entry'
assert result['title'] == 'Home'
assert result['data'] == {
'track_home': True,
}
+52
View File
@@ -0,0 +1,52 @@
"""Test Met weather entity."""
async def test_tracking_home(hass, mock_weather):
"""Test we track home."""
await hass.config_entries.flow.async_init('met', context={
'source': 'onboarding'
})
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids('weather')) == 1
assert len(mock_weather.mock_calls) == 3
# Test we track config
await hass.config.async_update(
latitude=10,
longitude=20,
)
await hass.async_block_till_done()
assert len(mock_weather.mock_calls) == 6
entry = hass.config_entries.async_entries()[0]
await hass.config_entries.async_remove(entry.entry_id)
assert len(hass.states.async_entity_ids('weather')) == 0
async def test_not_tracking_home(hass, mock_weather):
"""Test when we not track home."""
await hass.config_entries.flow.async_init('met', context={
'source': 'user'
}, data={
'name': 'Somewhere',
'latitude': 10,
'longitude': 20,
'elevation': 0,
})
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids('weather')) == 1
assert len(mock_weather.mock_calls) == 3
# Test we do not track config
await hass.config.async_update(
latitude=10,
longitude=20,
)
await hass.async_block_till_done()
assert len(mock_weather.mock_calls) == 3
entry = hass.config_entries.async_entries()[0]
await hass.config_entries.async_remove(entry.entry_id)
assert len(hass.states.async_entity_ids('weather')) == 0
+25
View File
@@ -9,10 +9,17 @@ from homeassistant.components import onboarding
from homeassistant.components.onboarding import const, views
from tests.common import CLIENT_ID, register_auth_provider
from tests.components.met.conftest import mock_weather # noqa
from . import mock_storage
@pytest.fixture(autouse=True)
def always_mock_weather(mock_weather): # noqa
"""Mock the Met weather provider."""
pass
@pytest.fixture(autouse=True)
def auth_active(hass):
"""Ensure auth is always active."""
@@ -224,3 +231,21 @@ async def test_onboarding_integration_requires_auth(hass, hass_storage,
})
assert resp.status == 401
async def test_onboarding_core_sets_up_met(hass, hass_storage, hass_client):
"""Test finishing the core step."""
mock_storage(hass_storage, {
'done': [const.STEP_USER]
})
assert await async_setup_component(hass, 'onboarding', {})
client = await hass_client()
resp = await client.post('/api/onboarding/core_config')
assert resp.status == 200
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids('weather')) == 1