Skip to content

Commit

Permalink
add support for tier rate lookups (#9)
Browse files Browse the repository at this point in the history
* update tests

* fix test

* fix test round 2

* add support for tier rate lookups

* linting
  • Loading branch information
firstof9 authored Sep 8, 2021
1 parent 8793de1 commit adcd862
Show file tree
Hide file tree
Showing 5 changed files with 794 additions and 4 deletions.
13 changes: 10 additions & 3 deletions openeihttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ def __init__(
lon: float,
plan: str = None,
radius: float = None,
reading: float = None,
) -> None:
"""Initialize."""
self._api = api
self._lat = lat
self._lon = lon
self._plan = plan
self._radius = radius
self._reading = reading
self._data = None

def lookup_plans(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -108,12 +110,17 @@ def current_rate(self) -> float | None:
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"]:
return rate["rate"]
continue
return rate_data[-1]["rate"]
rate = self._data["energyratestructure"][rate_structure][0]["rate"]

return rate
return None

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.8"
VERSION = "0.1.9"


setup(
Expand Down
48 changes: 48 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@ def test_lookup_radius():
return openeihttp.Rates(api="fakeAPIKey", lat="1", lon="1", radius="20")


@pytest.fixture(name="test_lookup_tier_low")
def test_lookup_tier_low():
"""Load the charger data."""
return openeihttp.Rates(
api="fakeAPIKey",
lat="1",
lon="1",
radius="20",
reading="5.1",
plan="574613aa5457a3557e906f5b",
)


@pytest.fixture(name="test_lookup_tier_med")
def test_lookup_tier_med():
"""Load the charger data."""
return openeihttp.Rates(
api="fakeAPIKey",
lat="1",
lon="1",
radius="20",
reading="10.3",
plan="574613aa5457a3557e906f5b",
)


@pytest.fixture(name="test_lookup_tier_high")
def test_lookup_tier_high():
"""Load the charger data."""
return openeihttp.Rates(
api="fakeAPIKey",
lat="1",
lon="1",
radius="20",
reading="40.1",
plan="574613aa5457a3557e906f5b",
)


@pytest.fixture(name="test_rates")
def test_rates():
"""Load the charger data."""
Expand Down Expand Up @@ -61,6 +100,15 @@ def mock_demand_plandata(requests_mock):
)


@pytest.fixture(name="tier_plandata_mock")
def mock_tier_plandata(requests_mock):
"""Mock the status reply."""
requests_mock.get(
"https://api.openei.org/utility_rates?version=latest&format=json&api_key=fakeAPIKey&lat=1&lon=1&sector=Residential&detail=full&getpage=574613aa5457a3557e906f5b",
text=load_fixture("plan_tier_data.json"),
)


@pytest.fixture(name="lookup_mock_404")
def mock_lookup_404(requests_mock):
"""Mock the status reply."""
Expand Down
Loading

0 comments on commit adcd862

Please sign in to comment.