Convert nws integration to component configuration (#31398)
* convert nws to component configuration * add more debug logging * remove assumed state * move initialization of data into init * refactor update logic * use forecast success for checking available entities * Split unique_id into more usable pieces. use a base_unique_id for each entry. Add domain for signaling to entities. Each entity in each platform can use base_unique_id to form individual unique_id's. * Revert "move initialization of data into init" This reverts commit 09eb0220469285b10f0500f5f6def67415931a81. * add silver quality scale to manifest * unsubscribe listener in will_remove_from_hass * initialize _unsub_listener in __init__ * use async_on_remove * remove scan interval from configuration * Use better name Co-Authored-By: J. Nick Koston <nick@koston.org> Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
1 parent
f5b7deda72
commit
a3e84791c6
8 files changed
+496
-273
No files matched your search
@@ -0,0 +1,24 @@
|
||||
"""Fixtures for National Weather Service tests."""
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.common import mock_coro
|
||||
from tests.components.nws.const import DEFAULT_FORECAST, DEFAULT_OBSERVATION
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_simple_nws():
|
||||
"""Mock pynws SimpleNWS with default values."""
|
||||
with patch("homeassistant.components.nws.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast.return_value = mock_coro()
|
||||
instance.update_forecast_hourly.return_value = mock_coro()
|
||||
instance.station = "ABC"
|
||||
instance.stations = ["ABC"]
|
||||
instance.observation = DEFAULT_OBSERVATION
|
||||
instance.forecast = DEFAULT_FORECAST
|
||||
instance.forecast_hourly = DEFAULT_FORECAST
|
||||
yield mock_nws
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Helpers for interacting with pynws."""
|
||||
from homeassistant.components.nws.const import DOMAIN
|
||||
from homeassistant.components.nws.weather import ATTR_FORECAST_PRECIP_PROB
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_FORECAST_CONDITION,
|
||||
@@ -14,6 +15,7 @@ from homeassistant.components.weather import (
|
||||
ATTR_WEATHER_WIND_SPEED,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
LENGTH_KILOMETERS,
|
||||
LENGTH_METERS,
|
||||
LENGTH_MILES,
|
||||
@@ -27,6 +29,8 @@ from homeassistant.util.distance import convert as convert_distance
|
||||
from homeassistant.util.pressure import convert as convert_pressure
|
||||
from homeassistant.util.temperature import convert as convert_temperature
|
||||
|
||||
MINIMAL_CONFIG = {DOMAIN: [{CONF_API_KEY: "test"}]}
|
||||
|
||||
DEFAULT_STATIONS = ["ABC", "XYZ"]
|
||||
|
||||
DEFAULT_OBSERVATION = {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Tests for init module."""
|
||||
from homeassistant.components import nws
|
||||
from homeassistant.components.nws.const import CONF_STATION, DOMAIN
|
||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import assert_setup_component
|
||||
from tests.components.nws.const import MINIMAL_CONFIG
|
||||
|
||||
LATLON_CONFIG = {
|
||||
DOMAIN: [{CONF_API_KEY: "test", CONF_LATITUDE: 45.0, CONF_LONGITUDE: -75.0}]
|
||||
}
|
||||
FULL_CONFIG = {
|
||||
DOMAIN: [
|
||||
{
|
||||
CONF_API_KEY: "test",
|
||||
CONF_LATITUDE: 45.0,
|
||||
CONF_LONGITUDE: -75.0,
|
||||
CONF_STATION: "XYZ",
|
||||
}
|
||||
]
|
||||
}
|
||||
DUPLICATE_CONFIG = {
|
||||
DOMAIN: [
|
||||
{CONF_API_KEY: "test", CONF_LATITUDE: 45.0, CONF_LONGITUDE: -75.0},
|
||||
{CONF_API_KEY: "test", CONF_LATITUDE: 45.0, CONF_LONGITUDE: -75.0},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
async def test_no_config(hass, mock_simple_nws):
|
||||
"""Test that nws does not setup with no config."""
|
||||
with assert_setup_component(0):
|
||||
assert await async_setup_component(hass, DOMAIN, {}) is True
|
||||
assert DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_successful_minimal_config(hass, mock_simple_nws):
|
||||
"""Test that nws setup with minimal config."""
|
||||
hass.config.latitude = 40.0
|
||||
hass.config.longitude = -75.0
|
||||
with assert_setup_component(1):
|
||||
assert await async_setup_component(hass, DOMAIN, MINIMAL_CONFIG) is True
|
||||
assert DOMAIN in hass.data
|
||||
assert nws.base_unique_id(40.0, -75.0) in hass.data[DOMAIN]
|
||||
|
||||
|
||||
async def test_successful_latlon_config(hass, mock_simple_nws):
|
||||
"""Test that nws setup with latlon config."""
|
||||
with assert_setup_component(1):
|
||||
assert await async_setup_component(hass, DOMAIN, LATLON_CONFIG) is True
|
||||
assert DOMAIN in hass.data
|
||||
assert nws.base_unique_id(45.0, -75.0) in hass.data[DOMAIN]
|
||||
|
||||
|
||||
async def test_successful_full_config(hass, mock_simple_nws):
|
||||
"""Test that nws setup with full config."""
|
||||
with assert_setup_component(1):
|
||||
assert await async_setup_component(hass, DOMAIN, FULL_CONFIG) is True
|
||||
assert DOMAIN in hass.data
|
||||
assert nws.base_unique_id(45.0, -75.0) in hass.data[DOMAIN]
|
||||
|
||||
|
||||
async def test_unsuccessful_duplicate_config(hass, mock_simple_nws):
|
||||
"""Test that nws setup with duplicate config."""
|
||||
assert await async_setup_component(hass, DOMAIN, DUPLICATE_CONFIG) is True
|
||||
assert len(hass.data[DOMAIN]) == 1
|
||||
@@ -1,35 +1,26 @@
|
||||
"""Tests for the NWS weather component."""
|
||||
from unittest.mock import patch
|
||||
from datetime import timedelta
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import nws
|
||||
from homeassistant.components.weather import ATTR_FORECAST
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
|
||||
|
||||
from .const import (
|
||||
DEFAULT_FORECAST,
|
||||
DEFAULT_OBSERVATION,
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.nws.const import (
|
||||
EXPECTED_FORECAST_IMPERIAL,
|
||||
EXPECTED_FORECAST_METRIC,
|
||||
EXPECTED_OBSERVATION_IMPERIAL,
|
||||
EXPECTED_OBSERVATION_METRIC,
|
||||
MINIMAL_CONFIG,
|
||||
NONE_FORECAST,
|
||||
NONE_OBSERVATION,
|
||||
)
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
MINIMAL_CONFIG = {
|
||||
"weather": {
|
||||
"platform": "nws",
|
||||
"api_key": "x@example.com",
|
||||
"latitude": 40.0,
|
||||
"longitude": -85.0,
|
||||
}
|
||||
}
|
||||
|
||||
HOURLY_CONFIG = {
|
||||
"weather": {
|
||||
"platform": "nws",
|
||||
@@ -48,21 +39,29 @@ HOURLY_CONFIG = {
|
||||
(METRIC_SYSTEM, EXPECTED_OBSERVATION_METRIC, EXPECTED_FORECAST_METRIC),
|
||||
],
|
||||
)
|
||||
async def test_imperial_metric(hass, units, result_observation, result_forecast):
|
||||
async def test_imperial_metric(
|
||||
hass, units, result_observation, result_forecast, mock_simple_nws
|
||||
):
|
||||
"""Test with imperial and metric units."""
|
||||
hass.config.units = units
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast.return_value = mock_coro()
|
||||
instance.observation = DEFAULT_OBSERVATION
|
||||
instance.forecast = DEFAULT_FORECAST
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await async_setup_component(hass, "weather", MINIMAL_CONFIG)
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
data = state.attributes
|
||||
for key, value in result_observation.items():
|
||||
assert data.get(key) == value
|
||||
|
||||
forecast = data.get(ATTR_FORECAST)
|
||||
for key, value in result_forecast.items():
|
||||
assert forecast[0].get(key) == value
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
|
||||
state = hass.states.get("weather.abc")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
@@ -75,50 +74,17 @@ async def test_imperial_metric(hass, units, result_observation, result_forecast)
|
||||
assert forecast[0].get(key) == value
|
||||
|
||||
|
||||
async def test_hourly(hass):
|
||||
"""Test with hourly option."""
|
||||
hass.config.units = IMPERIAL_SYSTEM
|
||||
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast_hourly.return_value = mock_coro()
|
||||
instance.observation = DEFAULT_OBSERVATION
|
||||
instance.forecast_hourly = DEFAULT_FORECAST
|
||||
|
||||
await async_setup_component(hass, "weather", HOURLY_CONFIG)
|
||||
|
||||
state = hass.states.get("weather.abc")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
data = state.attributes
|
||||
for key, value in EXPECTED_OBSERVATION_IMPERIAL.items():
|
||||
assert data.get(key) == value
|
||||
|
||||
forecast = data.get(ATTR_FORECAST)
|
||||
for key, value in EXPECTED_FORECAST_IMPERIAL.items():
|
||||
assert forecast[0].get(key) == value
|
||||
|
||||
|
||||
async def test_none_values(hass):
|
||||
async def test_none_values(hass, mock_simple_nws):
|
||||
"""Test with none values in observation and forecast dicts."""
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast.return_value = mock_coro()
|
||||
instance.observation = NONE_OBSERVATION
|
||||
instance.forecast = NONE_FORECAST
|
||||
await async_setup_component(hass, "weather", MINIMAL_CONFIG)
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.observation = NONE_OBSERVATION
|
||||
instance.forecast = NONE_FORECAST
|
||||
|
||||
state = hass.states.get("weather.abc")
|
||||
assert state
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state.state == "unknown"
|
||||
|
||||
data = state.attributes
|
||||
for key in EXPECTED_OBSERVATION_IMPERIAL:
|
||||
assert data.get(key) is None
|
||||
@@ -128,19 +94,16 @@ async def test_none_values(hass):
|
||||
assert forecast[0].get(key) is None
|
||||
|
||||
|
||||
async def test_none(hass):
|
||||
async def test_none(hass, mock_simple_nws):
|
||||
"""Test with None as observation and forecast."""
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast.return_value = mock_coro()
|
||||
instance.observation = None
|
||||
instance.forecast = None
|
||||
await async_setup_component(hass, "weather", MINIMAL_CONFIG)
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.observation = None
|
||||
instance.forecast = None
|
||||
|
||||
state = hass.states.get("weather.abc")
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
|
||||
@@ -152,46 +115,68 @@ async def test_none(hass):
|
||||
assert forecast is None
|
||||
|
||||
|
||||
async def test_error_station(hass):
|
||||
async def test_error_station(hass, mock_simple_nws):
|
||||
"""Test error in setting station."""
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.side_effect = aiohttp.ClientError
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast.return_value = mock_coro()
|
||||
instance.observation = None
|
||||
instance.forecast = None
|
||||
await async_setup_component(hass, "weather", MINIMAL_CONFIG)
|
||||
|
||||
state = hass.states.get("weather.abc")
|
||||
assert state is None
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.set_station.side_effect = aiohttp.ClientError
|
||||
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG) is True
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("weather.abc_hourly") is None
|
||||
assert hass.states.get("weather.abc_daynight") is None
|
||||
|
||||
|
||||
async def test_error_observation(hass, caplog):
|
||||
async def test_error_observation(hass, mock_simple_nws, caplog):
|
||||
"""Test error during update observation."""
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.side_effect = aiohttp.ClientError
|
||||
instance.update_forecast.return_value = mock_coro()
|
||||
instance.observation = None
|
||||
instance.forecast = None
|
||||
await async_setup_component(hass, "weather", MINIMAL_CONFIG)
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_observation.side_effect = aiohttp.ClientError
|
||||
|
||||
assert "Error updating observation from station ABC" in caplog.text
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
instance.update_observation.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
async_fire_time_changed(hass, future_time)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Error updating observation" in caplog.text
|
||||
assert "Success updating observation" in caplog.text
|
||||
|
||||
|
||||
async def test_error_forecast(hass, caplog):
|
||||
async def test_error_forecast(hass, caplog, mock_simple_nws):
|
||||
"""Test error during update forecast."""
|
||||
with patch("homeassistant.components.nws.weather.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.station = "ABC"
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.update_observation.return_value = mock_coro()
|
||||
instance.update_forecast.side_effect = aiohttp.ClientError
|
||||
instance.observation = None
|
||||
instance.forecast = None
|
||||
await async_setup_component(hass, "weather", MINIMAL_CONFIG)
|
||||
assert "Error updating forecast from station ABC" in caplog.text
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_forecast.side_effect = aiohttp.ClientError
|
||||
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
instance.update_forecast.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
async_fire_time_changed(hass, future_time)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Error updating forecast" in caplog.text
|
||||
assert "Success updating forecast" in caplog.text
|
||||
|
||||
|
||||
async def test_error_forecast_hourly(hass, caplog, mock_simple_nws):
|
||||
"""Test error during update forecast hourly."""
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_forecast_hourly.side_effect = aiohttp.ClientError
|
||||
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
instance.update_forecast_hourly.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
async_fire_time_changed(hass, future_time)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Error updating forecast_hourly" in caplog.text
|
||||
assert "Success updating forecast_hourly" in caplog.text
|
||||
Reference in new issue
Block a user