Skip to content

Commit

Permalink
Fix issue with no gas prices in the morning (#16)
Browse files Browse the repository at this point in the history
* Fix issue with gas reading in the morning

* Remove the else block

* Put back in else block
  • Loading branch information
klaasnicolaas authored Dec 30, 2022
1 parent 08ca1aa commit df84b27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
22 changes: 12 additions & 10 deletions energyzero/energyzero.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,23 @@ async def gas_prices(
"""
start_date_utc: datetime
end_date_utc: datetime
if get_utcnow().hour < 5:
# Set start_date to 05:00:00 prev day and the end_date to 04:59:59 UTC
if get_utcnow().hour >= 5 and get_utcnow().hour <= 22:
# Set start_date to 05:00:00 and the end_date to 04:59:59 UTC next day
start_date_utc = datetime(
start_date.year, start_date.month, start_date.day - 1, 5, 0, 0
start_date.year, start_date.month, start_date.day, 5, 0, 0
)
end_date_utc = datetime(
end_date.year, end_date.month, end_date.day, 4, 59, 59
) + timedelta(days=1)
else:
# Set start_date to 05:00:00 prev day and the end_date to 04:59:59 UTC
start_date_utc = datetime(
start_date.year, start_date.month, start_date.day, 5, 0, 0
) - timedelta(days=1)
end_date_utc = datetime(
end_date.year, end_date.month, end_date.day, 4, 59, 59
)
# Set start_date to 05:00:00 and the end_date to 04:59:59 UTC next day
start_date_utc = datetime(
start_date.year, start_date.month, start_date.day, 5, 0, 0
)
end_date_utc = datetime(
end_date.year, end_date.month, end_date.day + 1, 4, 59, 59
)

data = await self._request(
"energyprices",
params={
Expand Down
6 changes: 3 additions & 3 deletions test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
async def main() -> None:
"""Show example on fetching the energy prices from EnergyZero."""
async with EnergyZero() as client:
local = pytz.timezone("Europe/Amsterdam")
today = date(2022, 12, 28)
tomorrow = date(2022, 12, 28)
local = pytz.timezone("CET")
today = date(2022, 12, 30)
tomorrow = date(2022, 12, 30)

# Select your test readings
energy_reading: bool = True
Expand Down
6 changes: 3 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ async def test_no_electricity_data(aresponses: ResponsesMockServer) -> None:


@pytest.mark.asyncio
@patch(
"energyzero.energyzero.get_utcnow", Mock(return_value=datetime(2022, 12, 7, 14, 0))
)
@patch(
"energyzero.models.Gas.utcnow",
Mock(return_value=datetime(2022, 12, 7, 14, 0).replace(tzinfo=timezone.utc)),
Expand Down Expand Up @@ -128,9 +131,6 @@ async def test_gas_model(aresponses: ResponsesMockServer) -> None:


@pytest.mark.asyncio
@patch(
"energyzero.energyzero.get_utcnow", Mock(return_value=datetime(2022, 12, 7, 4, 0))
)
@patch(
"energyzero.models.Gas.utcnow",
Mock(return_value=datetime(2022, 12, 7, 5, 0).replace(tzinfo=timezone.utc)),
Expand Down

0 comments on commit df84b27

Please sign in to comment.