Skip to content

Commit

Permalink
Merge pull request #82 from Snailed/dev
Browse files Browse the repository at this point in the history
minor: Deprecated EnergiDataService and CarbonIntensityGB
  • Loading branch information
Snailed authored Sep 13, 2024
2 parents 69b4087 + adc8969 commit 74a4bd0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
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"


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)
Expand Down
13 changes: 7 additions & 6 deletions carbontracker/emissions/intensity/intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion tests/intensity/test_electricitymaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/intensity/test_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 74a4bd0

Please sign in to comment.