mirror of
https://github.com/home-assistant/core.git
synced 2025-05-04 05:59:16 +00:00
Cast return values and add unit tests for the yahoo weather component. (#10699)
* Add unittest for yahoo weather component. * Add requitements for tests. * Update requirements. * Revert "Update requirements." This reverts commit 024b5cf8bf0d1bdfe927a117f5befb53c5a5cff4. * Update requirements. * Randomize sample data. * Remove unnecessary methods. * Remove dependency and replace with MockDependency. * Add temporary fix for yahoo weather API issue.
This commit is contained in:
parent
9384e2c963
commit
ed66c21035
@ -666,7 +666,6 @@ omit =
|
|||||||
homeassistant/components/weather/darksky.py
|
homeassistant/components/weather/darksky.py
|
||||||
homeassistant/components/weather/metoffice.py
|
homeassistant/components/weather/metoffice.py
|
||||||
homeassistant/components/weather/openweathermap.py
|
homeassistant/components/weather/openweathermap.py
|
||||||
homeassistant/components/weather/yweather.py
|
|
||||||
homeassistant/components/weather/zamg.py
|
homeassistant/components/weather/zamg.py
|
||||||
homeassistant/components/zeroconf.py
|
homeassistant/components/zeroconf.py
|
||||||
homeassistant/components/zwave/util.py
|
homeassistant/components/zwave/util.py
|
||||||
|
@ -125,27 +125,28 @@ class YahooWeatherWeather(WeatherEntity):
|
|||||||
@property
|
@property
|
||||||
def pressure(self):
|
def pressure(self):
|
||||||
"""Return the pressure."""
|
"""Return the pressure."""
|
||||||
return self._data.yahoo.Atmosphere['pressure']
|
return round(float(self._data.yahoo.Atmosphere['pressure'])/33.8637526,
|
||||||
|
2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def humidity(self):
|
def humidity(self):
|
||||||
"""Return the humidity."""
|
"""Return the humidity."""
|
||||||
return self._data.yahoo.Atmosphere['humidity']
|
return int(self._data.yahoo.Atmosphere['humidity'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def visibility(self):
|
def visibility(self):
|
||||||
"""Return the visibility."""
|
"""Return the visibility."""
|
||||||
return self._data.yahoo.Atmosphere['visibility']
|
return round(float(self._data.yahoo.Atmosphere['visibility'])/1.61, 2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wind_speed(self):
|
def wind_speed(self):
|
||||||
"""Return the wind speed."""
|
"""Return the wind speed."""
|
||||||
return self._data.yahoo.Wind['speed']
|
return round(float(self._data.yahoo.Wind['speed'])/1.61, 2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wind_bearing(self):
|
def wind_bearing(self):
|
||||||
"""Return the wind direction."""
|
"""Return the wind direction."""
|
||||||
return self._data.yahoo.Wind['direction']
|
return int(self._data.yahoo.Wind['direction'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def attribution(self):
|
def attribution(self):
|
||||||
|
169
tests/components/weather/test_yweather.py
Normal file
169
tests/components/weather/test_yweather.py
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
"""The tests for the Yahoo weather component."""
|
||||||
|
import json
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
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, load_fixture,
|
||||||
|
MockDependency)
|
||||||
|
|
||||||
|
|
||||||
|
def _yql_queryMock(yql): # pylint: disable=invalid-name
|
||||||
|
"""Mock yahoo query language query."""
|
||||||
|
return ('{"query": {"count": 1, "created": "2017-11-17T13:40:47Z", '
|
||||||
|
'"lang": "en-US", "results": {"place": {"woeid": "23511632"}}}}')
|
||||||
|
|
||||||
|
|
||||||
|
def get_woeidMock(lat, lon): # pylint: disable=invalid-name
|
||||||
|
"""Mock get woeid Where On Earth Identifiers."""
|
||||||
|
return '23511632'
|
||||||
|
|
||||||
|
|
||||||
|
class YahooWeatherMock():
|
||||||
|
"""Mock class for the YahooWeather object."""
|
||||||
|
|
||||||
|
def __init__(self, woeid, temp_unit):
|
||||||
|
"""Initialize Telnet object."""
|
||||||
|
self.woeid = woeid
|
||||||
|
self.temp_unit = temp_unit
|
||||||
|
self._data = json.loads(load_fixture('yahooweather.json'))
|
||||||
|
|
||||||
|
# pylint: disable=no-self-use
|
||||||
|
def updateWeather(self): # pylint: disable=invalid-name
|
||||||
|
"""Return sample values."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def RawData(self): # pylint: disable=invalid-name
|
||||||
|
"""Raw Data."""
|
||||||
|
if self.woeid == '12345':
|
||||||
|
return json.loads('[]')
|
||||||
|
return self._data
|
||||||
|
|
||||||
|
@property
|
||||||
|
def Now(self): # pylint: disable=invalid-name
|
||||||
|
"""Current weather data."""
|
||||||
|
if self.woeid == '111':
|
||||||
|
raise ValueError
|
||||||
|
return self._data['query']['results']['channel']['item']['condition']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def Atmosphere(self): # pylint: disable=invalid-name
|
||||||
|
"""Atmosphere weather data."""
|
||||||
|
return self._data['query']['results']['channel']['atmosphere']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def Wind(self): # pylint: disable=invalid-name
|
||||||
|
"""Wind weather data."""
|
||||||
|
return self._data['query']['results']['channel']['wind']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def Forecast(self): # pylint: disable=invalid-name
|
||||||
|
"""Forecast data 0-5 Days."""
|
||||||
|
if self.woeid == '123123':
|
||||||
|
raise ValueError
|
||||||
|
return self._data['query']['results']['channel']['item']['forecast']
|
||||||
|
|
||||||
|
|
||||||
|
class TestWeather(unittest.TestCase):
|
||||||
|
"""Test the Yahoo weather component."""
|
||||||
|
|
||||||
|
DEVICES = []
|
||||||
|
|
||||||
|
def add_devices(self, devices):
|
||||||
|
"""Mock add devices."""
|
||||||
|
for device in devices:
|
||||||
|
device.update()
|
||||||
|
self.DEVICES.append(device)
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""Setup things to be run when tests are started."""
|
||||||
|
self.hass = get_test_home_assistant()
|
||||||
|
self.hass.config.units = METRIC_SYSTEM
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
"""Stop down everything that was started."""
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
@MockDependency('yahooweather')
|
||||||
|
@patch('yahooweather._yql_query', new=_yql_queryMock)
|
||||||
|
@patch('yahooweather.get_woeid', new=get_woeidMock)
|
||||||
|
@patch('yahooweather.YahooWeather', new=YahooWeatherMock)
|
||||||
|
def test_setup(self, mock_yahooweather):
|
||||||
|
"""Test for typical weather data attributes."""
|
||||||
|
self.assertTrue(
|
||||||
|
setup_component(self.hass, 'weather', {
|
||||||
|
'weather': {
|
||||||
|
'platform': 'yweather',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('weather.yweather')
|
||||||
|
assert state is not None
|
||||||
|
|
||||||
|
assert state.state == 'cloudy'
|
||||||
|
|
||||||
|
data = state.attributes
|
||||||
|
self.assertEqual(data.get(ATTR_WEATHER_TEMPERATURE), 18.0)
|
||||||
|
self.assertEqual(data.get(ATTR_WEATHER_HUMIDITY), 71)
|
||||||
|
self.assertEqual(data.get(ATTR_WEATHER_PRESSURE), 1000.0)
|
||||||
|
self.assertEqual(data.get(ATTR_WEATHER_WIND_SPEED), 3.94)
|
||||||
|
self.assertEqual(data.get(ATTR_WEATHER_WIND_BEARING), 0)
|
||||||
|
self.assertEqual(state.attributes.get('friendly_name'), 'Yweather')
|
||||||
|
|
||||||
|
@MockDependency('yahooweather')
|
||||||
|
@patch('yahooweather._yql_query', new=_yql_queryMock)
|
||||||
|
@patch('yahooweather.get_woeid', new=get_woeidMock)
|
||||||
|
@patch('yahooweather.YahooWeather', new=YahooWeatherMock)
|
||||||
|
def test_setup_no_data(self, mock_yahooweather):
|
||||||
|
"""Test for note receiving data."""
|
||||||
|
self.assertTrue(
|
||||||
|
setup_component(self.hass, 'weather', {
|
||||||
|
'weather': {
|
||||||
|
'platform': 'yweather',
|
||||||
|
'woeid': '12345',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('weather.yweather')
|
||||||
|
assert state is not None
|
||||||
|
|
||||||
|
@MockDependency('yahooweather')
|
||||||
|
@patch('yahooweather._yql_query', new=_yql_queryMock)
|
||||||
|
@patch('yahooweather.get_woeid', new=get_woeidMock)
|
||||||
|
@patch('yahooweather.YahooWeather', new=YahooWeatherMock)
|
||||||
|
def test_setup_bad_data(self, mock_yahooweather):
|
||||||
|
"""Test for bad forecast data."""
|
||||||
|
self.assertTrue(
|
||||||
|
setup_component(self.hass, 'weather', {
|
||||||
|
'weather': {
|
||||||
|
'platform': 'yweather',
|
||||||
|
'woeid': '123123',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('weather.yweather')
|
||||||
|
assert state is None
|
||||||
|
|
||||||
|
@MockDependency('yahooweather')
|
||||||
|
@patch('yahooweather._yql_query', new=_yql_queryMock)
|
||||||
|
@patch('yahooweather.get_woeid', new=get_woeidMock)
|
||||||
|
@patch('yahooweather.YahooWeather', new=YahooWeatherMock)
|
||||||
|
def test_setup_condition_error(self, mock_yahooweather):
|
||||||
|
"""Test for bad forecast data."""
|
||||||
|
self.assertTrue(
|
||||||
|
setup_component(self.hass, 'weather', {
|
||||||
|
'weather': {
|
||||||
|
'platform': 'yweather',
|
||||||
|
'woeid': '111',
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('weather.yweather')
|
||||||
|
assert state is None
|
Loading…
x
Reference in New Issue
Block a user