Skip to content

Commit

Permalink
add address lookup ability (#11)
Browse files Browse the repository at this point in the history
* add address lookup ability

Address can be city,state, zip code, or full street address.

* update tests
  • Loading branch information
firstof9 authored Sep 10, 2021
1 parent 333feed commit da276d1
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 7 deletions.
25 changes: 19 additions & 6 deletions openeihttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class Rates:
def __init__(
self,
api: str,
lat: float,
lon: float,
lat: float = None,
lon: float = None,
plan: str = None,
radius: float = None,
address: str = None,
reading: float = None,
) -> None:
"""Initialize."""
Expand All @@ -41,15 +42,22 @@ def __init__(
self._plan = plan
self._radius = radius
self._reading = reading
self._address = address
self._data = None

def lookup_plans(self) -> Dict[str, Any]:
"""Return the rate plan names per utility in the area."""
url = f"{BASE_URL}version=latest&format=json"
url = f"{url}&api_key={self._api}&lat={self._lat}&lon={self._lon}"
url = f"{url}&api_key={self._api}"
url = f"{url}&sector=Residential"
if self._radius is not None:
url = f"{url}&radius={self._radius}"

if self._address is None:
url = f"{url}&lat={self._lat}&lon={self._lon}"
else:
url = f"{url}&address={self._address}"

rate_names: Dict[str, Any] = {}

result = requests.get(url)
Expand All @@ -75,12 +83,17 @@ def lookup_plans(self) -> Dict[str, Any]:

def update(self) -> None:
"""Update the data."""
url = f"{BASE_URL}version=latest&format=json"
url = f"{url}&api_key={self._api}&lat={self._lat}&lon={self._lon}"
url = f"{url}&sector=Residential&detail=full&getpage={self._plan}"
url = f"{BASE_URL}version=latest&format=json&sector=Residential"
url = f"{url}&detail=full&api_key={self._api}"
url = f"{url}&getpage={self._plan}"
if self._radius is not None:
url = f"{url}&radius={self._radius}"

if self._address is None:
url = f"{url}&lat={self._lat}&lon={self._lon}"
else:
url = f"{url}&address={self._address}"

result = requests.get(url)
if result.status_code == 404:
raise UrlNotFound
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.10"
VERSION = "0.1.11"


setup(
Expand Down
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_lookup():
return openeihttp.Rates(api="fakeAPIKey", lat="1", lon="1")


@pytest.fixture(name="test_lookup_address")
def test_lookup_address():
"""Load the charger data."""
return openeihttp.Rates(api="fakeAPIKey", address="12345")


@pytest.fixture(name="test_lookup_radius")
def test_lookup_radius():
"""Load the charger data."""
Expand Down Expand Up @@ -103,6 +109,14 @@ def test_rates():
)


@pytest.fixture(name="test_rates_address")
def test_rates_address():
"""Load the charger data."""
return openeihttp.Rates(
api="fakeAPIKey", address="12345", plan="574613aa5457a3557e906f5b"
)


@pytest.fixture(name="lookup_mock")
def mock_lookup(requests_mock):
"""Mock the status reply."""
Expand All @@ -112,6 +126,15 @@ def mock_lookup(requests_mock):
)


@pytest.fixture(name="lookup_mock_address")
def mock_lookup_address(requests_mock):
"""Mock the status reply."""
requests_mock.get(
"https://api.openei.org/utility_rates?version=latest&format=json&api_key=fakeAPIKey&address=12345&sector=Residential",
text=load_fixture("lookup.json"),
)


@pytest.fixture(name="lookup_mock_radius")
def mock_lookup_radius(requests_mock):
"""Mock the status reply."""
Expand All @@ -130,6 +153,15 @@ def mock_plandata(requests_mock):
)


@pytest.fixture(name="plandata_mock_address")
def mock_plandata_address(requests_mock):
"""Mock the status reply."""
requests_mock.get(
"https://api.openei.org/utility_rates?version=latest&format=json&api_key=fakeAPIKey&address=12345&sector=Residential&detail=full&getpage=574613aa5457a3557e906f5b",
text=load_fixture("plan_data.json"),
)


