Skip to content

Commit

Permalink
Merge pull request #15 from hudsonbrendon/feat/records
Browse files Browse the repository at this point in the history
feat: add method records
  • Loading branch information
hudsonbrendon authored Jan 27, 2025
2 parents 85e2cb8 + a1518ca commit f729057
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 11 deletions.
31 changes: 21 additions & 10 deletions solar_plus_intelbras/enums.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
from enum import Enum


class MethodEnum(Enum):
"""A enum with the methods available in the API.
class EndpointEnum(Enum):
"""A enum with the endpoints available in the API.
Args:
Enum (_type_): A class to represent a enumeration.
"""

GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
LOGIN = "login"
PLANTS = "plants"
RECORDS = "records"


class EndpointEnum(Enum):
"""A enum with the endpoints available in the API.
class PeriodEnum(Enum):
"""A enum with the periods available in the API.
Args:
Enum (_type_): A class to represent a enumeration.
"""

LOGIN = "login"
PLANTS = "plants"
DAY = "day"
MONTH = "month"
YEAR = "year"


class KeyEnum(Enum):
"""A enum with the keys available in the API.
Args:
Enum (_type_): A class to represent a enumeration.
"""

PAC = "pac"
ENERGY_TODAY = "energy_today"
53 changes: 52 additions & 1 deletion solar_plus_intelbras/solar_plus_intelbras.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from datetime import datetime, timezone
from typing import Optional

import requests
from pydantic import EmailStr

from solar_plus_intelbras.enums import EndpointEnum
from solar_plus_intelbras.enums import EndpointEnum, KeyEnum, PeriodEnum


