Use DataUpdateCoordinator in NWS (#34372)
This commit is contained in:
1 parent
01599d44f5
commit
64cd38d96f
7 files changed
+103
-169
No files matched your search
@@ -22,3 +22,14 @@ def mock_simple_nws():
|
||||
instance.forecast = DEFAULT_FORECAST
|
||||
instance.forecast_hourly = DEFAULT_FORECAST
|
||||
yield mock_nws
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_simple_nws_config():
|
||||
"""Mock pynws SimpleNWS with default values in config_flow."""
|
||||
with patch("homeassistant.components.nws.config_flow.SimpleNWS") as mock_nws:
|
||||
instance = mock_nws.return_value
|
||||
instance.set_station.return_value = mock_coro()
|
||||
instance.station = "ABC"
|
||||
instance.stations = ["ABC"]
|
||||
yield mock_nws
|
||||
@@ -6,7 +6,7 @@ from homeassistant import config_entries, setup
|
||||
from homeassistant.components.nws.const import DOMAIN
|
||||
|
||||
|
||||
async def test_form(hass, mock_simple_nws):
|
||||
async def test_form(hass, mock_simple_nws_config):
|
||||
"""Test we get the form."""
|
||||
hass.config.latitude = 35
|
||||
hass.config.longitude = -90
|
||||
@@ -40,9 +40,9 @@ async def test_form(hass, mock_simple_nws):
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass, mock_simple_nws):
|
||||
async def test_form_cannot_connect(hass, mock_simple_nws_config):
|
||||
"""Test we handle cannot connect error."""
|
||||
mock_instance = mock_simple_nws.return_value
|
||||
mock_instance = mock_simple_nws_config.return_value
|
||||
mock_instance.set_station.side_effect = aiohttp.ClientError
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -57,9 +57,9 @@ async def test_form_cannot_connect(hass, mock_simple_nws):
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass, mock_simple_nws):
|
||||
async def test_form_unknown_error(hass, mock_simple_nws_config):
|
||||
"""Test we handle unknown error."""
|
||||
mock_instance = mock_simple_nws.return_value
|
||||
mock_instance = mock_simple_nws_config.return_value
|
||||
mock_instance.set_station.side_effect = ValueError
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -74,7 +74,7 @@ async def test_form_unknown_error(hass, mock_simple_nws):
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_form_already_configured(hass, mock_simple_nws):
|
||||
async def test_form_already_configured(hass, mock_simple_nws_config):
|
||||
"""Test we handle duplicate entries."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
|
||||
@@ -29,7 +29,7 @@ from tests.components.nws.const import (
|
||||
],
|
||||
)
|
||||
async def test_imperial_metric(
|
||||
hass, units, result_observation, result_forecast, mock_simple_nws, caplog
|
||||
hass, units, result_observation, result_forecast, mock_simple_nws
|
||||
):
|
||||
"""Test with imperial and metric units."""
|
||||
hass.config.units = units
|
||||
@@ -64,9 +64,6 @@ async def test_imperial_metric(
|
||||
for key, value in result_forecast.items():
|
||||
assert forecast[0].get(key) == value
|
||||
|
||||
assert "Error updating observation" not in caplog.text
|
||||
assert "Success updating observation" not in caplog.text
|
||||
|
||||
|
||||
async def test_none_values(hass, mock_simple_nws):
|
||||
"""Test with none values in observation and forecast dicts."""
|
||||
@@ -128,7 +125,7 @@ async def test_error_station(hass, mock_simple_nws):
|
||||
assert hass.states.get("weather.abc_daynight") is None
|
||||
|
||||
|
||||
async def test_error_observation(hass, mock_simple_nws, caplog):
|
||||
async def test_error_observation(hass, mock_simple_nws):
|
||||
"""Test error during update observation."""
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_observation.side_effect = aiohttp.ClientError
|
||||
@@ -148,10 +145,6 @@ async def test_error_observation(hass, mock_simple_nws, caplog):
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
|
||||
assert "Error updating observation for station ABC" in caplog.text
|
||||
assert "Success updating observation for station ABC" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.update_observation.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
@@ -168,11 +161,8 @@ async def test_error_observation(hass, mock_simple_nws, caplog):
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
assert "Error updating observation for station ABC" not in caplog.text
|
||||
assert "Success updating observation for station ABC" in caplog.text
|
||||
|
||||
|
||||
async def test_error_forecast(hass, caplog, mock_simple_nws):
|
||||
async def test_error_forecast(hass, mock_simple_nws):
|
||||
"""Test error during update forecast."""
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_forecast.side_effect = aiohttp.ClientError
|
||||
@@ -188,10 +178,6 @@ async def test_error_forecast(hass, caplog, mock_simple_nws):
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
|
||||
assert "Error updating forecast for station ABC" in caplog.text
|
||||
assert "Success updating forecast for station ABC" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.update_forecast.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
@@ -204,11 +190,8 @@ async def test_error_forecast(hass, caplog, mock_simple_nws):
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
assert "Error updating forecast for station ABC" not in caplog.text
|
||||
assert "Success updating forecast for station ABC" in caplog.text
|
||||
|
||||
|
||||
async def test_error_forecast_hourly(hass, caplog, mock_simple_nws):
|
||||
async def test_error_forecast_hourly(hass, mock_simple_nws):
|
||||
"""Test error during update forecast hourly."""
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_forecast_hourly.side_effect = aiohttp.ClientError
|
||||
@@ -224,10 +207,6 @@ async def test_error_forecast_hourly(hass, caplog, mock_simple_nws):
|
||||
|
||||
instance.update_forecast_hourly.assert_called_once()
|
||||
|
||||
assert "Error updating forecast_hourly for station ABC" in caplog.text
|
||||
assert "Success updating forecast_hourly for station ABC" not in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
instance.update_forecast_hourly.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
@@ -239,6 +218,3 @@ async def test_error_forecast_hourly(hass, caplog, mock_simple_nws):
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
assert "Error updating forecast_hourly for station ABC" not in caplog.text
|
||||
assert "Success updating forecast_hourly for station ABC" in caplog.text
|
||||
Reference in new issue
Block a user