Skip to content

Commit

Permalink
add guards for missing data (#8)
Browse files Browse the repository at this point in the history
* update tests

* fix test

* fix test round 2

* add guards for missing data
  • Loading branch information
firstof9 authored Sep 8, 2021
1 parent 9caee90 commit 8793de1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
86 changes: 48 additions & 38 deletions openeihttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,63 +95,71 @@ def update(self) -> None:
self._data = data

@property
def current_rate(self) -> float:
def current_rate(self) -> float | None:
"""Return the current rate."""
assert self._data is not None
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"
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]
lookup_table = self._data[table]
rate_structure = lookup_table[month][hour]

rate = self._data["energyratestructure"][rate_structure][0]["rate"]
rate = self._data["energyratestructure"][rate_structure][0]["rate"]

return rate
return rate
return None

@property
def all_rates(self) -> list:
def all_rates(self) -> list | None:
"""Return the current rate."""
assert self._data is not None
rates = []
rate_data = self._data["energyratestructure"]
for rate in rate_data:
rates.append(rate[0]["rate"])
if "energyratestructure" in self._data.keys():
rates = []
rate_data = self._data["energyratestructure"]
for rate in rate_data:
rates.append(rate[0]["rate"])

return rates
return rates
return None

@property
def current_demand_rate(self) -> float:
def current_demand_rate(self) -> float | None:
"""Return the current rate."""
assert self._data is not None
weekend = False
now = datetime.datetime.today()
month = now.month - 1
hour = now.hour
if now.weekday() > 4:
weekend = True
table = "demandweekdayschedule"
if weekend:
table = "demandweekendschedule"
if "demandratestructure" in self._data.keys():
weekend = False
now = datetime.datetime.today()
month = now.month - 1
hour = now.hour
if now.weekday() > 4:
weekend = True
table = "demandweekdayschedule"
if weekend:
table = "demandweekendschedule"

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

rate = self._data["demandratestructure"][rate_structure][0]["rate"]
rate = self._data["demandratestructure"][rate_structure][0]["rate"]

return rate
return rate
return None

@property
def demand_unit(self) -> str:
def demand_unit(self) -> str | None:
"""Return the demand rate unit."""
assert self._data is not None
return self._data["demandrateunit"]
if "demandrateunit" in self._data.keys():
return self._data["demandrateunit"]
return None

@property
def rate_name(self) -> str:
Expand All @@ -166,7 +174,9 @@ def approval(self) -> bool:
return self._data["approved"]

@property
def distributed_generation(self) -> str:
def distributed_generation(self) -> str | None:
"""Return the distributed generation name."""
assert self._data is not None
return self._data["dgrules"]
if "dgrules" in self._data.keys():
return self._data["dgrules"]
return None
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.7"
VERSION = "0.1.8"


setup(
Expand Down

0 comments on commit 8793de1

Please sign in to comment.