Skip to content

Commit

Permalink
Add post inverters for site endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLester committed Apr 23, 2023
1 parent 9561afc commit a4150e0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
44 changes: 41 additions & 3 deletions pv_site_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pvlib import irradiance, location, pvsystem
from pvsite_datamodel.read.site import get_all_sites
from pvsite_datamodel.read.status import get_latest_status
from pvsite_datamodel.sqlmodels import ClientSQL, SiteSQL
from pvsite_datamodel.sqlmodels import ClientSQL, InverterSQL, SiteSQL
from pvsite_datamodel.write.generation import insert_generation_values
from sqlalchemy.orm import Session

Expand Down Expand Up @@ -402,6 +402,7 @@ def get_pv_estimate_clearsky(
def get_pv_estimate_clearsky_many_sites(
site_uuids: str,
session: Session = Depends(get_session),
auth: Auth = Depends(auth),
):
"""
### Gets a estimate of AC production under a clear sky for multiple sites.
Expand Down Expand Up @@ -460,7 +461,9 @@ def get_pv_estimate_clearsky_many_sites(


@app.get("/enode/link", response_class=RedirectResponse)
def get_enode_link(redirect_uri: str, session: Session = Depends(get_session)):
def get_enode_link(
redirect_uri: str, session: Session = Depends(get_session), auth: Auth = Depends(auth)
):
"""
### Returns a URL from Enode that starts a user's Enode link flow.
"""
Expand All @@ -480,6 +483,7 @@ def get_enode_link(redirect_uri: str, session: Session = Depends(get_session)):
@app.get("/enode/inverters")
async def get_inverters(
session: Session = Depends(get_session),
auth: Auth = Depends(auth),
):
if is_fake():
return make_fake_inverters()
Expand All @@ -498,9 +502,10 @@ async def get_inverters(


@app.get("/sites/{site_uuid}/inverters")
async def get_inverters_by_site(
async def get_inverters_for_site(
site_uuid: str,
session: Session = Depends(get_session),
auth: Auth = Depends(auth),
):
if is_fake():
return make_fake_inverters()
Expand All @@ -520,6 +525,39 @@ async def get_inverters_by_site(
)


@app.post("/sites/{site_uuid}/inverters")
def post_inverters_for_site(
site_uuid: str,
client_ids: list[str],
session: Session = Depends(get_session),
auth: Auth = Depends(auth),
):
if is_fake():
print(f"Successfully changed inverters for {site_uuid}")
print("Not doing anything with it (yet!)")
return

# @TODO: get client corresponding to auth
client = session.query(ClientSQL).first()
assert client is not None

site = (
session.query(SiteSQL)
.filter_by(client_uuid=client.client_uuid, site_uuid=site_uuid)
.first()
)
if site is None:
raise HTTPException(status_code=404, detail="Site not found")

site.inverters.clear()

for client_id in client_ids:
site.inverters.append(InverterSQL(site_uuid=site_uuid, client_id=client_id))

session.add(site)
session.commit()


# get_status: get the status of the system
@app.get("/api_status", response_model=PVSiteAPIStatus)
def get_status(session: Session = Depends(get_session)):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_inverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
enode_api_base_url = os.getenv("ENODE_API_BASE_URL", "https://enode-api.sandbox.enode.io")


def test_get_inverters_from_site(client, sites, inverters, httpx_mock):
def test_post_inverters_for_site(client, sites, httpx_mock):
test_inverter_client_id = "6c078ca2-2e75-40c8-9a7f-288bd0b70065"
json = [test_inverter_client_id]
response = client.post(f"/sites/{sites[0].site_uuid}/inverters", json=json)
assert response.status_code == 200

mock_inverter_response(test_inverter_client_id, httpx_mock)

response = client.get(f"/sites/{sites[0].site_uuid}/inverters")

assert response.json()["inverters"][0]["id"] == test_inverter_client_id


def test_get_inverters_for_site(client, sites, inverters, httpx_mock):
mock_inverter_response("id1", httpx_mock)
mock_inverter_response("id2", httpx_mock)
mock_inverter_response("id3", httpx_mock)
Expand Down

0 comments on commit a4150e0

Please sign in to comment.