diff --git a/.coveragerc b/.coveragerc index d0026245d03..a264bde79a2 100644 --- a/.coveragerc +++ b/.coveragerc @@ -666,7 +666,6 @@ omit = homeassistant/components/weather/darksky.py homeassistant/components/weather/metoffice.py homeassistant/components/weather/openweathermap.py - homeassistant/components/weather/yweather.py homeassistant/components/weather/zamg.py homeassistant/components/zeroconf.py homeassistant/components/zwave/util.py diff --git a/homeassistant/components/weather/yweather.py b/homeassistant/components/weather/yweather.py index 0ef0aba2d1b..20ed97ec249 100644 --- a/homeassistant/components/weather/yweather.py +++ b/homeassistant/components/weather/yweather.py @@ -125,27 +125,28 @@ class YahooWeatherWeather(WeatherEntity): @property def pressure(self): """Return the pressure.""" - return self._data.yahoo.Atmosphere['pressure'] + return round(float(self._data.yahoo.Atmosphere['pressure'])/33.8637526, + 2) @property def humidity(self): """Return the humidity.""" - return self._data.yahoo.Atmosphere['humidity'] + return int(self._data.yahoo.Atmosphere['humidity']) @property def visibility(self): """Return the visibility.""" - return self._data.yahoo.Atmosphere['visibility'] + return round(float(self._data.yahoo.Atmosphere['visibility'])/1.61, 2) @property def wind_speed(self): """Return the wind speed.""" - return self._data.yahoo.Wind['speed'] + return round(float(self._data.yahoo.Wind['speed'])/1.61, 2) @property def wind_bearing(self): """Return the wind direction.""" - return self._data.yahoo.Wind['direction'] + return int(self._data.yahoo.Wind['direction']) @property def attribution(self): diff --git a/tests/components/weather/test_yweather.py b/tests/components/weather/test_yweather.py new file mode 100644 index 00000000000..3e5eff9dae7 --- /dev/null +++ b/tests/components/weather/test_yweather.py @@ -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