@pytest.fixture(name="demand_plandata_mock")
def mock_demand_plandata(requests_mock):
"""Mock the status reply."""
Expand Down
142 changes: 142 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,145 @@ def test_get_monthly_tier_rate_data_high(
test_lookup_monthly_tier_high.update()
status = test_lookup_monthly_tier_high.monthly_tier_rate
assert status == 0.40745


def test_get_lookup_data_with_address(test_lookup_address, lookup_mock_address):
"""Test v4 Status reply"""
status = test_lookup_address.lookup_plans()
assert status == {
"Arizona Public Service Co": [
{"name": "Time Advantage 7pm to noon", "label": "539fba68ec4f024bc1dc2db3"},
{
"name": "Residential Bundled & Adjustments No Tax TOU; Time Adv Super peak 7PM-Noon (ET-SP)",
"label": "542aec8a5257a310694b1ab6",
},
{
"name": "Residential Service Bundled Standard rate",
"label": "55563ddf5457a3bf6c8b4570",
},
{
"name": "Residential Bundled & Adjustments No Tax TOU ; Time Adv 7PM-Noon (ET-2)",
"label": "55563df55457a35e758b4568",
},
{
"name": "Residential Bundled & Adjustments No Tax TOU w/ Demand; Combined Adv 7PM-Noon (ECT-2)",
"label": "562bea025457a31838418740",
},
{
"name": "Residential Service Bundled Standard rate",
"label": "5743aa135457a3a636906f5b",
},
{
"name": "Residential Service TOU Time Advantage 7PM-Noon (ET-2)",
"label": "574613aa5457a3557e906f5b",
},
{
"name": "Residential Service TOU Combined Advantage 7PM-Noon (ECT-2)",
"label": "574615f85457a3557e906f5c",
},
{
"name": "Residential Service Standard (E-12)",
"label": "5748ab725457a37e7d906f5b",
},
{
"name": "Residential Service TOU Time Advantage 7PM-Noon (ET-2)",
"label": "5748ac7a5457a37d7d906f5b",
},
{
"name": "Residential Service Standard (E-12)",
"label": "5881345e5457a35973316ce7",
},
{
"name": "Residential Service TOU Time Advantage 7PM-Noon (ET-2)",
"label": "588135ee5457a3873f316ce6",
},
{
"name": "Residential Service TOU Combined Advantage 7PM-Noon (ECT-2)",
"label": "5890c92f5457a36c373dbec0",
},
{
"name": "Residential Service Standard (E-12) Low Income",
"label": "59fcde8e5457a3a04cc05084",
},
{
"name": "Residential Service Standard (E-12) [Frozen]",
"label": "5a592dee5457a3931d423a81",
},
{
"name": "Residential Service TOU Time Advantage 7PM-Noon (ET-2) [Frozen]",
"label": "5a5930055457a36274423a7d",
},
{
"name": "Residential Service TOU Combined Advantage 7PM-Noon (ECT-2) [Frozen]",
"label": "5a5930bf5457a3371c423a7e",
},
{
"name": "Residential Time of Use (Saver Choice)",
"label": "5a5933d25457a3ad69423a81",
},
{
"name": "Residential Service (Saver Choice Plus)",
"label": "5a5935475457a3ad69423a82",
},
{
"name": "Residential Service (Saver Choice Max)",
"label": "5a5936ef5457a36274423a7f",
},
{
"name": "Extra Small Residential Service (Lite Choice)",
"label": "5a5937bb5457a3b71c423a7f",
},
{
"name": "Small Residential Service (Premier Choice)",
"label": "5a59388c5457a3931d423a84",
},
{
"name": "Large Residential Service (Premier Choice Large)",
"label": "5a5939515457a3371c423a7f",
},
{
"name": "Residential Service (Pilot Technology Rate)",
"label": "5a593d815457a3371c423a80",
},
{
"name": "Extra Small Residential Service (Lite Choice) R-XS",
"label": "5cacc5605457a379257780e2",
},
{
"name": "Small Residential Service (Premier Choice) R-Basic",
"label": "5cacc6545457a3634c7780e2",
},
{
"name": "Large Residential Service (Premier Choice Large) R-Basic L [Frozen]",
"label": "5cacc6e75457a3a8497780e2",
},
{
"name": "Residential Time of Use (Saver Choice) TOU-E",
"label": "5cacc7715457a393487780e2",
},
{
"name": "Residential Service (Saver Choice Max)",
"label": "5cacc9ae5457a3c6517780e2",
},
{
"name": "Residential Service (Saver Choice Plus) R-2",
"label": "5cacc9d15457a31d537780e2",
},
{
"name": "Residential Service (Saver Choice Max) R-3",
"label": "5caccb2f5457a3c8517780e2",
},
{
"name": "Residential Service (Pilot Technology Rate) R-Tech",
"label": "5caccd925457a31a537780e2",
},
]
}


@freeze_time("2021-08-13 10:21:34")
def test_get_rate_data_address(test_rates_address, plandata_mock_address):
"""Test rate schedules."""
test_rates_address.update()
status = test_rates_address.current_rate
assert status == 0.06118

0 comments on commit da276d1

Please sign in to comment.