core/tests/components/wunderground/test_sensor.py

181 lines
6.0 KiB
Python
Raw Normal View History

2016-08-18 22:39:16 +00:00
"""The tests for the WUnderground platform."""
import aiohttp
from pytest import raises
2016-08-18 04:33:39 +00:00
Consolidate all platforms that have tests (#22109) * 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.
2019-03-19 06:07:39 +00:00
import homeassistant.components.wunderground.sensor as wunderground
from homeassistant.const import LENGTH_INCHES, STATE_UNKNOWN, TEMP_CELSIUS
from homeassistant.exceptions import PlatformNotReady
from homeassistant.setup import async_setup_component
from tests.common import assert_setup_component, load_fixture
2016-08-18 04:33:39 +00:00
VALID_CONFIG_PWS = {
2019-07-31 19:25:30 +00:00
"platform": "wunderground",
"api_key": "foo",
"pws_id": "bar",
"monitored_conditions": [
"weather",
"feelslike_c",
"alerts",
"elevation",
"location",
],
2016-08-18 04:33:39 +00:00
}
VALID_CONFIG = {
2019-07-31 19:25:30 +00:00
"platform": "wunderground",
"api_key": "foo",
"lang": "EN",
"monitored_conditions": [
"weather",
"feelslike_c",
"alerts",
"elevation",
"location",
"weather_1d_metric",
"precip_1d_in",
],
2016-08-18 04:33:39 +00:00
}
INVALID_CONFIG = {
2019-07-31 19:25:30 +00:00
"platform": "wunderground",
"api_key": "BOB",
"pws_id": "bar",
"lang": "foo",
"monitored_conditions": ["weather", "feelslike_c", "alerts"],
}
2019-07-31 19:25:30 +00:00
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"
2019-07-31 19:25:30 +00:00
INVALID_URL = (
"http://api.wunderground.com/api/BOB/alerts/conditions/lang:foo/q/pws:bar.json"
2019-07-31 19:25:30 +00:00
)
async def test_setup(hass, aioclient_mock):
"""Test that the component is loaded."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(URL, text=load_fixture("wunderground-valid.json"))
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
with assert_setup_component(1, "sensor"):
await async_setup_component(hass, "sensor", {"sensor": VALID_CONFIG})
2016-08-18 04:33:39 +00:00
2016-08-24 03:25:52 +00:00
async def test_setup_pws(hass, aioclient_mock):
"""Test that the component is loaded with PWS id."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(PWS_URL, text=load_fixture("wunderground-valid.json"))
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
with assert_setup_component(1, "sensor"):
await async_setup_component(hass, "sensor", {"sensor": VALID_CONFIG_PWS})
2016-08-18 04:33:39 +00:00
async def test_setup_invalid(hass, aioclient_mock):
"""Test that the component is not loaded with invalid config."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(INVALID_URL, text=load_fixture("wunderground-error.json"))
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
with assert_setup_component(0, "sensor"):
await async_setup_component(hass, "sensor", {"sensor": INVALID_CONFIG})
2016-08-18 04:33:39 +00:00
async def test_sensor(hass, aioclient_mock):
"""Test the WUnderground sensor class and methods."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(URL, text=load_fixture("wunderground-valid.json"))
await async_setup_component(hass, "sensor", {"sensor": VALID_CONFIG})
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.pws_weather")
assert state.state == "Clear"
assert state.name == "Weather Summary"
2019-07-31 19:25:30 +00:00
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"
2019-07-31 19:25:30 +00:00
assert state.name == "Location"
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.pws_elevation")
assert state.state == "413"
assert state.name == "Elevation"
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.pws_feelslike_c")
assert state.state == "40"
assert state.name == "Feels Like"
2019-07-31 19:25:30 +00:00
assert "entity_picture" not in state.attributes
assert state.attributes["unit_of_measurement"] == TEMP_CELSIUS
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.pws_weather_1d_metric")
assert state.state == "Mostly Cloudy. Fog overnight."
2019-07-31 19:25:30 +00:00
assert state.name == "Tuesday"
2016-08-18 04:33:39 +00:00
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.pws_precip_1d_in")
assert state.state == "0.03"
assert state.name == "Precipitation Intensity Today"
2019-07-31 19:25:30 +00:00
assert state.attributes["unit_of_measurement"] == LENGTH_INCHES
2017-01-07 00:47:25 +00:00
2016-08-18 04:33:39 +00:00
async def test_connect_failed(hass, aioclient_mock):
"""Test the WUnderground connection error."""
aioclient_mock.get(URL, exc=aiohttp.ClientError())
with raises(PlatformNotReady):
await wunderground.async_setup_platform(hass, VALID_CONFIG, lambda _: None)
2016-08-18 04:33:39 +00:00
async def test_invalid_data(hass, aioclient_mock):
"""Test the WUnderground invalid data."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(URL, text=load_fixture("wunderground-invalid.json"))
await async_setup_component(hass, "sensor", {"sensor": VALID_CONFIG})
2019-07-31 19:25:30 +00:00
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."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(URL, text=load_fixture("wunderground-valid.json"))
aioclient_mock.get(PWS_URL, text=load_fixture("wunderground-valid.json"))
2019-07-31 19:25:30 +00:00
config = [VALID_CONFIG, {**VALID_CONFIG_PWS, "entity_namespace": "hi"}]
await async_setup_component(hass, "sensor", {"sensor": config})
await hass.async_block_till_done()
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.pws_weather")
assert state is not None
2019-07-31 19:25:30 +00:00
assert state.state == "Clear"
2019-07-31 19:25:30 +00:00
state = hass.states.get("sensor.hi_pws_weather")
assert state is not None
2019-07-31 19:25:30 +00:00
assert state.state == "Clear"
async def test_fails_because_of_unique_id(hass, aioclient_mock):
"""Test same config twice fails because of unique_id."""
2019-07-31 19:25:30 +00:00
aioclient_mock.get(URL, text=load_fixture("wunderground-valid.json"))
aioclient_mock.get(PWS_URL, text=load_fixture("wunderground-valid.json"))
config = [
VALID_CONFIG,
2019-07-31 19:25:30 +00:00
{**VALID_CONFIG, "entity_namespace": "hi"},
VALID_CONFIG_PWS,
]
2019-07-31 19:25:30 +00:00
await async_setup_component(hass, "sensor", {"sensor": config})
await hass.async_block_till_done()
states = hass.states.async_all()
2019-07-31 19:25:30 +00:00
expected = len(VALID_CONFIG["monitored_conditions"]) + len(
VALID_CONFIG_PWS["monitored_conditions"]
)
assert len(states) == expected