Move weather.ipma into a component (#20706)

* initial version

* works

* lint

* move

* hound

* fix formatting

* update

* add extra features

* houmd

* docstring

* fix tests

* lint

* update requirements_all.txt

* new tests

* lint

* update CODEOWNERS

* MockDependency pyipma

* hound

* bump pyipma version

* add config_flow tests

* lint

* improve test coverage

* fix test

* address comments by @MartinHjelmare

* remove device_info

* hound

* stale comment

* Add deprecation warning

* address comments

* lint
This commit is contained in:
Diogo Gomes authored and Martin Hjelmare committed 2019-02-08 10:55:58 +01:00
1 parent 1e95719436
commit 55d1d3d8ae
13 files changed
+408 -90

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the IPMA component."""
+118
View File
@@ -0,0 +1,118 @@
"""Tests for IPMA config flow."""
from unittest.mock import Mock, patch
from tests.common import mock_coro
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.components.ipma import config_flow
async def test_show_config_form():
"""Test show configuration form."""
hass = Mock()
flow = config_flow.IpmaFlowHandler()
flow.hass = hass
result = await flow._show_config_form()
assert result['type'] == 'form'
assert result['step_id'] == 'user'
async def test_show_config_form_default_values():
"""Test show configuration form."""
hass = Mock()
flow = config_flow.IpmaFlowHandler()
flow.hass = hass
result = await flow._show_config_form(
name="test", latitude='0', longitude='0')
assert result['type'] == 'form'
assert result['step_id'] == 'user'
async def test_flow_with_home_location(hass):
"""Test config flow .
Tests the flow when a default location is configured
then it should return a form with default values
"""
flow = config_flow.IpmaFlowHandler()
flow.hass = hass
hass.config.location_name = 'Home'
hass.config.latitude = 1
hass.config.longitude = 1
result = await flow.async_step_user()
assert result['type'] == 'form'
assert result['step_id'] == 'user'
async def test_flow_show_form():
"""Test show form scenarios first time.
Test when the form should show when no configurations exists
"""
hass = Mock()
flow = config_flow.IpmaFlowHandler()
flow.hass = hass
with \
patch.object(flow, '_show_config_form',
return_value=mock_coro()) as config_form:
await flow.async_step_user()
assert len(config_form.mock_calls) == 1
async def test_flow_entry_created_from_user_input():
"""Test that create data from user input.
Test when the form should show when no configurations exists
"""
hass = Mock()
flow = config_flow.IpmaFlowHandler()
flow.hass = hass
test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}
# Test that entry created when user_input name not exists
with \
patch.object(flow, '_show_config_form',
return_value=mock_coro()) as config_form,\
patch.object(flow.hass.config_entries, 'async_entries',
return_value=mock_coro()) as config_entries:
result = await flow.async_step_user(user_input=test_data)
assert result['type'] == 'create_entry'
assert result['data'] == test_data
assert len(config_entries.mock_calls) == 1
assert not config_form.mock_calls
async def test_flow_entry_config_entry_already_exists():
"""Test that create data from user input and config_entry already exists.
Test when the form should show when user puts existing name
in the config gui. Then the form should show with error
"""
hass = Mock()
flow = config_flow.IpmaFlowHandler()
flow.hass = hass
test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}
# Test that entry created when user_input name not exists
with \
patch.object(flow, '_show_config_form',
return_value=mock_coro()) as config_form,\
patch.object(flow.hass.config_entries, 'async_entries',
return_value={'home': test_data}) as config_entries:
await flow.async_step_user(user_input=test_data)
assert len(config_form.mock_calls) == 1
assert len(config_entries.mock_calls) == 1
assert len(flow._errors) == 1
+103
View File
@@ -0,0 +1,103 @@
"""The tests for the IPMA weather component."""
from unittest.mock import patch
from collections import namedtuple
from homeassistant.components import weather
from homeassistant.components.weather import (
ATTR_WEATHER_HUMIDITY, ATTR_WEATHER_PRESSURE, ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_WIND_BEARING, ATTR_WEATHER_WIND_SPEED,
DOMAIN as WEATHER_DOMAIN)
from tests.common import MockConfigEntry, mock_coro
from homeassistant.setup import async_setup_component
TEST_CONFIG = {
"name": "HomeTown",
"latitude": "40.00",
"longitude": "-8.00",
}
class MockStation():
"""Mock Station from pyipma."""
async def observation(self):
"""Mock Observation."""
Observation = namedtuple('Observation', ['temperature', 'humidity',
'windspeed', 'winddirection',
'precipitation', 'pressure',
'description'])
return Observation(18, 71.0, 3.94, 'NW', 0, 1000.0, '---')
async def forecast(self):
"""Mock Forecast."""
Forecast = namedtuple('Forecast', ['precipitaProb', 'tMin', 'tMax',
'predWindDir', 'idWeatherType',
'classWindSpeed', 'longitude',
'forecastDate', 'classPrecInt',
'latitude', 'description'])
return [Forecast(73.0, 13.7, 18.7, 'NW', 6, 2, -8.64,
'2018-05-31', 2, 40.61,
'Aguaceiros, com vento Moderado de Noroeste')]
@property
def local(self):
"""Mock location."""
return "HomeTown"
@property
def latitude(self):
"""Mock latitude."""
return 0
@property
def longitude(self):
"""Mock longitude."""
return 0
async def test_setup_configuration(hass):
"""Test for successfully setting up the IPMA platform."""
with patch('homeassistant.components.ipma.weather.async_get_station',
return_value=mock_coro(MockStation())):
assert await async_setup_component(hass, weather.DOMAIN, {
'weather': {
'name': 'HomeTown',
'platform': 'ipma',
}
})
await hass.async_block_till_done()
state = hass.states.get('weather.hometown')
assert state.state == 'rainy'
data = state.attributes
assert data.get(ATTR_WEATHER_TEMPERATURE) == 18.0
assert data.get(ATTR_WEATHER_HUMIDITY) == 71
assert data.get(ATTR_WEATHER_PRESSURE) == 1000.0
assert data.get(ATTR_WEATHER_WIND_SPEED) == 3.94
assert data.get(ATTR_WEATHER_WIND_BEARING) == 'NW'
assert state.attributes.get('friendly_name') == 'HomeTown'
async def test_setup_config_flow(hass):
"""Test for successfully setting up the IPMA platform."""
with patch('homeassistant.components.ipma.weather.async_get_station',
return_value=mock_coro(MockStation())):
entry = MockConfigEntry(domain='ipma', data=TEST_CONFIG)
await hass.config_entries.async_forward_entry_setup(
entry, WEATHER_DOMAIN)
await hass.async_block_till_done()
state = hass.states.get('weather.hometown')
assert state.state == 'rainy'
data = state.attributes
assert data.get(ATTR_WEATHER_TEMPERATURE) == 18.0
assert data.get(ATTR_WEATHER_HUMIDITY) == 71
assert data.get(ATTR_WEATHER_PRESSURE) == 1000.0
assert data.get(ATTR_WEATHER_WIND_SPEED) == 3.94
assert data.get(ATTR_WEATHER_WIND_BEARING) == 'NW'
assert state.attributes.get('friendly_name') == 'HomeTown'
-85
View File
@@ -1,85 +0,0 @@
"""The tests for the IPMA weather component."""
import unittest
from unittest.mock import patch
from collections import namedtuple
from homeassistant.components import weather
from homeassistant.components.weather import (
ATTR_WEATHER_HUMIDITY, ATTR_WEATHER_PRESSURE, ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_WIND_BEARING, ATTR_WEATHER_WIND_SPEED)
from homeassistant.util.unit_system import METRIC_SYSTEM
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant, MockDependency
class MockStation():
"""Mock Station from pyipma."""
@classmethod
async def get(cls, websession, lat, lon):
"""Mock Factory."""
return MockStation()
async def observation(self):
"""Mock Observation."""
Observation = namedtuple('Observation', ['temperature', 'humidity',
'windspeed', 'winddirection',
'precipitation', 'pressure',
'description'])
return Observation(18, 71.0, 3.94, 'NW', 0, 1000.0, '---')
async def forecast(self):
"""Mock Forecast."""
Forecast = namedtuple('Forecast', ['precipitaProb', 'tMin', 'tMax',
'predWindDir', 'idWeatherType',
'classWindSpeed', 'longitude',
'forecastDate', 'classPrecInt',
'latitude', 'description'])
return [Forecast(73.0, 13.7, 18.7, 'NW', 6, 2, -8.64,
'2018-05-31', 2, 40.61,
'Aguaceiros, com vento Moderado de Noroeste')]
@property
def local(self):
"""Mock location."""
return "HomeTown"
class TestIPMA(unittest.TestCase):
"""Test the IPMA weather component."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.hass.config.units = METRIC_SYSTEM
self.lat = self.hass.config.latitude = 40.00
self.lon = self.hass.config.longitude = -8.00
def tearDown(self):
"""Stop down everything that was started."""
self.hass.stop()
@MockDependency("pyipma")
@patch("pyipma.Station", new=MockStation)
def test_setup(self, mock_pyipma):
"""Test for successfully setting up the IPMA platform."""
assert setup_component(self.hass, weather.DOMAIN, {
'weather': {
'name': 'HomeTown',
'platform': 'ipma',
}
})
state = self.hass.states.get('weather.hometown')
assert state.state == 'rainy'
data = state.attributes
assert data.get(ATTR_WEATHER_TEMPERATURE) == 18.0
assert data.get(ATTR_WEATHER_HUMIDITY) == 71
assert data.get(ATTR_WEATHER_PRESSURE) == 1000.0
assert data.get(ATTR_WEATHER_WIND_SPEED) == 3.94
assert data.get(ATTR_WEATHER_WIND_BEARING) == 'NW'
assert state.attributes.get('friendly_name') == 'HomeTown'