class SolarPlusIntelbras:
Expand All @@ -17,6 +18,9 @@ def __init__(self, email: EmailStr, plus: str) -> None:
Args:
email (EmailStr): A valid email address.
plus (str): A string.
Returns:
None: The constructor does not return anything.
"""
self.__email = email
self.__plus = plus
Expand Down Expand Up @@ -124,3 +128,50 @@ def plants(self) -> dict:
headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
)
return response.json()

def records(
self,
period: PeriodEnum,
key: Optional[KeyEnum] = None,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
) -> dict:
"""Return the records.
Args:
period (PeriodEnum): A period.
key (Optional[KeyEnum], optional): A key. Defaults to None.
start_date (Optional[str], optional): A start date. Defaults to None.
end_date (Optional[str], optional): An end date. Defaults to None.
Returns:
dict: A dictionary with the records.
"""
params = {}

if period:
params["period"] = period

if key:
params["key"] = key

if start_date:
try:
datetime.strptime(start_date, "%Y-%m-%d")
params["start_date"] = start_date
except ValueError:
raise ValueError("start_date must be in the format YYYY-MM-DD.")

if end_date:
try:
datetime.strptime(end_date, "%Y-%m-%d")
params["end_date"] = end_date
except ValueError:
raise ValueError("end_date must be in the format YYYY-MM-DD.")

response = requests.get(
f"{self.base_api_url}{EndpointEnum.RECORDS.value}",
headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
params=params,
)
return response.json()
149 changes: 149 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,152 @@ def plants() -> dict:
}
],
}


@pytest.fixture
def records_today() -> dict:
return {
"data": {
"rows": [
{"date": "2025-01-23 05:25", "total": 0},
{"date": "2025-01-23 05:30", "total": 0},
{"date": "2025-01-23 05:35", "total": 0},
{"date": "2025-01-23 05:40", "total": 98},
{"date": "2025-01-23 05:45", "total": 98},
{"date": "2025-01-23 05:50", "total": 142},
{"date": "2025-01-23 05:55", "total": 176},
{"date": "2025-01-23 06:00", "total": 176},
{"date": "2025-01-23 06:05", "total": 220},
{"date": "2025-01-23 06:10", "total": 220},
{"date": "2025-01-23 06:15", "total": 258},
{"date": "2025-01-23 06:20", "total": 258},
{"date": "2025-01-23 06:25", "total": 294},
{"date": "2025-01-23 06:30", "total": 342},
{"date": "2025-01-23 06:35", "total": 342},
{"date": "2025-01-23 06:40", "total": 471},
{"date": "2025-01-23 06:45", "total": 471},
{"date": "2025-01-23 06:50", "total": 375},
{"date": "2025-01-23 06:55", "total": 375},
{"date": "2025-01-23 07:00", "total": 810},
{"date": "2025-01-23 07:05", "total": 810},
{"date": "2025-01-23 07:10", "total": 511},
{"date": "2025-01-23 07:15", "total": 1334},
{"date": "2025-01-23 07:20", "total": 1334},
{"date": "2025-01-23 07:25", "total": 1247},
{"date": "2025-01-23 07:30", "total": 1247},
{"date": "2025-01-23 07:35", "total": 2044},
{"date": "2025-01-23 07:40", "total": 2044},
{"date": "2025-01-23 07:45", "total": 968},
{"date": "2025-01-23 07:50", "total": 2632},
{"date": "2025-01-23 07:55", "total": 2632},
{"date": "2025-01-23 08:00", "total": 1891},
{"date": "2025-01-23 08:05", "total": 1891},
{"date": "2025-01-23 08:10", "total": 1121},
{"date": "2025-01-23 08:15", "total": 1121},
{"date": "2025-01-23 08:20", "total": 966},
{"date": "2025-01-23 08:25", "total": 1114},
{"date": "2025-01-23 08:30", "total": 1114},
{"date": "2025-01-23 08:35", "total": 3615},
{"date": "2025-01-23 08:40", "total": 3615},
{"date": "2025-01-23 08:45", "total": 3660},
{"date": "2025-01-23 08:50", "total": 3660},
{"date": "2025-01-23 08:55", "total": 1086},
{"date": "2025-01-23 09:00", "total": 1086},
{"date": "2025-01-23 09:05", "total": 4194},
{"date": "2025-01-23 09:10", "total": 4194},
{"date": "2025-01-23 09:15", "total": 1390},
{"date": "2025-01-23 09:20", "total": 1352},
{"date": "2025-01-23 09:25", "total": 1352},
{"date": "2025-01-23 09:30", "total": 1047},
{"date": "2025-01-23 09:35", "total": 1047},
{"date": "2025-01-23 09:40", "total": 3334},
{"date": "2025-01-23 09:45", "total": 4275},
{"date": "2025-01-23 09:50", "total": 4275},
{"date": "2025-01-23 09:55", "total": 4224},
{"date": "2025-01-23 10:00", "total": 4224},
{"date": "2025-01-23 10:05", "total": 1617},
{"date": "2025-01-23 10:10", "total": 1617},
{"date": "2025-01-23 10:15", "total": 5009},
{"date": "2025-01-23 10:20", "total": 3338},
{"date": "2025-01-23 10:25", "total": 0},
{"date": "2025-01-23 10:30", "total": 3338},
{"date": "2025-01-23 10:35", "total": 1102},
{"date": "2025-01-23 10:40", "total": 1102},
{"date": "2025-01-23 10:45", "total": 1391},
{"date": "2025-01-23 10:50", "total": 1391},
{"date": "2025-01-23 10:55", "total": 4800},
{"date": "2025-01-23 11:00", "total": 4800},
{"date": "2025-01-23 11:05", "total": 2246},
{"date": "2025-01-23 11:10", "total": 2246},
{"date": "2025-01-23 11:15", "total": 4769},
{"date": "2025-01-23 11:20", "total": 2746},
{"date": "2025-01-23 11:25", "total": 2746},
{"date": "2025-01-23 11:30", "total": 4540},
{"date": "2025-01-23 11:35", "total": 4540},
{"date": "2025-01-23 11:40", "total": 4477},
{"date": "2025-01-23 11:45", "total": 4477},
{"date": "2025-01-23 11:50", "total": 4481},
{"date": "2025-01-23 11:55", "total": 4481},
{"date": "2025-01-23 12:00", "total": 4596},
{"date": "2025-01-23 12:05", "total": 4596},
{"date": "2025-01-23 12:10", "total": 4481},
{"date": "2025-01-23 12:15", "total": 4417},
{"date": "2025-01-23 12:20", "total": 4417},
{"date": "2025-01-23 12:25", "total": 0},
{"date": "2025-01-23 12:30", "total": 4137},
{"date": "2025-01-23 12:35", "total": 4161},
{"date": "2025-01-23 12:40", "total": 4161},
{"date": "2025-01-23 12:45", "total": 4177},
{"date": "2025-01-23 12:50", "total": 4177},
{"date": "2025-01-23 12:55", "total": 4123},
{"date": "2025-01-23 13:00", "total": 0},
{"date": "2025-01-23 13:05", "total": 0},
{"date": "2025-01-23 13:10", "total": 3993},
{"date": "2025-01-23 13:15", "total": 3993},
{"date": "2025-01-23 13:20", "total": 3879},
{"date": "2025-01-23 13:25", "total": 0},
{"date": "2025-01-23 13:30", "total": 0},
{"date": "2025-01-23 13:35", "total": 0},
{"date": "2025-01-23 13:40", "total": 3678},
{"date": "2025-01-23 13:45", "total": 3678},
{"date": "2025-01-23 13:50", "total": 3591},
{"date": "2025-01-23 13:55", "total": 3591},
{"date": "2025-01-23 14:00", "total": 3490},
{"date": "2025-01-23 14:05", "total": 3406},
{"date": "2025-01-23 14:10", "total": 3406},
{"date": "2025-01-23 14:15", "total": 3307},
{"date": "2025-01-23 14:20", "total": 3307},
{"date": "2025-01-23 14:25", "total": 3149},
{"date": "2025-01-23 14:30", "total": 3149},
{"date": "2025-01-23 14:35", "total": 3031},
{"date": "2025-01-23 14:40", "total": 2886},
{"date": "2025-01-23 14:45", "total": 2886},
{"date": "2025-01-23 14:50", "total": 2726},
{"date": "2025-01-23 14:55", "total": 2726},
{"date": "2025-01-23 15:00", "total": 2560},
{"date": "2025-01-23 15:05", "total": 2560},
{"date": "2025-01-23 15:10", "total": 2440},
{"date": "2025-01-23 15:15", "total": 2440},
{"date": "2025-01-23 15:20", "total": 2265},
{"date": "2025-01-23 15:25", "total": 2265},
{"date": "2025-01-23 15:30", "total": 2089},
{"date": "2025-01-23 15:35", "total": 1780},
{"date": "2025-01-23 15:40", "total": 1780},
{"date": "2025-01-23 15:45", "total": 1466},
{"date": "2025-01-23 15:50", "total": 1466},
{"date": "2025-01-23 15:55", "total": 1305},
{"date": "2025-01-23 16:00", "total": 1133},
{"date": "2025-01-23 16:05", "total": 1133},
{"date": "2025-01-23 16:10", "total": 964},
{"date": "2025-01-23 16:15", "total": 964},
{"date": "2025-01-23 16:20", "total": 823},
{"date": "2025-01-23 16:25", "total": 823},
{"date": "2025-01-23 16:30", "total": 640},
{"date": "2025-01-23 16:35", "total": 640},
{"date": "2025-01-23 16:40", "total": 469},
{"date": "2025-01-23 16:45", "total": 352},
],
"total": 352,
},
"status": 200,
}
55 changes: 55 additions & 0 deletions tests/test_solar_plus_intelbras.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from requests_mock import Mocker

from solar_plus_intelbras.enums import KeyEnum, PeriodEnum
from solar_plus_intelbras.solar_plus_intelbras import SolarPlusIntelbras


Expand Down Expand Up @@ -200,3 +201,57 @@ def test_shouldnt_return_plants_without_authentication(
) -> None:
with pytest.raises(Exception):
solar_plus_intelbras.plants()


class TestSolarPlusIntelbrasRecords:
def test_should_return_records_today(
self,
requests_mock: Mocker,
solar_plus_intelbras: SolarPlusIntelbras,
records_today: dict,
login_response: dict,
) -> None:
requests_mock.post(
"https://ens-server.intelbras.com.br/api/login",
json=login_response,
status_code=200,
)

requests_mock.get(
"https://ens-server.intelbras.com.br/api/records",
json=records_today,
status_code=200,
)
assert (
solar_plus_intelbras.records(
period=PeriodEnum.DAY.value,
key=KeyEnum.PAC.value,
start_date="2025-01-23",
end_date="2025-01-23",
)
== records_today
)

def test_shouldnt_return_records_with_start_date_invalid(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.records(
period=PeriodEnum.DAY.value,
key=KeyEnum.PAC.value,
start_date="2025-01-23",
)
assert str(exc.value) == "start_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_records_with_end_date_invalid(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.records(
period=PeriodEnum.DAY.value,
key=KeyEnum.PAC.value,
end_date="2025-01-23",
)
assert str(exc.value) == "end_date must be in the format YYYY-MM-DD."

0 comments on commit f729057

Please sign in to comment.