@@ -142,8 +142,10 @@ def __init__(self, *args, **kwargs) -> None:
142
142
)
143
143
self ._measure_time = datetime .datetime .now ()
144
144
self ._logger = _logger
145
- self ._client : APIClient = APIClient (
146
- co2_api_token = self .co2_signal_api_token , timeout = 10
145
+ self ._client : APIClient | None = (
146
+ None
147
+ if self .co2_intensity
148
+ else APIClient (co2_api_token = self .co2_signal_api_token , timeout = 10 )
147
149
)
148
150
self ._processes : dict [str , ProcessData ] = {}
149
151
@@ -156,7 +158,7 @@ def check_refresh(self) -> bool:
156
158
whether a refresh of the CO2 intensity was requested
157
159
from the CO2 Signal API.
158
160
"""
159
- if not self . co2_intensity and (
161
+ if (
160
162
not self ._local_data .setdefault (self ._client .country_code , {})
161
163
or self .outdated
162
164
):
@@ -193,12 +195,11 @@ def estimate_co2_emissions(
193
195
if not (_process := self ._processes .get (process_id )):
194
196
self ._processes [process_id ] = (_process := ProcessData ())
195
197
196
- self .check_refresh ()
197
-
198
198
if self .co2_intensity :
199
199
_current_co2_intensity = self .co2_intensity
200
200
_co2_units = "kgCO2/kWh"
201
201
else :
202
+ self .check_refresh ()
202
203
self ._current_co2_data = CO2SignalResponse (
203
204
** self ._local_data [self ._client .country_code ]
204
205
)
@@ -255,7 +256,7 @@ def process_data(self) -> dict[str, ProcessData]:
255
256
256
257
@property
257
258
def current_carbon_intensity (self ) -> float :
258
- return self ._client .get ().data .carbon_intensity
259
+ return self .co2_intensity or self . _client .get ().data .carbon_intensity
259
260
260
261
@property
261
262
def total_power_usage (self ) -> float :
0 commit comments