Skip to content

Commit

Permalink
🎨 lint: Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azogue committed Mar 10, 2024
1 parent 0345a71 commit 368a167
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
17 changes: 9 additions & 8 deletions aiopvpc/prices.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
"""ESIOS API handler for HomeAssistant. Hourly price attributes."""

import zoneinfo
from contextlib import suppress
from datetime import datetime
from typing import Any

from .const import EsiosApiData, KEY_ADJUSTMENT, KEY_INDEXED, KEY_INJECTION, KEY_PVPC


def _is_tomorrow_price(ts: datetime, ref: datetime) -> bool:
return any(map(lambda x: x[0] > x[1], zip(ts.isocalendar(), ref.isocalendar())))
return any(
ts_comp > ts_tz_ref
for ts_comp, ts_tz_ref in zip(ts.isocalendar(), ref.isocalendar())
)


def _split_today_tomorrow_prices(
Expand Down Expand Up @@ -65,29 +69,26 @@ def _make_price_stats_attributes(
attributes["hours_to_better_price"] = int(delta_better.total_seconds()) // 3600
attributes["num_better_prices_ahead"] = len(better_prices_ahead)

try:
with suppress(ValueError):
attributes["price_position"] = (
list(prices_sorted.values()).index(current_price) + 1
)
except ValueError:
pass

max_price = max(current_prices.values())
min_price = min(current_prices.values())
try:
with suppress(ZeroDivisionError):
attributes["price_ratio"] = round(
(current_price - min_price) / (max_price - min_price), 2
)
except ZeroDivisionError: # pragma: no cover
pass

attributes["max_price"] = max_price
first_price_at = next(iter(prices_sorted)).astimezone(timezone).hour
last_price_at = next(iter(reversed(prices_sorted))).astimezone(timezone).hour
attributes["max_price_at"] = last_price_at if sign_is_best == 1 else first_price_at
attributes["min_price"] = min_price
attributes["min_price_at"] = first_price_at if sign_is_best == 1 else last_price_at
attributes["next_best_at"] = [
ts.astimezone(timezone).hour for ts in prices_sorted.keys() if ts >= utc_time
ts.astimezone(timezone).hour for ts in prices_sorted if ts >= utc_time
]
return attributes

Expand Down
10 changes: 5 additions & 5 deletions aiopvpc/pvpc_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(
if self._api_token is not None:
self._data_source = "esios"
assert (data_source != "esios") or self._api_token is not None, data_source
self._user_agents = deque(sorted(_STANDARD_USER_AGENTS, key=lambda x: random()))
self._user_agents = deque(sorted(_STANDARD_USER_AGENTS, key=lambda _: random()))

self._local_timezone = zoneinfo.ZoneInfo(str(local_timezone))
assert tariff in TARIFFS
Expand Down Expand Up @@ -298,7 +298,7 @@ async def _update_prices_series(
"[%s] Evening download avoided, now with %d prices from %s UTC",
sensor_key,
current_num_prices,
list(current_prices)[0].strftime("%Y-%m-%d %Hh"),
next(iter(current_prices)).strftime("%Y-%m-%d %Hh"),
)
return None
elif (
Expand All @@ -319,7 +319,7 @@ async def _update_prices_series(
return None

if current_num_prices and (
list(current_prices)[0].astimezone(REFERENCE_TZ).date()
next(iter(current_prices)).astimezone(REFERENCE_TZ).date()
== local_ref_now.date()
):
# avoid download of today prices
Expand All @@ -328,7 +328,7 @@ async def _update_prices_series(
sensor_key,
local_ref_now,
current_num_prices,
list(current_prices)[0].astimezone(REFERENCE_TZ).date(),
next(iter(current_prices)).astimezone(REFERENCE_TZ).date(),
local_ref_now.date(),
)
else:
Expand All @@ -350,7 +350,7 @@ async def _update_prices_series(
"[%s] Download done, now with %d prices from %s UTC",
sensor_key,
len(current_prices),
list(current_prices)[0].strftime("%Y-%m-%d %Hh"),
next(iter(current_prices)).strftime("%Y-%m-%d %Hh"),
)

return current_prices
Expand Down
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import zoneinfo
from datetime import date, datetime, timedelta
from typing import TYPE_CHECKING

from aiopvpc.const import (
ESIOS_INJECTION,
Expand All @@ -16,7 +17,9 @@
ESIOS_PVPC,
KEY_PVPC,
)
from aiopvpc.pvpc_data import EsiosApiData, PVPCData

if TYPE_CHECKING:
from aiopvpc.pvpc_data import EsiosApiData, PVPCData

TEST_EXAMPLES_PATH = pathlib.Path(__file__).parent / "api_examples"
TZ_TEST = zoneinfo.ZoneInfo("Atlantic/Canary")
Expand Down Expand Up @@ -147,7 +150,7 @@ async def run_h_step(
"[Calls=%d]-> start=%s --> %s -> %s (%d prices)",
mock_session.call_count,
start,
list(current_prices)[0].strftime("%Y-%m-%d %Hh"),
next(iter(current_prices)).strftime("%Y-%m-%d %Hh"),
list(current_prices)[-1].strftime("%Y-%m-%d %Hh"),
len(current_prices),
)
Expand Down

0 comments on commit 368a167

Please sign in to comment.