diff --git a/carbontracker/emissions/intensity/fetchers/electricitymaps.py b/carbontracker/emissions/intensity/fetchers/electricitymaps.py index a654afb..f258601 100644 --- a/carbontracker/emissions/intensity/fetchers/electricitymaps.py +++ b/carbontracker/emissions/intensity/fetchers/electricitymaps.py @@ -3,6 +3,7 @@ from carbontracker import exceptions from carbontracker.emissions.intensity.fetcher import IntensityFetcher from carbontracker.emissions.intensity import intensity +from carbontracker.loggerutil import Logger API_URL = "https://api-access.electricitymaps.com/free-tier/carbon-intensity/latest" @@ -10,12 +11,18 @@ class ElectricityMap(IntensityFetcher): _api_key = None + def __init__(self, logger: Logger): + self.logger = logger + @classmethod def set_api_key(cls, key): cls._api_key = key def suitable(self, g_location): - return self._api_key is not None + has_key = self._api_key is not None + if not has_key: + self.logger.err_warn("ElectricityMaps API key not set. Will default to average carbon intensity.") + return has_key def carbon_intensity(self, g_location, time_dur=None): carbon_intensity = intensity.CarbonIntensity(g_location=g_location) diff --git a/carbontracker/emissions/intensity/intensity.py b/carbontracker/emissions/intensity/intensity.py index dd7d46c..4ecfe2f 100644 --- a/carbontracker/emissions/intensity/intensity.py +++ b/carbontracker/emissions/intensity/intensity.py @@ -98,12 +98,13 @@ def set_default_message(self): self.message = default_intensity["description"] -def carbon_intensity(logger, time_dur=None): - fetchers = [ - electricitymaps.ElectricityMap(), - energidataservice.EnergiDataService(), - carbonintensitygb.CarbonIntensityGB(), - ] +def carbon_intensity(logger, time_dur=None, fetchers=None): + if fetchers is None: + fetchers = [ + electricitymaps.ElectricityMap(logger=logger), + #energidataservice.EnergiDataService(), # UPDATE 2024: EnergiDataService/CarbonIntensityGB has been deprecated + #carbonintensitygb.CarbonIntensityGB(), + ] carbon_intensity = CarbonIntensity(default=True) diff --git a/tests/intensity/test_electricitymaps.py b/tests/intensity/test_electricitymaps.py index 7aa5021..72cc007 100644 --- a/tests/intensity/test_electricitymaps.py +++ b/tests/intensity/test_electricitymaps.py @@ -5,7 +5,8 @@ class TestElectricityMap(unittest.TestCase): def setUp(self): - self.electricity_map = ElectricityMap() + self.logger = MagicMock() + self.electricity_map = ElectricityMap(logger=self.logger) self.g_location = MagicMock() self.g_location.lng = 0.0 self.g_location.lat = 0.0 diff --git a/tests/intensity/test_intensity.py b/tests/intensity/test_intensity.py index a9704a4..76ef709 100644 --- a/tests/intensity/test_intensity.py +++ b/tests/intensity/test_intensity.py @@ -215,7 +215,7 @@ def test_carbon_intensity_exception_carbonintensitygb(self, mock_geocoder, mock_ logger = MagicMock() - result = carbon_intensity(logger) + result = carbon_intensity(logger, fetchers=[mock_carbonintensitygb()]) self.assertEqual(result.carbon_intensity, 23.0) self.assertTrue(result.success) @@ -230,7 +230,7 @@ def test_carbon_intensity_energidataservice(self, mock_energidataservice): mock_energidataservice.return_value.carbon_intensity.return_value = mock_result logger = MagicMock() - result = carbon_intensity(logger) + result = carbon_intensity(logger, fetchers=[mock_energidataservice()]) self.assertEqual(result.carbon_intensity, 23.0) self.assertTrue(result.success)