From 333feed957a74cc3b4766b03a4f5f12f1243772f Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 9 Sep 2021 11:19:58 -0700 Subject: [PATCH] add monthly tier rate property (#10) * add monthly tier rate property * linting --- openeihttp/__init__.py | 30 ++++++++++++++++++++++++++++++ setup.py | 2 +- tests/conftest.py | 39 +++++++++++++++++++++++++++++++++++++++ tests/test_init.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/openeihttp/__init__.py b/openeihttp/__init__.py index 03506a3..e617d5e 100644 --- a/openeihttp/__init__.py +++ b/openeihttp/__init__.py @@ -124,6 +124,36 @@ def current_rate(self) -> float | None: return rate return None + @property + def monthly_tier_rate(self) -> float | None: + """Return tier rate. + + Requires the monthy accumulative meter reading. + """ + assert self._data is not None + if "energyratestructure" in self._data.keys(): + weekend = False + now = datetime.datetime.today() + month = now.month - 1 + hour = now.hour + if now.weekday() > 4: + weekend = True + table = "energyweekdayschedule" + if weekend: + table = "energyweekendschedule" + lookup_table = self._data[table] + rate_structure = lookup_table[month][hour] + if self._reading: + value = float(self._reading) + rate_data = self._data["energyratestructure"][rate_structure] + for rate in rate_data: + if "max" in rate.keys() and value < (rate["max"] * 29): + return rate["rate"] + continue + return rate_data[-1]["rate"] + return None + return None + @property def all_rates(self) -> list | None: """Return the current rate.""" diff --git a/setup.py b/setup.py index 68ea8da..c13fb58 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ PROJECT_DIR = Path(__file__).parent.resolve() README_FILE = PROJECT_DIR / "README.md" -VERSION = "0.1.9" +VERSION = "0.1.10" setup( diff --git a/tests/conftest.py b/tests/conftest.py index 4a62df0..a528bf0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -56,6 +56,45 @@ def test_lookup_tier_high(): ) +@pytest.fixture(name="test_lookup_monthly_tier_low") +def test_lookup_monthly_tier_low(): + """Load the charger data.""" + return openeihttp.Rates( + api="fakeAPIKey", + lat="1", + lon="1", + radius="20", + reading="114", + plan="574613aa5457a3557e906f5b", + ) + + +@pytest.fixture(name="test_lookup_monthly_tier_med") +def test_lookup_monthly_tier_med(): + """Load the charger data.""" + return openeihttp.Rates( + api="fakeAPIKey", + lat="1", + lon="1", + radius="20", + reading="301", + plan="574613aa5457a3557e906f5b", + ) + + +@pytest.fixture(name="test_lookup_monthly_tier_high") +def test_lookup_monthly_tier_high(): + """Load the charger data.""" + return openeihttp.Rates( + api="fakeAPIKey", + lat="1", + lon="1", + radius="20", + reading="1300", + plan="574613aa5457a3557e906f5b", + ) + + @pytest.fixture(name="test_rates") def test_rates(): """Load the charger data.""" diff --git a/tests/test_init.py b/tests/test_init.py index 505e23e..9d25344 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -667,3 +667,33 @@ def test_get_tier_rate_data_weekend(test_lookup_tier_low, tier_plandata_mock): test_lookup_tier_low.update() status = test_lookup_tier_low.current_rate assert status == 0.25902 + + +@freeze_time("2021-08-13 10:21:34") +def test_get_monthly_tier_rate_data_low( + test_lookup_monthly_tier_low, tier_plandata_mock +): + """Test rate schedules.""" + test_lookup_monthly_tier_low.update() + status = test_lookup_monthly_tier_low.monthly_tier_rate + assert status == 0.25902 + + +@freeze_time("2021-08-13 10:21:34") +def test_get_monthly_tier_rate_data_med( + test_lookup_monthly_tier_med, tier_plandata_mock +): + """Test rate schedules.""" + test_lookup_monthly_tier_med.update() + status = test_lookup_monthly_tier_med.monthly_tier_rate + assert status == 0.32596 + + +@freeze_time("2021-08-13 10:21:34") +def test_get_monthly_tier_rate_data_high( + test_lookup_monthly_tier_high, tier_plandata_mock +): + """Test rate schedules.""" + test_lookup_monthly_tier_high.update() + status = test_lookup_monthly_tier_high.monthly_tier_rate + assert status == 0.40745