Add demand window sensor for amberelectric (#121356)

This commit is contained in:
Xidorn Quan
2024-07-06 20:28:52 +10:00
committed by GitHub
parent be0cf545b2
commit 2bc7904b51
5 changed files with 123 additions and 15 deletions

View File

@@ -8,6 +8,7 @@ from unittest.mock import Mock, patch
from amberelectric.model.channel import ChannelType
from amberelectric.model.current_interval import CurrentInterval
from amberelectric.model.interval import SpikeStatus
from amberelectric.model.tariff_information import TariffInformation
from dateutil import parser
import pytest
@@ -111,7 +112,7 @@ async def setup_spike(hass: HomeAssistant) -> AsyncGenerator[Mock]:
@pytest.mark.usefixtures("setup_no_spike")
def test_no_spike_sensor(hass: HomeAssistant) -> None:
"""Testing the creation of the Amber renewables sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
sensor = hass.states.get("binary_sensor.mock_title_price_spike")
assert sensor
assert sensor.state == "off"
@@ -122,7 +123,7 @@ def test_no_spike_sensor(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("setup_potential_spike")
def test_potential_spike_sensor(hass: HomeAssistant) -> None:
"""Testing the creation of the Amber renewables sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
sensor = hass.states.get("binary_sensor.mock_title_price_spike")
assert sensor
assert sensor.state == "off"
@@ -133,9 +134,85 @@ def test_potential_spike_sensor(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("setup_spike")
def test_spike_sensor(hass: HomeAssistant) -> None:
"""Testing the creation of the Amber renewables sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
sensor = hass.states.get("binary_sensor.mock_title_price_spike")
assert sensor
assert sensor.state == "on"
assert sensor.attributes["icon"] == "mdi:power-plug-off"
assert sensor.attributes["spike_status"] == "spike"
@pytest.fixture
async def setup_inactive_demand_window(hass: HomeAssistant) -> AsyncGenerator[Mock]:
"""Set up general channel."""
MockConfigEntry(
domain="amberelectric",
data={
CONF_SITE_NAME: "mock_title",
CONF_API_TOKEN: MOCK_API_TOKEN,
CONF_SITE_ID: GENERAL_ONLY_SITE_ID,
},
).add_to_hass(hass)
instance = Mock()
with patch(
"amberelectric.api.AmberApi.create",
return_value=instance,
) as mock_update:
general_channel: list[CurrentInterval] = [
generate_current_interval(
ChannelType.GENERAL, parser.parse("2021-09-21T08:30:00+10:00")
),
]
general_channel[0].tariff_information = TariffInformation(demandWindow=False)
instance.get_current_price = Mock(return_value=general_channel)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
yield mock_update.return_value
@pytest.fixture
async def setup_active_demand_window(hass: HomeAssistant) -> AsyncGenerator[Mock]:
"""Set up general channel."""
MockConfigEntry(
domain="amberelectric",
data={
CONF_SITE_NAME: "mock_title",
CONF_API_TOKEN: MOCK_API_TOKEN,
CONF_SITE_ID: GENERAL_ONLY_SITE_ID,
},
).add_to_hass(hass)
instance = Mock()
with patch(
"amberelectric.api.AmberApi.create",
return_value=instance,
) as mock_update:
general_channel: list[CurrentInterval] = [
generate_current_interval(
ChannelType.GENERAL, parser.parse("2021-09-21T08:30:00+10:00")
),
]
general_channel[0].tariff_information = TariffInformation(demandWindow=True)
instance.get_current_price = Mock(return_value=general_channel)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
yield mock_update.return_value
@pytest.mark.usefixtures("setup_inactive_demand_window")
def test_inactive_demand_window_sensor(hass: HomeAssistant) -> None:
"""Testing the creation of the Amber demand_window sensor."""
assert len(hass.states.async_all()) == 6
sensor = hass.states.get("binary_sensor.mock_title_demand_window")
assert sensor
assert sensor.state == "off"
@pytest.mark.usefixtures("setup_active_demand_window")
def test_active_demand_window_sensor(hass: HomeAssistant) -> None:
"""Testing the creation of the Amber demand_window sensor."""
assert len(hass.states.async_all()) == 6
sensor = hass.states.get("binary_sensor.mock_title_demand_window")
assert sensor
assert sensor.state == "on"

View File

@@ -105,7 +105,7 @@ async def setup_general_and_feed_in(hass: HomeAssistant) -> AsyncGenerator[Mock]
async def test_general_price_sensor(hass: HomeAssistant, setup_general: Mock) -> None:
"""Test the General Price sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
price = hass.states.get("sensor.mock_title_general_price")
assert price
assert price.state == "0.08"
@@ -143,7 +143,7 @@ async def test_general_price_sensor(hass: HomeAssistant, setup_general: Mock) ->
@pytest.mark.usefixtures("setup_general_and_controlled_load")
async def test_general_and_controlled_load_price_sensor(hass: HomeAssistant) -> None:
"""Test the Controlled Price sensor."""
assert len(hass.states.async_all()) == 8
assert len(hass.states.async_all()) == 9
price = hass.states.get("sensor.mock_title_controlled_load_price")
assert price
assert price.state == "0.08"
@@ -165,7 +165,7 @@ async def test_general_and_controlled_load_price_sensor(hass: HomeAssistant) ->
@pytest.mark.usefixtures("setup_general_and_feed_in")
async def test_general_and_feed_in_price_sensor(hass: HomeAssistant) -> None:
"""Test the Feed In sensor."""
assert len(hass.states.async_all()) == 8
assert len(hass.states.async_all()) == 9
price = hass.states.get("sensor.mock_title_feed_in_price")
assert price
assert price.state == "-0.08"
@@ -188,7 +188,7 @@ async def test_general_forecast_sensor(
hass: HomeAssistant, setup_general: Mock
) -> None:
"""Test the General Forecast sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
price = hass.states.get("sensor.mock_title_general_forecast")
assert price
assert price.state == "0.09"
@@ -230,7 +230,7 @@ async def test_general_forecast_sensor(
@pytest.mark.usefixtures("setup_general_and_controlled_load")
async def test_controlled_load_forecast_sensor(hass: HomeAssistant) -> None:
"""Test the Controlled Load Forecast sensor."""
assert len(hass.states.async_all()) == 8
assert len(hass.states.async_all()) == 9
price = hass.states.get("sensor.mock_title_controlled_load_forecast")
assert price
assert price.state == "0.09"
@@ -254,7 +254,7 @@ async def test_controlled_load_forecast_sensor(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("setup_general_and_feed_in")
async def test_feed_in_forecast_sensor(hass: HomeAssistant) -> None:
"""Test the Feed In Forecast sensor."""
assert len(hass.states.async_all()) == 8
assert len(hass.states.async_all()) == 9
price = hass.states.get("sensor.mock_title_feed_in_forecast")
assert price
assert price.state == "-0.09"
@@ -278,7 +278,7 @@ async def test_feed_in_forecast_sensor(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("setup_general")
def test_renewable_sensor(hass: HomeAssistant) -> None:
"""Testing the creation of the Amber renewables sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
sensor = hass.states.get("sensor.mock_title_renewables")
assert sensor
assert sensor.state == "51"
@@ -287,7 +287,7 @@ def test_renewable_sensor(hass: HomeAssistant) -> None:
@pytest.mark.usefixtures("setup_general")
def test_general_price_descriptor_descriptor_sensor(hass: HomeAssistant) -> None:
"""Test the General Price Descriptor sensor."""
assert len(hass.states.async_all()) == 5
assert len(hass.states.async_all()) == 6
price = hass.states.get("sensor.mock_title_general_price_descriptor")
assert price
assert price.state == "extremely_low"
@@ -298,7 +298,7 @@ def test_general_and_controlled_load_price_descriptor_sensor(
hass: HomeAssistant,
) -> None:
"""Test the Controlled Price Descriptor sensor."""
assert len(hass.states.async_all()) == 8
assert len(hass.states.async_all()) == 9
price = hass.states.get("sensor.mock_title_controlled_load_price_descriptor")
assert price
assert price.state == "extremely_low"
@@ -307,7 +307,7 @@ def test_general_and_controlled_load_price_descriptor_sensor(
@pytest.mark.usefixtures("setup_general_and_feed_in")
def test_general_and_feed_in_price_descriptor_sensor(hass: HomeAssistant) -> None:
"""Test the Feed In Price Descriptor sensor."""
assert len(hass.states.async_all()) == 8
assert len(hass.states.async_all()) == 9
price = hass.states.get("sensor.mock_title_feed_in_price_descriptor")
assert price
assert price.state == "extremely_low"