Fix nws platform setup and data update. (#34106)
* add test and fix tests for update logging. * Fix update logging. * Remove assert is True * guard against platform config and use async_write_ha_state * fix discovery info passing to platform * Improve testing for entities in init
This commit is contained in:
1 parent
ad5a396c10
commit
e6a6c3ceb6
4 files changed
+74
-38
No files matched your search
@@ -31,7 +31,12 @@ DUPLICATE_CONFIG = {
|
||||
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 await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
assert len(entity_registry.entities) == 0
|
||||
|
||||
assert DOMAIN not in hass.data
|
||||
|
||||
|
||||
@@ -39,29 +44,49 @@ 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
|
||||
with assert_setup_component(1, DOMAIN):
|
||||
assert await async_setup_component(hass, DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
assert len(entity_registry.entities) == 2
|
||||
|
||||
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
|
||||
with assert_setup_component(1, DOMAIN):
|
||||
assert await async_setup_component(hass, DOMAIN, LATLON_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
assert len(entity_registry.entities) == 2
|
||||
|
||||
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
|
||||
with assert_setup_component(1, DOMAIN):
|
||||
assert await async_setup_component(hass, DOMAIN, FULL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
assert len(entity_registry.entities) == 2
|
||||
|
||||
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 await async_setup_component(hass, DOMAIN, DUPLICATE_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
assert len(entity_registry.entities) == 2
|
||||
|
||||
assert len(hass.data[DOMAIN]) == 1
|
||||
@@ -21,16 +21,6 @@ from tests.components.nws.const import (
|
||||
NONE_OBSERVATION,
|
||||
)
|
||||
|
||||
HOURLY_CONFIG = {
|
||||
"weather": {
|
||||
"platform": "nws",
|
||||
"api_key": "x@example.com",
|
||||
"latitude": 40.0,
|
||||
"longitude": -85.0,
|
||||
"mode": "hourly",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"units,result_observation,result_forecast",
|
||||
@@ -40,7 +30,7 @@ HOURLY_CONFIG = {
|
||||
],
|
||||
)
|
||||
async def test_imperial_metric(
|
||||
hass, units, result_observation, result_forecast, mock_simple_nws
|
||||
hass, units, result_observation, result_forecast, mock_simple_nws, caplog
|
||||
):
|
||||
"""Test with imperial and metric units."""
|
||||
hass.config.units = units
|
||||
@@ -73,6 +63,9 @@ 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."""
|
||||
@@ -136,14 +129,18 @@ async def test_error_observation(hass, mock_simple_nws, caplog):
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
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)
|
||||
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
|
||||
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):
|
||||
@@ -154,14 +151,18 @@ async def test_error_forecast(hass, caplog, mock_simple_nws):
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
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)
|
||||
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
|
||||
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):
|
||||
@@ -172,11 +173,15 @@ async def test_error_forecast_hourly(hass, caplog, mock_simple_nws):
|
||||
assert await async_setup_component(hass, nws.DOMAIN, MINIMAL_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
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)
|
||||
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
|
||||
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