* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
165 lines
5.6 KiB
Python
165 lines
5.6 KiB
Python
"""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
|
|
"""Return raw Data."""
|
|
if self.woeid == '12345':
|
|
return json.loads('[]')
|
|
return self._data
|
|
|
|
@property
|
|
def Now(self): # pylint: disable=invalid-name
|
|
"""Return 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
|
|
"""Return atmosphere weather data."""
|
|
return self._data['query']['results']['channel']['atmosphere']
|
|
|
|
@property
|
|
def Wind(self): # pylint: disable=invalid-name
|
|
"""Return wind weather data."""
|
|
return self._data['query']['results']['channel']['wind']
|
|
|
|
@property
|
|
def Forecast(self): # pylint: disable=invalid-name
|
|
"""Return 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_entities(self, devices):
|
|
"""Mock add devices."""
|
|
for device in devices:
|
|
device.update()
|
|
self.DEVICES.append(device)
|
|
|
|
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
|
|
|
|
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."""
|
|
assert 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
|
|
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) == 0
|
|
assert 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."""
|
|
assert 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."""
|
|
assert 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."""
|
|
assert setup_component(self.hass, 'weather', {
|
|
'weather': {
|
|
'platform': 'yweather',
|
|
'woeid': '111',
|
|
}
|
|
})
|
|
|
|
state = self.hass.states.get('weather.yweather')
|
|
assert state is None
|