From f452c5b84ea35c3d7c3fcea4d156d1d8eaf44b0d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 Apr 2024 10:48:23 -1000 Subject: [PATCH] Add forecast subscription failure test case to nws (#115541) --- tests/components/nws/conftest.py | 18 +++++++++++ tests/components/nws/test_weather.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/tests/components/nws/conftest.py b/tests/components/nws/conftest.py index 7ffde0c5731..ac2c281c57b 100644 --- a/tests/components/nws/conftest.py +++ b/tests/components/nws/conftest.py @@ -1,5 +1,6 @@ """Fixtures for National Weather Service tests.""" +import asyncio from unittest.mock import AsyncMock, patch import pytest @@ -24,6 +25,23 @@ def mock_simple_nws(): yield mock_nws +@pytest.fixture +def mock_simple_nws_times_out(): + """Mock pynws SimpleNWS that times out.""" + with patch("homeassistant.components.nws.SimpleNWS") as mock_nws: + instance = mock_nws.return_value + instance.set_station = AsyncMock(side_effect=asyncio.TimeoutError) + instance.update_observation = AsyncMock(side_effect=asyncio.TimeoutError) + instance.update_forecast = AsyncMock(side_effect=asyncio.TimeoutError) + instance.update_forecast_hourly = AsyncMock(side_effect=asyncio.TimeoutError) + instance.station = "ABC" + instance.stations = ["ABC"] + instance.observation = None + instance.forecast = None + instance.forecast_hourly = None + yield mock_nws + + @pytest.fixture def mock_simple_nws_config(): """Mock pynws SimpleNWS with default values in config_flow.""" diff --git a/tests/components/nws/test_weather.py b/tests/components/nws/test_weather.py index 0fb5654d7ee..ad40b576a8a 100644 --- a/tests/components/nws/test_weather.py +++ b/tests/components/nws/test_weather.py @@ -476,3 +476,49 @@ async def test_forecast_subscription( assert forecast2 != [] assert forecast2 == snapshot + + +@pytest.mark.parametrize( + ("forecast_type", "entity_id"), + [("hourly", "weather.abc")], +) +async def test_forecast_subscription_with_failing_coordinator( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + freezer: FrozenDateTimeFactory, + snapshot: SnapshotAssertion, + mock_simple_nws_times_out, + no_sensor, + forecast_type: str, + entity_id: str, +) -> None: + """Test a forecast subscription when the coordinator is failing to update.""" + client = await hass_ws_client(hass) + + registry = er.async_get(hass) + # Pre-create the hourly entity + registry.async_get_or_create( + WEATHER_DOMAIN, + nws.DOMAIN, + "35_-75_hourly", + suggested_object_id="abc_hourly", + ) + + entry = MockConfigEntry( + domain=nws.DOMAIN, + data=NWS_CONFIG, + ) + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + await client.send_json_auto_id( + { + "type": "weather/subscribe_forecast", + "forecast_type": forecast_type, + "entity_id": entity_id, + } + ) + msg = await client.receive_json() + assert not msg["success"]