Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -6,65 +6,57 @@ from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
|
||||
from tests.common import (get_test_home_assistant, load_fixture,
|
||||
MockDependency)
|
||||
from tests.common import get_test_home_assistant, load_fixture, MockDependency
|
||||
|
||||
VALID_CONFIG_MINIMAL = {
|
||||
'sensor': {
|
||||
'platform': 'yweather',
|
||||
'monitored_conditions': [
|
||||
'weather',
|
||||
],
|
||||
}
|
||||
"sensor": {"platform": "yweather", "monitored_conditions": ["weather"]}
|
||||
}
|
||||
|
||||
VALID_CONFIG_ALL = {
|
||||
'sensor': {
|
||||
'platform': 'yweather',
|
||||
'monitored_conditions': [
|
||||
'weather',
|
||||
'weather_current',
|
||||
'temperature',
|
||||
'temp_min',
|
||||
'temp_max',
|
||||
'wind_speed',
|
||||
'pressure',
|
||||
'visibility',
|
||||
'humidity',
|
||||
"sensor": {
|
||||
"platform": "yweather",
|
||||
"monitored_conditions": [
|
||||
"weather",
|
||||
"weather_current",
|
||||
"temperature",
|
||||
"temp_min",
|
||||
"temp_max",
|
||||
"wind_speed",
|
||||
"pressure",
|
||||
"visibility",
|
||||
"humidity",
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
BAD_CONF_RAW = {
|
||||
'sensor': {
|
||||
'platform': 'yweather',
|
||||
'woeid': '12345',
|
||||
'monitored_conditions': [
|
||||
'weather',
|
||||
],
|
||||
"sensor": {
|
||||
"platform": "yweather",
|
||||
"woeid": "12345",
|
||||
"monitored_conditions": ["weather"],
|
||||
}
|
||||
}
|
||||
|
||||
BAD_CONF_DATA = {
|
||||
'sensor': {
|
||||
'platform': 'yweather',
|
||||
'woeid': '111',
|
||||
'monitored_conditions': [
|
||||
'weather',
|
||||
],
|
||||
"sensor": {
|
||||
"platform": "yweather",
|
||||
"woeid": "111",
|
||||
"monitored_conditions": ["weather"],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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"}}}}')
|
||||
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'
|
||||
return "23511632"
|
||||
|
||||
|
||||
def get_woeidNoneMock(lat, lon): # pylint: disable=invalid-name
|
||||
@@ -72,14 +64,14 @@ def get_woeidNoneMock(lat, lon): # pylint: disable=invalid-name
|
||||
return None
|
||||
|
||||
|
||||
class YahooWeatherMock():
|
||||
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'))
|
||||
self._data = json.loads(load_fixture("yahooweather.json"))
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
def updateWeather(self): # pylint: disable=invalid-name
|
||||
@@ -89,36 +81,36 @@ class YahooWeatherMock():
|
||||
@property
|
||||
def RawData(self): # pylint: disable=invalid-name
|
||||
"""Raw Data."""
|
||||
if self.woeid == '12345':
|
||||
return json.loads('[]')
|
||||
if self.woeid == "12345":
|
||||
return json.loads("[]")
|
||||
return self._data
|
||||
|
||||
@property
|
||||
def Units(self): # pylint: disable=invalid-name
|
||||
"""Return dict with units."""
|
||||
return self._data['query']['results']['channel']['units']
|
||||
return self._data["query"]["results"]["channel"]["units"]
|
||||
|
||||
@property
|
||||
def Now(self): # pylint: disable=invalid-name
|
||||
"""Return current weather data."""
|
||||
if self.woeid == '111':
|
||||
if self.woeid == "111":
|
||||
raise ValueError
|
||||
return self._data['query']['results']['channel']['item']['condition']
|
||||
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']
|
||||
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']
|
||||
return self._data["query"]["results"]["channel"]["wind"]
|
||||
|
||||
@property
|
||||
def Forecast(self): # pylint: disable=invalid-name
|
||||
"""Forecast data 0-5 Days."""
|
||||
return self._data['query']['results']['channel']['item']['forecast']
|
||||
return self._data["query"]["results"]["channel"]["item"]["forecast"]
|
||||
|
||||
def getWeatherImage(self, code): # pylint: disable=invalid-name
|
||||
"""Create a link to weather image from yahoo code."""
|
||||
@@ -136,114 +128,103 @@ class TestWeather(unittest.TestCase):
|
||||
"""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)
|
||||
@MockDependency("yahooweather")
|
||||
@patch("yahooweather._yql_query", new=_yql_queryMock)
|
||||
@patch("yahooweather.get_woeid", new=get_woeidMock)
|
||||
@patch("yahooweather.YahooWeather", new=YahooWeatherMock)
|
||||
def test_setup_minimal(self, mock_yahooweather):
|
||||
"""Test for minimal weather sensor config."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_MINIMAL)
|
||||
assert setup_component(self.hass, "sensor", VALID_CONFIG_MINIMAL)
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_condition')
|
||||
state = self.hass.states.get("sensor.yweather_condition")
|
||||
assert state is not None
|
||||
|
||||
assert state.state == 'Mostly Cloudy'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Condition'
|
||||
assert state.state == "Mostly Cloudy"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Condition"
|
||||
|
||||
@MockDependency('yahooweather')
|
||||
@patch('yahooweather._yql_query', new=_yql_queryMock)
|
||||
@patch('yahooweather.get_woeid', new=get_woeidMock)
|
||||
@patch('yahooweather.YahooWeather', new=YahooWeatherMock)
|
||||
@MockDependency("yahooweather")
|
||||
@patch("yahooweather._yql_query", new=_yql_queryMock)
|
||||
@patch("yahooweather.get_woeid", new=get_woeidMock)
|
||||
@patch("yahooweather.YahooWeather", new=YahooWeatherMock)
|
||||
def test_setup_all(self, mock_yahooweather):
|
||||
"""Test for all weather data attributes."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_ALL)
|
||||
assert setup_component(self.hass, "sensor", VALID_CONFIG_ALL)
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_condition')
|
||||
state = self.hass.states.get("sensor.yweather_condition")
|
||||
assert state is not None
|
||||
assert state.state == 'Mostly Cloudy'
|
||||
assert state.attributes.get('condition_code') == \
|
||||
'28'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Condition'
|
||||
assert state.state == "Mostly Cloudy"
|
||||
assert state.attributes.get("condition_code") == "28"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Condition"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_current')
|
||||
state = self.hass.states.get("sensor.yweather_current")
|
||||
assert state is not None
|
||||
assert state.state == 'Cloudy'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Current'
|
||||
assert state.state == "Cloudy"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Current"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_temperature')
|
||||
state = self.hass.states.get("sensor.yweather_temperature")
|
||||
assert state is not None
|
||||
assert state.state == '18'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Temperature'
|
||||
assert state.state == "18"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Temperature"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_temperature_max')
|
||||
state = self.hass.states.get("sensor.yweather_temperature_max")
|
||||
assert state is not None
|
||||
assert state.state == '23'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Temperature max'
|
||||
assert state.state == "23"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Temperature max"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_temperature_min')
|
||||
state = self.hass.states.get("sensor.yweather_temperature_min")
|
||||
assert state is not None
|
||||
assert state.state == '16'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Temperature min'
|
||||
assert state.state == "16"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Temperature min"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_wind_speed')
|
||||
state = self.hass.states.get("sensor.yweather_wind_speed")
|
||||
assert state is not None
|
||||
assert state.state == '3.94'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Wind speed'
|
||||
assert state.state == "3.94"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Wind speed"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_pressure')
|
||||
state = self.hass.states.get("sensor.yweather_pressure")
|
||||
assert state is not None
|
||||
assert state.state == '1000.0'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Pressure'
|
||||
assert state.state == "1000.0"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Pressure"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_visibility')
|
||||
state = self.hass.states.get("sensor.yweather_visibility")
|
||||
assert state is not None
|
||||
assert state.state == '14.23'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Visibility'
|
||||
assert state.state == "14.23"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Visibility"
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_humidity')
|
||||
state = self.hass.states.get("sensor.yweather_humidity")
|
||||
assert state is not None
|
||||
assert state.state == '71'
|
||||
assert state.attributes.get('friendly_name') == \
|
||||
'Yweather Humidity'
|
||||
assert state.state == "71"
|
||||
assert state.attributes.get("friendly_name") == "Yweather Humidity"
|
||||
|
||||
@MockDependency('yahooweather')
|
||||
@patch('yahooweather._yql_query', new=_yql_queryMock)
|
||||
@patch('yahooweather.get_woeid', new=get_woeidNoneMock)
|
||||
@patch('yahooweather.YahooWeather', new=YahooWeatherMock)
|
||||
@MockDependency("yahooweather")
|
||||
@patch("yahooweather._yql_query", new=_yql_queryMock)
|
||||
@patch("yahooweather.get_woeid", new=get_woeidNoneMock)
|
||||
@patch("yahooweather.YahooWeather", new=YahooWeatherMock)
|
||||
def test_setup_bad_woied(self, mock_yahooweather):
|
||||
"""Test for bad woeid."""
|
||||
assert setup_component(self.hass, 'sensor', VALID_CONFIG_MINIMAL)
|
||||
assert setup_component(self.hass, "sensor", VALID_CONFIG_MINIMAL)
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_condition')
|
||||
state = self.hass.states.get("sensor.yweather_condition")
|
||||
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)
|
||||
@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_raw(self, mock_yahooweather):
|
||||
"""Test for bad RawData."""
|
||||
assert setup_component(self.hass, 'sensor', BAD_CONF_RAW)
|
||||
assert setup_component(self.hass, "sensor", BAD_CONF_RAW)
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_condition')
|
||||
state = self.hass.states.get("sensor.yweather_condition")
|
||||
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)
|
||||
@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 data."""
|
||||
assert setup_component(self.hass, 'sensor', BAD_CONF_DATA)
|
||||
assert setup_component(self.hass, "sensor", BAD_CONF_DATA)
|
||||
|
||||
state = self.hass.states.get('sensor.yweather_condition')
|
||||
state = self.hass.states.get("sensor.yweather_condition")
|
||||
assert state is None
|
||||
Reference in new issue
Block a user