Ejemplo n.º 1
0
async def test_cannot_connect(hass):
    """Test that errors are shown when cannot connect to GIOS server."""
    with patch("gios.Gios._async_get", side_effect=ApiError("error")):
        flow = config_flow.GiosFlowHandler()
        flow.hass = hass
        flow.context = {}

        result = await flow.async_step_user(user_input=CONFIG)

        assert result["errors"] == {"base": "cannot_connect"}
Ejemplo n.º 2
0
async def test_api_error():
    """Test GIOS API error."""
    with patch("gios.Gios._async_get",
               side_effect=ApiError(404)), pytest.raises(ApiError) as error:

        async with ClientSession() as websession:
            gios = Gios(VALID_STATION_ID, websession)
            await gios.update()
    assert str(error.value) == "404"
    assert gios.available is False
Ejemplo n.º 3
0
async def test_availability(hass):
    """Ensure that we mark the entities unavailable correctly when service causes an error."""
    await init_integration(hass)

    state = hass.states.get("sensor.home_pm2_5")
    assert state
    assert state.state != STATE_UNAVAILABLE
    assert state.state == "4"

    future = utcnow() + timedelta(minutes=60)
    with patch(
        "homeassistant.components.gios.Gios._get_all_sensors",
        side_effect=ApiError("Unexpected error"),
    ):
        async_fire_time_changed(hass, future)
        await hass.async_block_till_done()

        state = hass.states.get("sensor.home_pm2_5")
        assert state
        assert state.state == STATE_UNAVAILABLE

    future = utcnow() + timedelta(minutes=120)
    with patch(
        "homeassistant.components.gios.Gios._get_all_sensors",
        return_value=json.loads(load_fixture("gios/sensors.json")),
    ), patch(
        "homeassistant.components.gios.Gios._get_indexes",
        return_value={},
    ):
        async_fire_time_changed(hass, future)
        await hass.async_block_till_done()

        state = hass.states.get("sensor.home_pm2_5")
        assert state
        assert state.state != STATE_UNAVAILABLE
        assert state.state == "4"

        state = hass.states.get("sensor.home_aqi")
        assert state
        assert state.state == STATE_UNAVAILABLE