async def test_properties_no_data(hass: HomeAssistant) -> None:
    """Test properties when no API data available."""
    entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG)
    entry.add_to_hass(hass)

    with patch(
            "homeassistant.components.smhi.weather.Smhi.async_get_forecast",
            side_effect=SmhiForecastException("boom"),
    ):
        await hass.config_entries.async_setup(entry.entry_id)
        await hass.async_block_till_done()

    state = hass.states.get(ENTITY_ID)

    assert state
    assert state.name == "test"
    assert state.state == STATE_UNKNOWN
    assert state.attributes[
        ATTR_ATTRIBUTION] == "Swedish weather institute (SMHI)"
    assert ATTR_WEATHER_HUMIDITY not in state.attributes
    assert ATTR_WEATHER_PRESSURE not in state.attributes
    assert ATTR_WEATHER_TEMPERATURE not in state.attributes
    assert ATTR_WEATHER_VISIBILITY not in state.attributes
    assert ATTR_WEATHER_WIND_SPEED not in state.attributes
    assert ATTR_WEATHER_WIND_BEARING not in state.attributes
    assert ATTR_FORECAST not in state.attributes
    assert ATTR_SMHI_CLOUDINESS not in state.attributes
    assert ATTR_SMHI_THUNDER_PROBABILITY not in state.attributes
    assert ATTR_SMHI_WIND_GUST_SPEED not in state.attributes
Пример #2
0
async def test_refresh_weather_forecast_exception() -> None:
    """Test any exception."""
    from smhi.smhi_lib import SmhiForecastException

    hass = Mock()
    weather = weather_smhi.SmhiWeather("name", "17.0022", "62.0022")
    weather.hass = hass

    with patch.object(hass.helpers.event,
                      "async_call_later") as call_later, patch.object(
                          weather_smhi, "async_timeout"), patch.object(
                              weather_smhi.SmhiWeather,
                              "retry_update"), patch.object(
                                  weather_smhi.SmhiWeather,
                                  "get_weather_forecast",
                                  side_effect=SmhiForecastException(),
                              ):

        hass.async_add_job = Mock()
        call_later = hass.helpers.event.async_call_later

        await weather.async_update()
        assert len(call_later.mock_calls) == 1
        # Assert we are going to wait RETRY_TIMEOUT seconds
        assert call_later.mock_calls[0][1][0] == weather_smhi.RETRY_TIMEOUT
Пример #3
0
async def test_check_location_faulty() -> None:
    """Test check location when faulty input."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    with patch.object(
        config_flow.aiohttp_client, "async_get_clientsession"
    ), patch.object(SmhiApi, "async_get_forecast", side_effect=SmhiForecastException()):

        assert await flow._check_location("58", "17") is False
Пример #4
0
async def test_refresh_weather_forecast_exceeds_retries(hass) -> None:
    """Test the refresh weather forecast function."""
    from smhi.smhi_lib import SmhiForecastException

    with \
        patch.object(hass.helpers.event, 'async_call_later') as call_later, \
        patch.object(weather_smhi.SmhiWeather, 'get_weather_forecast',
                     side_effect=SmhiForecastException()):

        weather = weather_smhi.SmhiWeather('name', '17.0022', '62.0022')
        weather.hass = hass
        weather._fail_count = 2

        await weather.async_update()
        assert weather._forecasts is None
        assert not call_later.mock_calls
Пример #5
0
async def test_refresh_weather_forecast_exceeds_retries(hass) -> None:
    """Test the refresh weather forecast function."""

    with patch.object(hass.helpers.event,
                      "async_call_later") as call_later, patch.object(
                          weather_smhi.SmhiWeather,
                          "get_weather_forecast",
                          side_effect=SmhiForecastException(),
                      ):

        weather = weather_smhi.SmhiWeather("name", "17.0022", "62.0022")
        weather.hass = hass
        weather._fail_count = 2

        await weather.async_update()
        assert weather._forecasts is None
        assert not call_later.mock_calls
Пример #6
0
async def test_refresh_weather_forecast_exception() -> None:
    """Test any exception."""
    from smhi.smhi_lib import SmhiForecastException

    hass = Mock()
    weather = weather_smhi.SmhiWeather('name', '17.0022', '62.0022')
    weather.hass = hass

    with \
        patch.object(hass.helpers.event, 'async_call_later') as call_later, \
        patch.object(weather_smhi, 'async_timeout'), \
        patch.object(weather_smhi.SmhiWeather, 'retry_update'), \
        patch.object(weather_smhi.SmhiWeather, 'get_weather_forecast',
                     side_effect=SmhiForecastException()):

        hass.async_add_job = Mock()
        call_later = hass.helpers.event.async_call_later

        await weather.async_update()
        assert len(call_later.mock_calls) == 1
        # Assert we are going to wait RETRY_TIMEOUT seconds
        assert call_later.mock_calls[0][1][0] == weather_smhi.RETRY_TIMEOUT
        await hass.config_entries.async_setup(entry.entry_id)
        await hass.async_block_till_done()

    state = hass.states.get(ENTITY_ID)

    assert state
    assert state.name == "test"
    assert state.state == STATE_UNKNOWN
    assert ATTR_FORECAST in state.attributes
    assert all(forecast[ATTR_FORECAST_CONDITION] is None
               for forecast in state.attributes[ATTR_FORECAST])


@pytest.mark.parametrize(
    "error",
    [SmhiForecastException(), asyncio.TimeoutError()])
async def test_refresh_weather_forecast_retry(hass: HomeAssistant,
                                              error: Exception) -> None:
    """Test the refresh weather forecast function."""
    entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG)
    entry.add_to_hass(hass)
    now = utcnow()

    with patch(
            "homeassistant.components.smhi.weather.Smhi.async_get_forecast",
            side_effect=error,
    ) as mock_get_forecast:
        await hass.config_entries.async_setup(entry.entry_id)
        await hass.async_block_till_done()

        state = hass.states.get(ENTITY_ID)
Пример #8
0
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()

    state = opp.states.get(ENTITY_ID)

    assert state
    assert state.name == "test"
    assert state.state == STATE_UNKNOWN
    assert ATTR_FORECAST in state.attributes
    assert all(
        forecast[ATTR_FORECAST_CONDITION] is None
        for forecast in state.attributes[ATTR_FORECAST]
    )


@pytest.mark.parametrize("error", [SmhiForecastException(), asyncio.TimeoutError()])
async def test_refresh_weather_forecast_retry(
    opp: OpenPeerPower, error: Exception
) -> None:
    """Test the refresh weather forecast function."""
    entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG)
    entry.add_to_opp(opp)
    now = utcnow()

    with patch(
        "openpeerpower.components.smhi.weather.Smhi.async_get_forecast",
        side_effect=error,
    ) as mock_get_forecast:
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()