Skip to content

Commit f452c5b

Browse files
authored
Add forecast subscription failure test case to nws (#115541)
1 parent f1ac33c commit f452c5b

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

tests/components/nws/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Fixtures for National Weather Service tests."""
22

3+
import asyncio
34
from unittest.mock import AsyncMock, patch
45

56
import pytest
@@ -24,6 +25,23 @@ def mock_simple_nws():
2425
yield mock_nws
2526

2627

28+
@pytest.fixture
29+
def mock_simple_nws_times_out():
30+
"""Mock pynws SimpleNWS that times out."""
31+
with patch("homeassistant.components.nws.SimpleNWS") as mock_nws:
32+
instance = mock_nws.return_value
33+
instance.set_station = AsyncMock(side_effect=asyncio.TimeoutError)
34+
instance.update_observation = AsyncMock(side_effect=asyncio.TimeoutError)
35+
instance.update_forecast = AsyncMock(side_effect=asyncio.TimeoutError)
36+
instance.update_forecast_hourly = AsyncMock(side_effect=asyncio.TimeoutError)
37+
instance.station = "ABC"
38+
instance.stations = ["ABC"]
39+
instance.observation = None
40+
instance.forecast = None
41+
instance.forecast_hourly = None
42+
yield mock_nws
43+
44+
2745
@pytest.fixture
2846
def mock_simple_nws_config():
2947
"""Mock pynws SimpleNWS with default values in config_flow."""

tests/components/nws/test_weather.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,49 @@ async def test_forecast_subscription(
476476

477477
assert forecast2 != []
478478
assert forecast2 == snapshot
479+
480+
481+
@pytest.mark.parametrize(
482+
("forecast_type", "entity_id"),
483+
[("hourly", "weather.abc")],
484+
)
485+
async def test_forecast_subscription_with_failing_coordinator(
486+
hass: HomeAssistant,
487+
hass_ws_client: WebSocketGenerator,
488+
freezer: FrozenDateTimeFactory,
489+
snapshot: SnapshotAssertion,
490+
mock_simple_nws_times_out,
491+
no_sensor,
492+
forecast_type: str,
493+
entity_id: str,
494+
) -> None:
495+
"""Test a forecast subscription when the coordinator is failing to update."""
496+
client = await hass_ws_client(hass)
497+
498+
registry = er.async_get(hass)
499+
# Pre-create the hourly entity
500+
registry.async_get_or_create(
501+
WEATHER_DOMAIN,
502+
nws.DOMAIN,
503+
"35_-75_hourly",
504+
suggested_object_id="abc_hourly",
505+
)
506+
507+
entry = MockConfigEntry(
508+
domain=nws.DOMAIN,
509+
data=NWS_CONFIG,
510+
)
511+
entry.add_to_hass(hass)
512+
513+
await hass.config_entries.async_setup(entry.entry_id)
514+
await hass.async_block_till_done()
515+
516+
await client.send_json_auto_id(
517+
{
518+
"type": "weather/subscribe_forecast",
519+
"forecast_type": forecast_type,
520+
"entity_id": entity_id,
521+
}
522+
)
523+
msg = await client.receive_json()
524+
assert not msg["success"]

0 commit comments

Comments
 (0)