Ejemplo n.º 1
0
async def test_fetch_general_and_feed_in_site(hass: HomeAssistant,
                                              current_price_api: Mock) -> None:
    """Test fetching a site with a general and feed_in channel."""

    current_price_api.get_current_price.return_value = GENERAL_CHANNEL + FEED_IN_CHANNEL
    data_service = AmberUpdateCoordinator(hass, current_price_api,
                                          GENERAL_AND_FEED_IN_SITE_ID)
    result = await data_service._async_update_data()

    current_price_api.get_current_price.assert_called_with(
        GENERAL_AND_FEED_IN_SITE_ID, next=48)

    assert result["current"].get("general") == GENERAL_CHANNEL[0]
    assert result["forecasts"].get("general") == [
        GENERAL_CHANNEL[1],
        GENERAL_CHANNEL[2],
        GENERAL_CHANNEL[3],
    ]
    assert result["current"].get("controlled_load") is None
    assert result["forecasts"].get("controlled_load") is None
    assert result["current"].get("feed_in") is FEED_IN_CHANNEL[0]
    assert result["forecasts"].get("feed_in") == [
        FEED_IN_CHANNEL[1],
        FEED_IN_CHANNEL[2],
        FEED_IN_CHANNEL[3],
    ]
    assert result["grid"]["renewables"] == round(GENERAL_CHANNEL[0].renewables)
    assert result["grid"]["price_spike"] == "none"
Ejemplo n.º 2
0
async def test_fetch_no_general_site(hass: HomeAssistant,
                                     current_price_api: Mock) -> None:
    """Test fetching a site with no general channel."""

    current_price_api.get_current_price.return_value = CONTROLLED_LOAD_CHANNEL
    data_service = AmberUpdateCoordinator(hass, current_price_api,
                                          GENERAL_ONLY_SITE_ID)
    with pytest.raises(UpdateFailed):
        await data_service._async_update_data()

    current_price_api.get_current_price.assert_called_with(
        GENERAL_ONLY_SITE_ID, next=48)
Ejemplo n.º 3
0
async def test_fetch_spike(hass: HomeAssistant, current_price_api: Mock) -> None:
    """Test fetching a site with only a general channel."""

    general_channel: list[CurrentInterval] = [
        generate_current_interval(
            ChannelType.GENERAL, parser.parse("2021-09-21T08:30:00+10:00")
        ),
    ]
    general_channel[0].spike_status = SpikeStatus.SPIKE
    current_price_api.get_current_price.return_value = general_channel
    data_service = AmberUpdateCoordinator(hass, current_price_api, GENERAL_ONLY_SITE_ID)
    result = await data_service._async_update_data()
    assert result["grid"]["price_spike"] == "spike"
Ejemplo n.º 4
0
async def test_fetch_api_error(hass: HomeAssistant,
                               current_price_api: Mock) -> None:
    """Test that the old values are maintained if a second call fails."""

    current_price_api.get_current_price.return_value = GENERAL_CHANNEL
    data_service = AmberUpdateCoordinator(hass, current_price_api,
                                          GENERAL_ONLY_SITE_ID)
    result = await data_service._async_update_data()

    current_price_api.get_current_price.assert_called_with(
        GENERAL_ONLY_SITE_ID, next=48)

    assert result["current"].get("general") == GENERAL_CHANNEL[0]
    assert result["forecasts"].get("general") == [
        GENERAL_CHANNEL[1],
        GENERAL_CHANNEL[2],
        GENERAL_CHANNEL[3],
    ]
    assert result["current"].get("controlled_load") is None
    assert result["forecasts"].get("controlled_load") is None
    assert result["current"].get("feed_in") is None
    assert result["forecasts"].get("feed_in") is None
    assert result["grid"]["renewables"] == round(GENERAL_CHANNEL[0].renewables)

    current_price_api.get_current_price.side_effect = ApiException(status=403)
    with pytest.raises(UpdateFailed):
        await data_service._async_update_data()

    assert result["current"].get("general") == GENERAL_CHANNEL[0]
    assert result["forecasts"].get("general") == [
        GENERAL_CHANNEL[1],
        GENERAL_CHANNEL[2],
        GENERAL_CHANNEL[3],
    ]
    assert result["current"].get("controlled_load") is None
    assert result["forecasts"].get("controlled_load") is None
    assert result["current"].get("feed_in") is None
    assert result["forecasts"].get("feed_in") is None
    assert result["grid"]["renewables"] == round(GENERAL_CHANNEL[0].renewables)
    assert result["grid"]["price_spike"] == "none"