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
async def test_retry_update(): """Test retry function of refresh forecast.""" hass = Mock() weather = weather_smhi.SmhiWeather('name', '17.0022', '62.0022') weather.hass = hass with patch.object(weather_smhi.SmhiWeather, 'async_update') as update: await weather.retry_update() assert len(update.mock_calls) == 1
async def test_retry_update(): """Test retry function of refresh forecast.""" hass = Mock() weather = weather_smhi.SmhiWeather("name", "17.0022", "62.0022") weather.hass = hass with patch.object(weather, "async_update", AsyncMock()) as update: await weather.retry_update(None) assert len(update.mock_calls) == 1
def test_properties_unknown_symbol() -> None: """Test behaviour when unknown symbol from API.""" hass = Mock() data = Mock() data.temperature = 5 data.mean_precipitation = 0.5 data.total_precipitation = 1 data.humidity = 5 data.wind_speed = 10 data.wind_direction = 180 data.horizontal_visibility = 6 data.pressure = 1008 data.cloudiness = 52 data.symbol = 100 # Faulty symbol data.valid_time = datetime(2018, 1, 1, 0, 1, 2) data2 = Mock() data2.temperature = 5 data2.mean_precipitation = 0.5 data2.total_precipitation = 1 data2.humidity = 5 data2.wind_speed = 10 data2.wind_direction = 180 data2.horizontal_visibility = 6 data2.pressure = 1008 data2.cloudiness = 52 data2.symbol = 100 # Faulty symbol data2.valid_time = datetime(2018, 1, 1, 12, 1, 2) data3 = Mock() data3.temperature = 5 data3.mean_precipitation = 0.5 data3.total_precipitation = 1 data3.humidity = 5 data3.wind_speed = 10 data3.wind_direction = 180 data3.horizontal_visibility = 6 data3.pressure = 1008 data3.cloudiness = 52 data3.symbol = 100 # Faulty symbol data3.valid_time = datetime(2018, 1, 2, 12, 1, 2) testdata = [data, data2, data3] weather = weather_smhi.SmhiWeather('name', '10', '10') weather.hass = hass weather._forecasts = testdata assert weather.condition is None forecast = weather.forecast[0] assert forecast[ATTR_FORECAST_CONDITION] is None
async def test_refresh_weather_forecast_timeout(hass) -> None: """Test timeout exception.""" 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.SmhiWeather, 'retry_update'), \ patch.object(weather_smhi.SmhiWeather, 'get_weather_forecast', side_effect=asyncio.TimeoutError): 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
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
def test_properties_no_data(hass: HomeAssistant) -> None: """Test properties when no API data available.""" weather = weather_smhi.SmhiWeather('name', '10', '10') weather.hass = hass assert weather.name == 'name' assert weather.should_poll is True assert weather.temperature is None assert weather.humidity is None assert weather.wind_speed is None assert weather.wind_bearing is None assert weather.visibility is None assert weather.pressure is None assert weather.cloudiness is None assert weather.condition is None assert weather.forecast is None assert weather.temperature_unit == TEMP_CELSIUS
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
async def test_refresh_weather_forecast_timeout(hass) -> None: """Test timeout exception.""" 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.SmhiWeather, "retry_update"), patch.object( weather_smhi.SmhiWeather, "get_weather_forecast", side_effect=asyncio.TimeoutError, ): 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
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