Skip to content

Commit 856e471

Browse files
committed
thrid
1 parent 0720c18 commit 856e471

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

custom_components/nordpool/aio_price.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, currency, client, timeezone=None):
100100
"AggregatePrices",
101101
"AggregatePrices",
102102
"AggregatePrices",
103-
"GetAnnuals",
103+
"AggregatePrices/GetAnnuals",
104104
)
105105
self.API_URL = "https://dataportal-api.nordpoolgroup.com/api/%s"
106106
self.currency = currency
@@ -142,7 +142,7 @@ def _parse_json(self, data, areas=None, data_type=None):
142142
areas = list(areas)
143143

144144
# Ripped from Kipe's nordpool
145-
elif data_type == self.DAILY:
145+
if data_type == self.DAILY:
146146
data_source = ("multiAreaDailyAggregates", "averagePerArea")
147147
elif data_type == self.WEEKLY:
148148
data_source = ("multiAreaWeeklyAggregates", "averagePerArea")
@@ -157,9 +157,12 @@ def _parse_json(self, data, areas=None, data_type=None):
157157
raise Exception(f"Invalid response from Nordpool API: {data}")
158158

159159
# Update currency from data
160-
currency = data["currency"]
160+
# currency it not avaiable in yearly... We just have to trust that the one
161+
# we set in the class is correct.
162+
currency = data.get("currency", self.currency)
161163

162164
# Ensure that the provided currency match the requested one
165+
163166
if currency != self.currency:
164167
raise CurrencyMismatch
165168

@@ -222,6 +225,9 @@ async def _fetch_json(self, data_type, end_date=None, areas=None):
222225
if not isinstance(end_date, date) and not isinstance(end_date, datetime):
223226
end_date = parse_dt(end_date)
224227

228+
if not isinstance(areas, list) and areas is not None:
229+
areas = [i.strip() for i in areas.split(",")]
230+
225231
kws = {
226232
"currency": self.currency,
227233
"market": "DayAhead",
@@ -237,9 +243,9 @@ async def _fetch_json(self, data_type, end_date=None, areas=None):
237243

238244
# Add more exceptions as we find them. KeyError is raised when the api return
239245
# junk due to currency not being available in the data.
240-
@backoff.on_exception(
241-
backoff.expo, (aiohttp.ClientError, KeyError), logger=_LOGGER, max_value=20
242-
)
246+
# @backoff.on_exception(
247+
# backoff.expo, (aiohttp.ClientError, KeyError), logger=_LOGGER, max_value=20
248+
# )
243249
async def fetch(self, data_type, end_date=None, areas=None):
244250
"""
245251
Fetch data from API.
@@ -268,12 +274,16 @@ async def fetch(self, data_type, end_date=None, areas=None):
268274
today = datetime.now()
269275
tomorrow = datetime.now() + timedelta(days=1)
270276

271-
jobs = [
272-
self._fetch_json(data_type, yesterday, areas),
273-
self._fetch_json(data_type, today, areas),
274-
self._fetch_json(data_type, tomorrow, areas),
275-
]
276-
print(jobs)
277+
if data_type == self.HOURLY:
278+
jobs = [
279+
self._fetch_json(data_type, yesterday, areas),
280+
self._fetch_json(data_type, today, areas),
281+
self._fetch_json(data_type, tomorrow, areas),
282+
]
283+
else:
284+
# This is really not today but a year..
285+
# All except from hourly returns the raw values
286+
return await self._fetch_json(data_type, today, areas)
277287

278288
res = await asyncio.gather(*jobs)
279289
raw = [

custom_components/nordpool/services.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@
77
from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse
88
from homeassistant.helpers.aiohttp_client import async_get_clientsession
99
import homeassistant.helpers.config_validation as cv
10-
from .const import _REGIONS
10+
from .const import _REGIONS, DEFAULT_TEMPLATE
1111

1212

1313
_LOGGER = logging.getLogger(__name__)
1414

1515

16+
def check(values):
17+
return any([i for i in values if i in list(_REGIONS.keys())])
18+
19+
20+
def check2(value):
21+
def validator(value):
22+
c = any([i for i in value if i in list(_REGIONS.keys())])
23+
if c is not True:
24+
vol.Invalid("ERRRR")
25+
print(value)
26+
return value
27+
28+
return validator
29+
30+
1631
HOURLY_SCHEMA = vol.Schema(
1732
{
1833
vol.Required("currency"): str,
1934
vol.Required("date"): cv.date,
20-
vol.Required("area"): vol.In(list(_REGIONS.keys())),
35+
vol.Required("area"): check2(cv.ensure_list),
2136
}
2237
)
2338

@@ -26,7 +41,8 @@
2641
{
2742
vol.Required("currency"): str,
2843
vol.Required("year"): cv.matches_regex(r"^[1|2]\d{3}$"),
29-
vol.Required("area"): vol.ensure_list,
44+
vol.Required("area"): check2(cv.ensure_list),
45+
vol.Optional("template"): str,
3046
}
3147
)
3248

@@ -39,25 +55,40 @@ async def async_setup_services(hass: HomeAssistant):
3955

4056
async def hourly(service_call: ServiceCall) -> Any:
4157
sc = service_call.data
58+
_LOGGER.debug("called hourly with %r", sc)
4259

4360
return await AioPrices(sc["currency"], client).hourly(
4461
areas=sc["area"], end_date=sc["date"]
4562
)
4663

4764
async def yearly(service_call: ServiceCall):
4865
sc = service_call.data
49-
return await AioPrices(sc["currency"], client).yearly(
66+
_LOGGER.debug("called hourly with %r", sc)
67+
68+
value = await AioPrices(sc["currency"], client).yearly(
5069
areas=sc["area"], end_date=sc["year"]
5170
)
71+
print(value)
72+
73+
t = cv.template(sc["template"]).async_render_with_possible_json_value(
74+
value, parse_result=True
75+
)
76+
print(t)
77+
78+
return ""
5279

5380
async def monthly(service_call: ServiceCall):
5481
sc = service_call.data
82+
_LOGGER.debug("called monthly with %r", sc)
83+
5584
return await AioPrices(sc["currency"], client).monthly(
5685
areas=sc["area"], end_date=sc["year"]
5786
)
5887

5988
async def daily(service_call: ServiceCall):
6089
sc = service_call.data
90+
_LOGGER.debug("called daily with %r", sc)
91+
6192
return await AioPrices(sc["currency"], client).daily(
6293
areas=sc["area"], end_date=sc["year"]
6394
)

0 commit comments

Comments
 (0)