* 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.
185 lines
6.3 KiB
Python
185 lines
6.3 KiB
Python
"""The tests for the WUnderground platform."""
|
|
import asyncio
|
|
import aiohttp
|
|
|
|
from pytest import raises
|
|
|
|
import homeassistant.components.wunderground.sensor as wunderground
|
|
from homeassistant.const import TEMP_CELSIUS, LENGTH_INCHES, STATE_UNKNOWN
|
|
from homeassistant.exceptions import PlatformNotReady
|
|
from homeassistant.setup import async_setup_component
|
|
from tests.common import load_fixture, assert_setup_component
|
|
|
|
VALID_CONFIG_PWS = {
|
|
'platform': 'wunderground',
|
|
'api_key': 'foo',
|
|
'pws_id': 'bar',
|
|
'monitored_conditions': [
|
|
'weather', 'feelslike_c', 'alerts', 'elevation', 'location'
|
|
]
|
|
}
|
|
|
|
VALID_CONFIG = {
|
|
'platform': 'wunderground',
|
|
'api_key': 'foo',
|
|
'lang': 'EN',
|
|
'monitored_conditions': [
|
|
'weather', 'feelslike_c', 'alerts', 'elevation', 'location',
|
|
'weather_1d_metric', 'precip_1d_in'
|
|
]
|
|
}
|
|
|
|
INVALID_CONFIG = {
|
|
'platform': 'wunderground',
|
|
'api_key': 'BOB',
|
|
'pws_id': 'bar',
|
|
'lang': 'foo',
|
|
'monitored_conditions': [
|
|
'weather', 'feelslike_c', 'alerts'
|
|
]
|
|
}
|
|
|
|
URL = 'http://api.wunderground.com/api/foo/alerts/conditions/forecast/lang' \
|
|
':EN/q/32.87336,-117.22743.json'
|
|
PWS_URL = 'http://api.wunderground.com/api/foo/alerts/conditions/' \
|
|
'lang:EN/q/pws:bar.json'
|
|
INVALID_URL = 'http://api.wunderground.com/api/BOB/alerts/conditions/' \
|
|
'lang:foo/q/pws:bar.json'
|
|
|
|
|
|
@asyncio.coroutine
|
|
def test_setup(hass, aioclient_mock):
|
|
"""Test that the component is loaded."""
|
|
aioclient_mock.get(URL, text=load_fixture('wunderground-valid.json'))
|
|
|
|
with assert_setup_component(1, 'sensor'):
|
|
yield from async_setup_component(hass, 'sensor',
|
|
{'sensor': VALID_CONFIG})
|
|
|
|
|
|
@asyncio.coroutine
|
|
def test_setup_pws(hass, aioclient_mock):
|
|
"""Test that the component is loaded with PWS id."""
|
|
aioclient_mock.get(PWS_URL, text=load_fixture('wunderground-valid.json'))
|
|
|
|
with assert_setup_component(1, 'sensor'):
|
|
yield from async_setup_component(hass, 'sensor',
|
|
{'sensor': VALID_CONFIG_PWS})
|
|
|
|
|
|
@asyncio.coroutine
|
|
def test_setup_invalid(hass, aioclient_mock):
|
|
"""Test that the component is not loaded with invalid config."""
|
|
aioclient_mock.get(INVALID_URL,
|
|
text=load_fixture('wunderground-error.json'))
|
|
|
|
with assert_setup_component(0, 'sensor'):
|
|
yield from async_setup_component(hass, 'sensor',
|
|
{'sensor': INVALID_CONFIG})
|
|
|
|
|
|
@asyncio.coroutine
|
|
def test_sensor(hass, aioclient_mock):
|
|
"""Test the WUnderground sensor class and methods."""
|
|
aioclient_mock.get(URL, text=load_fixture('wunderground-valid.json'))
|
|
|
|
yield from async_setup_component(hass, 'sensor', {'sensor': VALID_CONFIG})
|
|
|
|
state = hass.states.get('sensor.pws_weather')
|
|
assert state.state == 'Clear'
|
|
assert state.name == "Weather Summary"
|
|
assert 'unit_of_measurement' not in state.attributes
|
|
assert state.attributes['entity_picture'] == \
|
|
'https://icons.wxug.com/i/c/k/clear.gif'
|
|
|
|
state = hass.states.get('sensor.pws_alerts')
|
|
assert state.state == '1'
|
|
assert state.name == 'Alerts'
|
|
assert state.attributes['Message'] == \
|
|
"This is a test alert message"
|
|
assert state.attributes['icon'] == 'mdi:alert-circle-outline'
|
|
assert 'entity_picture' not in state.attributes
|
|
|
|
state = hass.states.get('sensor.pws_location')
|
|
assert state.state == "Holly Springs, NC"
|
|
assert state.name == 'Location'
|
|
|
|
state = hass.states.get('sensor.pws_elevation')
|
|
assert state.state == '413'
|
|
assert state.name == 'Elevation'
|
|
|
|
state = hass.states.get('sensor.pws_feelslike_c')
|
|
assert state.state == '40'
|
|
assert state.name == "Feels Like"
|
|
assert 'entity_picture' not in state.attributes
|
|
assert state.attributes['unit_of_measurement'] == TEMP_CELSIUS
|
|
|
|
state = hass.states.get('sensor.pws_weather_1d_metric')
|
|
assert state.state == "Mostly Cloudy. Fog overnight."
|
|
assert state.name == 'Tuesday'
|
|
|
|
state = hass.states.get('sensor.pws_precip_1d_in')
|
|
assert state.state == '0.03'
|
|
assert state.name == "Precipitation Intensity Today"
|
|
assert state.attributes['unit_of_measurement'] == LENGTH_INCHES
|
|
|
|
|
|
@asyncio.coroutine
|
|
def test_connect_failed(hass, aioclient_mock):
|
|
"""Test the WUnderground connection error."""
|
|
aioclient_mock.get(URL, exc=aiohttp.ClientError())
|
|
with raises(PlatformNotReady):
|
|
yield from wunderground.async_setup_platform(hass, VALID_CONFIG,
|
|
lambda _: None)
|
|
|
|
|
|
@asyncio.coroutine
|
|
def test_invalid_data(hass, aioclient_mock):
|
|
"""Test the WUnderground invalid data."""
|
|
aioclient_mock.get(URL, text=load_fixture('wunderground-invalid.json'))
|
|
|
|
yield from async_setup_component(hass, 'sensor', {'sensor': VALID_CONFIG})
|
|
|
|
for condition in VALID_CONFIG['monitored_conditions']:
|
|
state = hass.states.get('sensor.pws_' + condition)
|
|
assert state.state == STATE_UNKNOWN
|
|
|
|
|
|
async def test_entity_id_with_multiple_stations(hass, aioclient_mock):
|
|
"""Test not generating duplicate entity ids with multiple stations."""
|
|
aioclient_mock.get(URL, text=load_fixture('wunderground-valid.json'))
|
|
aioclient_mock.get(PWS_URL, text=load_fixture('wunderground-valid.json'))
|
|
|
|
config = [
|
|
VALID_CONFIG,
|
|
{**VALID_CONFIG_PWS, 'entity_namespace': 'hi'}
|
|
]
|
|
await async_setup_component(hass, 'sensor', {'sensor': config})
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get('sensor.pws_weather')
|
|
assert state is not None
|
|
assert state.state == 'Clear'
|
|
|
|
state = hass.states.get('sensor.hi_pws_weather')
|
|
assert state is not None
|
|
assert state.state == 'Clear'
|
|
|
|
|
|
async def test_fails_because_of_unique_id(hass, aioclient_mock):
|
|
"""Test same config twice fails because of unique_id."""
|
|
aioclient_mock.get(URL, text=load_fixture('wunderground-valid.json'))
|
|
aioclient_mock.get(PWS_URL, text=load_fixture('wunderground-valid.json'))
|
|
|
|
config = [
|
|
VALID_CONFIG,
|
|
{**VALID_CONFIG, 'entity_namespace': 'hi'},
|
|
VALID_CONFIG_PWS
|
|
]
|
|
await async_setup_component(hass, 'sensor', {'sensor': config})
|
|
await hass.async_block_till_done()
|
|
|
|
states = hass.states.async_all()
|
|
expected = len(VALID_CONFIG['monitored_conditions']) + \
|
|
len(VALID_CONFIG_PWS['monitored_conditions'])
|
|
assert len(states) == expected
|