Skip to content

Commit

Permalink
Updated date range to not include ValidTo and pulled the check into n…
Browse files Browse the repository at this point in the history
…ew method
  • Loading branch information
Conrad Juhl Andersen committed Jul 1, 2023
1 parent 471a6bb commit a3dfafd
Showing 1 changed file with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ async def async_get_tariffs(self):

tariff_data = {}
for entry in self._all_tariffs:
if (entry["ValidFrom"].split("T"))[0] <= check_date and (
entry["ValidTo"] is None
or (entry["ValidTo"].split("T"))[0] >= check_date
):
if self.__entry_in_range(entry, check_date):
_LOGGER.debug("Found possible dataset: %s", entry)
baseprice = 0
for key, val in entry.items():
Expand Down Expand Up @@ -125,10 +122,7 @@ def get_dated_tariff(self, date: datetime) -> dict:

tariff_data = {}
for entry in self._all_tariffs:
if (entry["ValidFrom"].split("T"))[0] <= check_date and (
entry["ValidTo"] is None
or (entry["ValidTo"].split("T"))[0] >= check_date
):
if self.__entry_in_range(entry, check_date):
baseprice = 0
for key, val in entry.items():
if key == "Price1":
Expand All @@ -150,10 +144,7 @@ def get_dated_system_tariff(self, date: datetime) -> dict:
check_date = date.strftime("%Y-%m-%d")
tariff_data = {}
for entry in self._all_additional_tariffs:
if (entry["ValidFrom"].split("T"))[0] <= check_date and (
entry["ValidTo"] is None
or (entry["ValidTo"].split("T"))[0] >= check_date
):
if self.__entry_in_range(entry, check_date):
if not entry["Note"] in tariff_data:
tariff_data.update(
{util_slugify(entry["Note"]): float(entry["Price1"])}
Expand Down Expand Up @@ -181,10 +172,7 @@ async def async_get_system_tariffs(self) -> dict:
check_date = (datetime.utcnow()).strftime("%Y-%m-%d")
tariff_data = {}
for entry in self._all_additional_tariffs:
if (entry["ValidFrom"].split("T"))[0] <= check_date and (
entry["ValidTo"] is None
or (entry["ValidTo"].split("T"))[0] >= check_date
):
if self.__entry_in_range(entry, check_date):
if not entry["Note"] in tariff_data:
tariff_data.update(
{util_slugify(entry["Note"]): float(entry["Price1"])}
Expand Down Expand Up @@ -215,3 +203,9 @@ async def async_call_api(self, query: str) -> dict:
except Exception as exc:
_LOGGER.error("Error during API request: %s", exc)
raise

def __entry_in_range(self, entry, check_date) -> bool:
"""Check if an entry is witin the date range"""
return (entry["ValidFrom"].split("T"))[0] <= check_date and (
entry["ValidTo"] is None
or (entry["ValidTo"].split("T"))[0] > check_date)

0 comments on commit a3dfafd

Please sign in to comment.