Skip to content

Commit

Permalink
add monthly tier rate property (#10)
Browse files Browse the repository at this point in the history
* add monthly tier rate property

* linting
  • Loading branch information
firstof9 authored Sep 9, 2021
1 parent adcd862 commit 333feed
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
30 changes: 30 additions & 0 deletions openeihttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
39 changes: 39 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
30 changes: 30 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 333feed

Please sign in to comment.