mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Add missing areas in Nord Pool services (#169752)
This commit is contained in:
@@ -36,6 +36,22 @@ if TYPE_CHECKING:
|
||||
from . import NordPoolConfigEntry
|
||||
from .const import ATTR_RESOLUTION, DOMAIN
|
||||
|
||||
|
||||
def _validate_areas(areas: list[str]) -> list[str]:
|
||||
"""Validate the areas."""
|
||||
validated_areas: list[str] = []
|
||||
|
||||
for area in areas:
|
||||
validated_area = cv.string(area)
|
||||
validated_area = validated_area.upper()
|
||||
if validated_area not in AREAS:
|
||||
raise vol.Invalid(f"Area {area} is not valid")
|
||||
|
||||
validated_areas.append(validated_area)
|
||||
|
||||
return validated_areas
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
ATTR_CONFIG_ENTRY = "config_entry"
|
||||
ATTR_AREAS = "areas"
|
||||
@@ -47,9 +63,11 @@ SERVICE_GET_PRICES_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector({"integration": DOMAIN}),
|
||||
vol.Required(ATTR_DATE): cv.date,
|
||||
vol.Optional(ATTR_AREAS): vol.All(vol.In(list(AREAS)), cv.ensure_list, [str]),
|
||||
vol.Optional(ATTR_AREAS, default=[]): vol.All(cv.ensure_list, _validate_areas),
|
||||
vol.Optional(ATTR_CURRENCY): vol.All(
|
||||
cv.string, vol.In([currency.value for currency in Currency])
|
||||
cv.string,
|
||||
vol.Upper,
|
||||
vol.In([currency.value for currency in Currency]),
|
||||
),
|
||||
}
|
||||
)
|
||||
@@ -76,20 +94,14 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
client = entry.runtime_data.client
|
||||
asked_date: date = call.data[ATTR_DATE]
|
||||
|
||||
areas: list[str] = entry.data[ATTR_AREAS]
|
||||
if _areas := call.data.get(ATTR_AREAS):
|
||||
areas = _areas
|
||||
areas = call.data.get(ATTR_AREAS)
|
||||
areas = areas or entry.data[ATTR_AREAS]
|
||||
|
||||
currency: str = entry.data[ATTR_CURRENCY]
|
||||
if _currency := call.data.get(ATTR_CURRENCY):
|
||||
currency = _currency
|
||||
currency = call.data.get(ATTR_CURRENCY)
|
||||
currency = currency or entry.data[ATTR_CURRENCY]
|
||||
|
||||
resolution: int = 60
|
||||
if _resolution := call.data.get(ATTR_RESOLUTION):
|
||||
resolution = _resolution
|
||||
|
||||
areas = [area.upper() for area in areas]
|
||||
currency = currency.upper()
|
||||
resolution = call.data.get(ATTR_RESOLUTION)
|
||||
resolution = resolution or 60
|
||||
|
||||
return (client, asked_date, currency, areas, resolution)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ get_prices_for_date:
|
||||
areas:
|
||||
selector:
|
||||
select:
|
||||
multiple: true
|
||||
options:
|
||||
- "EE"
|
||||
- "LT"
|
||||
@@ -34,6 +35,8 @@ get_prices_for_date:
|
||||
- "SE2"
|
||||
- "SE3"
|
||||
- "SE4"
|
||||
- "BG"
|
||||
- "TEL"
|
||||
- "SYS"
|
||||
mode: dropdown
|
||||
currency:
|
||||
@@ -60,6 +63,7 @@ get_price_indices_for_date:
|
||||
areas:
|
||||
selector:
|
||||
select:
|
||||
multiple: true
|
||||
options:
|
||||
- "EE"
|
||||
- "LT"
|
||||
@@ -82,6 +86,8 @@ get_price_indices_for_date:
|
||||
- "SE2"
|
||||
- "SE3"
|
||||
- "SE4"
|
||||
- "BG"
|
||||
- "TEL"
|
||||
- "SYS"
|
||||
mode: dropdown
|
||||
currency:
|
||||
|
||||
@@ -75,6 +75,17 @@ async def get_data_from_library(
|
||||
},
|
||||
json=load_json[0],
|
||||
)
|
||||
aioclient_mock.request(
|
||||
"GET",
|
||||
url=API + "/DayAheadPrices",
|
||||
params={
|
||||
"date": "2025-10-01",
|
||||
"market": "DayAhead",
|
||||
"deliveryArea": "SE3,SE4",
|
||||
"currency": "EUR",
|
||||
},
|
||||
json=load_json[0],
|
||||
)
|
||||
aioclient_mock.request(
|
||||
"GET",
|
||||
url=API + "/DayAheadPrices",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
||||
"""Test services in Nord Pool."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from pynordpool import (
|
||||
@@ -34,6 +35,18 @@ TEST_SERVICE_DATA = {
|
||||
ATTR_AREAS: "SE3",
|
||||
ATTR_CURRENCY: "EUR",
|
||||
}
|
||||
TEST_SERVICE_DATA2 = {
|
||||
ATTR_CONFIG_ENTRY: "to_replace",
|
||||
ATTR_DATE: "2025-10-01",
|
||||
ATTR_AREAS: "se3",
|
||||
ATTR_CURRENCY: "eur",
|
||||
}
|
||||
TEST_SERVICE_DATA3 = {
|
||||
ATTR_CONFIG_ENTRY: "to_replace",
|
||||
ATTR_DATE: "2025-10-01",
|
||||
ATTR_AREAS: ["SE3", "SE4"],
|
||||
ATTR_CURRENCY: "EUR",
|
||||
}
|
||||
TEST_SERVICE_DATA_USE_DEFAULTS = {
|
||||
ATTR_CONFIG_ENTRY: "to_replace",
|
||||
ATTR_DATE: "2025-10-01",
|
||||
@@ -54,15 +67,29 @@ TEST_SERVICE_INDICES_DATA_15 = {
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test_config",
|
||||
[
|
||||
(TEST_SERVICE_DATA),
|
||||
(TEST_SERVICE_DATA2),
|
||||
(TEST_SERVICE_DATA3),
|
||||
],
|
||||
ids=[
|
||||
"single_area_uppercase_currency_uppercase",
|
||||
"single_area_lowercase_currency_lowercase",
|
||||
"multiple_areas_uppercase_currency_uppercase",
|
||||
],
|
||||
)
|
||||
@pytest.mark.freeze_time("2025-10-01T18:00:00+00:00")
|
||||
async def test_service_call(
|
||||
hass: HomeAssistant,
|
||||
load_int: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
test_config: dict[str, Any],
|
||||
) -> None:
|
||||
"""Test get_prices_for_date service call."""
|
||||
|
||||
service_data = TEST_SERVICE_DATA.copy()
|
||||
service_data = test_config.copy()
|
||||
service_data[ATTR_CONFIG_ENTRY] = load_int.entry_id
|
||||
response = await hass.services.async_call(
|
||||
DOMAIN,
|
||||
@@ -73,7 +100,15 @@ async def test_service_call(
|
||||
)
|
||||
|
||||
assert response == snapshot
|
||||
price_value = response["SE3"][0]["price"]
|
||||
|
||||
|
||||
@pytest.mark.freeze_time("2025-10-01T18:00:00+00:00")
|
||||
async def test_service_call_use_defaults(
|
||||
hass: HomeAssistant,
|
||||
load_int: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test get_prices_for_date service call using default values."""
|
||||
|
||||
service_data = TEST_SERVICE_DATA_USE_DEFAULTS.copy()
|
||||
service_data[ATTR_CONFIG_ENTRY] = load_int.entry_id
|
||||
@@ -86,7 +121,6 @@ async def test_service_call(
|
||||
)
|
||||
|
||||
assert "SE3" in response
|
||||
assert response["SE3"][0]["price"] == price_value
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
Reference in New Issue
Block a user