Skip to content

Commit

Permalink
feat: add rate structure property (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
stabbylambda authored Nov 1, 2024
1 parent d9cf640 commit 769a7ec
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 61 deletions.
93 changes: 34 additions & 59 deletions openeihttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ def update_data(self) -> None:
self._data = data
_LOGGER.debug("Data updated, results: %s", self._data)

@property
def current_energy_rate_structure(self) -> int | None:
"""Return the current rate structure."""
return self.rate_structure(datetime.datetime.today(), "energy")

def rate_structure(self, date, rate_type) -> int | None:
"""Return the rate structure for a specific date."""
assert self._data is not None
if f"{rate_type}ratestructure" in self._data:
month = date.month - 1
hour = date.hour
weekend = date.weekday() > 4

table = (
f"{rate_type}weekendschedule"
if weekend
else f"{rate_type}weekdayschedule"
)
lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]

return rate_structure
return None

@property
def current_rate(self) -> float | None:
"""Return the current rate."""
Expand All @@ -163,17 +187,8 @@ def current_rate(self) -> float | None:
def rate(self, date) -> float | None:
"""Return the rate for a specific date."""
assert self._data is not None
if "energyratestructure" in self._data:
weekend = False
month = date.month - 1
hour = date.hour
if date.weekday() > 4:
weekend = True
table = "energyweekdayschedule"
if weekend:
table = "energyweekendschedule"
lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]
rate_structure = self.rate_structure(date, "energy")
if rate_structure is not None:
if self._reading:
value = float(self._reading)
rate_data = self._data["energyratestructure"][rate_structure]
Expand All @@ -194,18 +209,9 @@ def current_adjustment(self) -> float | None:
def adjustment(self, date) -> float | None:
"""Return the rate for a specific date."""
assert self._data is not None
if "energyratestructure" in self._data:
rate_structure = self.rate_structure(date, "energy")
if rate_structure is not None:
adj = None
weekend = False
month = date.month - 1
hour = date.hour
if date.weekday() > 4:
weekend = True
table = "energyweekdayschedule"
if weekend:
table = "energyweekendschedule"
lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]
if self._reading:
rate_data = self._data["energyratestructure"][rate_structure]
if "adj" in rate_data[-1]:
Expand All @@ -230,17 +236,8 @@ def tier_rate_for_month(self, date) -> float | None:
Requires the monthy accumulative meter reading.
"""
assert self._data is not None
if "energyratestructure" in self._data:
weekend = False
month = date.month - 1
hour = date.hour
if date.weekday() > 4:
weekend = True
table = "energyweekdayschedule"
if weekend:
table = "energyweekendschedule"
lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]
rate_structure = self.rate_structure(date, "energy")
if rate_structure is not None:
if self._reading:
value = float(self._reading)
rate_data = self._data["energyratestructure"][rate_structure]
Expand Down Expand Up @@ -276,21 +273,9 @@ def current_demand_rate(self) -> float | None:
def demand_rate(self, date) -> float | None:
"""Return the rate for a specific date."""
assert self._data is not None
if "demandratestructure" in self._data:
weekend = False
month = date.month - 1
hour = date.hour
if date.weekday() > 4:
weekend = True
table = "demandweekdayschedule"
if weekend:
table = "demandweekendschedule"

lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]

rate_structure = self.rate_structure(date, "demand")
if rate_structure is not None:
rate = self._data["demandratestructure"][rate_structure][0]["rate"]

return rate
return None

Expand All @@ -302,19 +287,9 @@ def current_demand_adjustment(self) -> float | None:
def demand_adjustment(self, date) -> float | None:
"""Return the rate for a specific date."""
assert self._data is not None
if "demandratestructure" in self._data:
rate_structure = self.rate_structure(date, "demand")
if rate_structure is not None:
adj = None
weekend = False
month = date.month - 1
hour = date.hour
if date.weekday() > 4:
weekend = True
table = "demandweekdayschedule"
if weekend:
table = "demandweekendschedule"

lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]

adj_data = self._data["demandratestructure"][rate_structure][0]
if "adj" in adj_data:
Expand Down
18 changes: 16 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,22 @@ def test_get_lookup_data_radius(test_lookup_radius, lookup_mock_radius, caplog):
def test_get_tier_rate_data_low(test_lookup_tier_low, tier_plandata_mock):
"""Test rate schedules."""
test_lookup_tier_low.update()
status = test_lookup_tier_low.current_rate
assert status == 0.25902
rate = test_lookup_tier_low.current_rate
struture = test_lookup_tier_low.current_energy_rate_structure
assert rate == 0.25902
assert struture == 0


@freeze_time(
"2021-11-01 10:21:34"
) # November 1 is the first day of a separate rate structure for this plan
def test_get_tier_rate_data_low_second_period(test_lookup_tier_low, tier_plandata_mock):
"""Test rate schedules."""
test_lookup_tier_low.update()
rate = test_lookup_tier_low.current_rate
structure = test_lookup_tier_low.current_energy_rate_structure
assert rate == 0.25902
assert structure == 1


@freeze_time("2021-08-13 10:21:34")
Expand Down

0 comments on commit 769a7ec

Please sign in to comment.