44import json
55from datetime import datetime , timezone
66from logging import Logger , getLogger
7+ from typing import Any
78
89from bs4 import BeautifulSoup
910
1011# The request library is used to fetch content through HTTP
1112from 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+
1321time_zone = "Pacific/Auckland"
1422
1523NZ_PRICE_REGIONS = set (range (1 , 14 ))
@@ -30,11 +38,11 @@ def fetch(session: Session | None = None):
3038
3139
3240def 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
7986def 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
147143if __name__ == "__main__" :
0 commit comments