Skip to content

Commit 95fa6d4

Browse files
yujia21VIKTORVAV99
andauthored
refactor: use event class in NZ parser #6011 (#8418)
Co-authored-by: Viktor Andersson <[email protected]>
1 parent 5a9629c commit 95fa6d4

File tree

2 files changed

+65
-61
lines changed

2 files changed

+65
-61
lines changed

electricitymap/contrib/parsers/NZ.py

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
import json
55
from datetime import datetime, timezone
66
from logging import Logger, getLogger
7+
from typing import Any
78

89
from bs4 import BeautifulSoup
910

1011
# The request library is used to fetch content through HTTP
1112
from requests import Session
1213

14+
from electricitymap.contrib.config import ZoneKey
15+
from electricitymap.contrib.lib.models.event_lists import (
16+
PriceList,
17+
ProductionBreakdownList,
18+
)
19+
from electricitymap.contrib.lib.models.events import ProductionMix, StorageMix
20+
1321
time_zone = "Pacific/Auckland"
1422

1523
NZ_PRICE_REGIONS = set(range(1, 14))
@@ -30,11 +38,11 @@ def fetch(session: Session | None = None):
3038

3139

3240
def fetch_price(
33-
zone_key: str = "NZ",
41+
zone_key: ZoneKey = ZoneKey("NZ"),
3442
session: Session | None = None,
3543
target_datetime: datetime | None = None,
3644
logger: Logger = getLogger(__name__),
37-
) -> dict:
45+
) -> list[dict[str, Any]]:
3846
"""
3947
Requests the current price of electricity based on the zone key.
4048
@@ -64,24 +72,23 @@ def fetch_price(
6472
date_time = datetime.strptime(time, "%Y-%m-%dT%H:%M:%SZ").replace(
6573
tzinfo=timezone.utc
6674
)
67-
68-
return [
69-
{
70-
"datetime": date_time,
71-
"price": avg_price,
72-
"currency": "NZD",
73-
"source": "api.em6.co.nz",
74-
"zoneKey": zone_key,
75-
}
76-
]
75+
price_list = PriceList(logger)
76+
price_list.append(
77+
zoneKey=zone_key,
78+
price=avg_price,
79+
currency="NZD",
80+
datetime=date_time,
81+
source="api.em6.co.nz",
82+
)
83+
return price_list.to_list()
7784

7885

7986
def fetch_production(
80-
zone_key: str = "NZ",
87+
zone_key: ZoneKey = ZoneKey("NZ"),
8188
session: Session | None = None,
8289
target_datetime: datetime | None = None,
8390
logger: Logger = getLogger(__name__),
84-
) -> dict:
91+
) -> list[dict[str, Any]]:
8592
"""Requests the last known production mix (in MW) of a given zone."""
8693
if target_datetime:
8794
raise NotImplementedError(
@@ -94,54 +101,43 @@ def fetch_production(
94101

95102
region_key = "New Zealand"
96103
productions = obj["soPgenGraph"]["data"][region_key]
97-
98-
data = [
99-
{
100-
"zoneKey": zone_key,
101-
"datetime": date_time,
102-
"production": {
103-
"coal": productions.get("Coal", {"generation": None})["generation"],
104-
"oil": productions.get("Diesel/Oil", {"generation": None})[
105-
"generation"
106-
],
107-
"gas": productions.get("Gas", {"generation": None})["generation"],
108-
"geothermal": productions.get("Geothermal", {"generation": None})[
109-
"generation"
110-
],
111-
"wind": productions.get("Wind", {"generation": None})["generation"],
112-
"hydro": productions.get("Hydro", {"generation": None})["generation"],
113-
"solar": productions.get("Solar", {"generation": None})["generation"],
114-
"unknown": productions.get("Co-Gen", {"generation": None})[
115-
"generation"
116-
],
117-
"nuclear": 0, # famous issue in NZ politics
118-
},
119-
"capacity": {
120-
"coal": productions.get("Coal", {"capacity": None})["capacity"],
121-
"oil": productions.get("Diesel/Oil", {"capacity": None})["capacity"],
122-
"gas": productions.get("Gas", {"capacity": None})["capacity"],
123-
"geothermal": productions.get("Geothermal", {"capacity": None})[
124-
"capacity"
125-
],
126-
"wind": productions.get("Wind", {"capacity": None})["capacity"],
127-
"hydro": productions.get("Hydro", {"capacity": None})["capacity"],
128-
"solar": productions.get("Solar", {"capacity": None})["capacity"],
129-
"battery storage": productions.get("Battery", {"capacity": None})[
130-
"capacity"
131-
],
132-
"unknown": productions.get("Co-Gen", {"capacity": None})["capacity"],
133-
"nuclear": 0, # famous issue in NZ politics
134-
},
135-
"storage": {
136-
"battery": productions.get("Battery", {"generation": None})[
137-
"generation"
138-
],
139-
},
140-
"source": "transpower.co.nz",
141-
}
104+
production_breakdowns = ProductionBreakdownList(logger)
105+
production_mix = ProductionMix()
106+
mix_mapping = {
107+
"coal": "Coal",
108+
"oil": "Diesel/Oil",
109+
"gas": "Gas",
110+
"geothermal": "Geothermal",
111+
"wind": "Wind",
112+
"hydro": "Hydro",
113+
"solar": "Solar",
114+
"unknown": "Co-Gen",
115+
}
116+
for mix_key, prod_key in mix_mapping.items():
117+
production_mix.add_value(
118+
mix_key, productions.get(prod_key, {"generation": None})["generation"]
119+
)
120+
production_mix.add_value("nuclear", 0) # famous issue in NZ politics
121+
storage_mix = StorageMix()
122+
storage_mix.add_value(
123+
"battery", productions.get("Battery", {"generation": None})["generation"]
124+
)
125+
production_breakdowns.append(
126+
zoneKey=zone_key,
127+
datetime=date_time,
128+
production=production_mix,
129+
storage=storage_mix,
130+
source="transpower.co.nz",
131+
)
132+
capacity = {
133+
mix_key: productions.get(prod_key, {"capacity": None})["capacity"]
134+
for mix_key, prod_key in mix_mapping.items()
135+
}
136+
capacity["battery storage"] = productions.get("Battery", {"capacity": None})[
137+
"capacity"
142138
]
143-
144-
return data
139+
capacity["nuclear"] = 0 # famous issue in NZ politics
140+
return [{**e, **{"capacity": capacity}} for e in production_breakdowns.to_list()]
145141

146142

147143
if __name__ == "__main__":

electricitymap/contrib/parsers/tests/__snapshots__/test_NZ.ambr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'datetime': datetime.datetime(2024, 4, 24, 18, 30, tzinfo=datetime.timezone.utc),
88
'price': 79.8623076923077,
99
'source': 'api.em6.co.nz',
10+
'sourceType': <EventSourceType.measured: 'measured'>,
1011
'zoneKey': 'NZ',
1112
}),
1213
]),
@@ -16,6 +17,7 @@
1617
'datetime': datetime.datetime(2024, 4, 24, 18, 30, tzinfo=datetime.timezone.utc),
1718
'price': 79.8623076923077,
1819
'source': 'api.em6.co.nz',
20+
'sourceType': <EventSourceType.measured: 'measured'>,
1921
'zoneKey': 'NZ',
2022
}),
2123
]),
@@ -37,6 +39,8 @@
3739
'unknown': 168,
3840
'wind': 1259,
3941
}),
42+
'correctedModes': list([
43+
]),
4044
'datetime': datetime.datetime(2024, 4, 24, 18, 0, tzinfo=datetime.timezone.utc),
4145
'production': dict({
4246
'coal': 156,
@@ -50,6 +54,7 @@
5054
'wind': 814,
5155
}),
5256
'source': 'transpower.co.nz',
57+
'sourceType': <EventSourceType.measured: 'measured'>,
5358
'storage': dict({
5459
'battery': 0,
5560
}),
@@ -70,6 +75,8 @@
7075
'unknown': 168,
7176
'wind': 1259,
7277
}),
78+
'correctedModes': list([
79+
]),
7380
'datetime': datetime.datetime(2024, 4, 24, 18, 0, tzinfo=datetime.timezone.utc),
7481
'production': dict({
7582
'coal': 156,
@@ -83,6 +90,7 @@
8390
'wind': 814,
8491
}),
8592
'source': 'transpower.co.nz',
93+
'sourceType': <EventSourceType.measured: 'measured'>,
8694
'storage': dict({
8795
'battery': 0,
8896
}),

0 commit comments

Comments
 (0)