From a4150e04c1810e56b82991fc6f26181d2be49b9d Mon Sep 17 00:00:00 2001 From: AndrewLester Date: Sun, 23 Apr 2023 15:50:06 -0500 Subject: [PATCH] Add post inverters for site endpoint --- pv_site_api/main.py | 44 ++++++++++++++++++++++++++++++++++++++--- tests/test_inverters.py | 15 +++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/pv_site_api/main.py b/pv_site_api/main.py index e80db9f..be2ad68 100644 --- a/pv_site_api/main.py +++ b/pv_site_api/main.py @@ -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 @@ -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. @@ -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. """ @@ -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() @@ -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() @@ -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)): diff --git a/tests/test_inverters.py b/tests/test_inverters.py index d16885b..123666a 100644 --- a/tests/test_inverters.py +++ b/tests/test_inverters.py @@ -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)