Skip to content

Commit

Permalink
Merge branch 'master' into Fixing-refresh-issues-around-midnight
Browse files Browse the repository at this point in the history
  • Loading branch information
MTrab committed Jun 22, 2023
2 parents a97a443 + 2577a9c commit a69092a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
with:
fetch-depth: 0
- name: Set up Python 3.8
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
python-version: 3.8
- name: Cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
cd custom_components/energidataservice
zip energidataservice.zip -r ./
- name: Upload zip to release
uses: svenstaro/upload-release-action@2.5.0
uses: svenstaro/upload-release-action@2.6.1
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ./custom_components/energidataservice/energidataservice.zip
Expand Down
2 changes: 2 additions & 0 deletions custom_components/energidataservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(
self.predictions = None
self.api_predictions = None
self.tariff_data = None
self.tariff_connector = None
self.predictions_calculated = False
self.predictions_currency = None
self.connector_currency = "EUR"
Expand Down Expand Up @@ -225,6 +226,7 @@ async def async_get_tariffs(self) -> None:
self._config.options.get(CONF_TARIFF_CHARGE_OWNER),
)

self.tariff_connector = tariff
self.tariff_data = await tariff.async_get_tariffs()

@property
Expand Down
4 changes: 2 additions & 2 deletions custom_components/energidataservice/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"issue_tracker": "https://github.com/MTrab/energidataservice/issues",
"requirements": [
"async-retrying-ng==0.1.1",
"CurrencyConverter==0.17.7"
"CurrencyConverter==0.17.8"
],
"version": "1.3.3"
"version": "1.3.4"
}
43 changes: 13 additions & 30 deletions custom_components/energidataservice/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from collections import namedtuple
from datetime import datetime
import json
import json # pylint: disable=unused-import
import logging

from homeassistant.components import sensor
Expand Down Expand Up @@ -639,17 +639,19 @@ def inner(*_, **__):
and len(self._api.tariff_data["tariffs"]) > 0
):
try:
if "additional_tariffs" in self._api.tariff_data:
for tariff, additional_tariff in self._api.tariff_data[
"additional_tariffs"
].items():
tariff_value += float(additional_tariff)
if tariff == "elafgift":
elafgift = float(additional_tariff)

tariff_value += float(
self._api.tariff_data["tariffs"][str(fake_dt.hour)]
system_tariff: dict = (
self._api.tariff_connector.get_dated_system_tariff(fake_dt)
)
chargeowner_tariff: dict = self._api.tariff_connector.get_dated_tariff(
fake_dt
)

for tariff, additional_tariff in system_tariff.items():
tariff_value += float(additional_tariff)
if tariff == "elafgift":
elafgift = float(additional_tariff)

tariff_value += float(chargeowner_tariff[str(fake_dt.hour)])
except KeyError:
_LOGGER.warning(
"Error adding tariffs for %s, no valid tariffs was found!", fake_dt
Expand Down Expand Up @@ -704,13 +706,6 @@ def inner(*_, **__):
if self._cent:
price = price * CENT_MULTIPLIER

_LOGGER.debug(
"Hour %s: Tariff %s, Template %s",
fake_dt.hour,
tariff_value,
template_value,
)

return price

def _format_list(
Expand All @@ -727,12 +722,6 @@ def _format_list(
if predictions:
list_for = "FORECASTS"

_LOGGER.debug(
"Unformatted list for '%s':\n%s",
list_for,
json.dumps(data, indent=2, default=str),
)

_start = datetime.now().timestamp()
Interval = namedtuple("Interval", "price hour")
for i in data:
Expand All @@ -746,12 +735,6 @@ def _format_list(
_stop = datetime.now().timestamp()
_ttf = round(_stop - _start, 2)

_LOGGER.debug(
"Formatted list for '%s':\n%s",
list_for,
json.dumps(formatted_pricelist, indent=2, default=str),
)

if tomorrow:
_calc_for = "TOMORROW"
self._api.tomorrow_calculated = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def __init__(
self.client = client
self._chargeowner = chargeowner
self._tariffs = {}
self._result = {}
self._additional_tariff = {}
self._all_tariffs = {}
self._all_additional_tariffs = {}

# dt_now = datetime.now()
# for elafgift in FM_EL_AFGIFT:
Expand Down Expand Up @@ -81,12 +82,12 @@ async def async_get_tariffs(self):
return
else:
# We got data from the DataHub - update the dataset
self._result = resp
self._all_tariffs = resp

check_date = (datetime.utcnow()).strftime("%Y-%m-%d")

tariff_data = {}
for entry in self._result:
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
Expand All @@ -113,10 +114,53 @@ async def async_get_tariffs(self):
return self.tariffs
except KeyError:
_LOGGER.error(
"Error finding '%s' in the list of charge owners - please reconfigure your integration.",
"Error finding '%s' in the list of charge owners - "
"please reconfigure your integration.",
self._chargeowner,
)

def get_dated_tariff(self, date: datetime) -> dict:
"""Get tariff for this specific date."""
check_date = date.strftime("%Y-%m-%d")

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
):
baseprice = 0
for key, val in entry.items():
if key == "Price1":
baseprice = val
if "Price" in key:
hour = str(int("".join(filter(str.isdigit, key))) - 1)

tariff_data.update(
{hour: val if val is not None else baseprice}
)

if len(tariff_data) == 24:
return tariff_data

return {}

def get_dated_system_tariff(self, date: datetime) -> dict:
"""Get system tariffs for this specific date."""
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 not entry["Note"] in tariff_data:
tariff_data.update(
{util_slugify(entry["Note"]): float(entry["Price1"])}
)

return tariff_data

async def async_get_system_tariffs(self) -> dict:
"""Get additional system tariffs defined by the Danish government."""
search_filter = '{"Note":["Elafgift","Systemtarif","Transmissions nettarif"]}'
Expand All @@ -131,10 +175,12 @@ async def async_get_system_tariffs(self) -> dict:
"Could not fetch tariff data from Energi Data Service DataHub!"
)
return
else:
self._all_additional_tariffs = dataset

check_date = (datetime.utcnow()).strftime("%Y-%m-%d")
tariff_data = {}
for entry in dataset:
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,174 +5,140 @@
"gln": "5790000705689",
"company": "Radius Elnet A/S",
"type": ["DT_C_01"],
"note": ["Nettarif C time"],
},
"RAH": {
"gln": "5790000681327",
"company": "RAH Net A/S",
"type": ["RAH-C"],
"note": ["Nettarif C time"],
},
"Konstant": {
"gln": "5790000704842",
"company": "Konstant Net A/S - 151",
"type": ["151-NT01T"],
"note": ["Nettarif C time"],
},
"Cerius": {
"gln": "5790000705184",
"company": "Cerius A/S",
"type": ["30TR_C_ET"],
"note": ["Nettarif C time"],
},
"N1": {
"gln": "5790001089030",
"company": "N1 A/S - 131",
"type": ["CD", "CD R"],
"note": ["Nettarif C"],
},
"N1 Randers": {
"gln": "5790000681372",
"company": "N1 Randers A/S",
"type": ["CD"],
"note": ["Nettarif C"],
},
"Dinel": {
"gln": "5790000610099",
"company": "Dinel A/S",
"type": ["TCL>100_02"],
"note": ["Nettarif C time"],
},
"TREFOR El-net": {
"gln": "5790000392261",
"company": "TREFOR El-net A/S",
"type": ["C"],
"note": ["Nettarif C time"],
},
"TREFOR El-net Øst": {
"gln": "5790000706686",
"company": "TREFOR El-net Øst A/S",
"type": ["46"],
"note": ["Nettarif C time"],
},
"Elektrus": {
"gln": "5790000836239",
"company": "Elektrus A/S",
"type": ["6000091"],
"note": ["Nettarif C time"],
},
"Elnet Midt": {
"gln": "5790001100520",
"company": "Elnet Midt A/S",
"type": ["T3002"],
"note": ["Nettarif C"],
},
"Hurup Elværk Net": {
"gln": "5790000610839",
"company": "Hurup Elværk Net A/S",
"type": ["HEV-NT-01"],
"note": ["Nettarif"],
},
"Veksel": {
"gln": "5790001088217",
"company": "Veksel A/S",
"type": ["NT-10"],
"note": ["Nettarif C time NT-10"],
},
"Vores Elnet": {
"gln": "5790000610976",
"company": "Vores Elnet A/S",
"type": ["TNT1009"],
"note": ["Nettarif C time"],
},
"Netselskabet Elværk": {
"gln": "5790000681075",
"company": "Netselskabet Elværk A/S - 042",
"type": ["0NCFF"],
"note": ["Nettarif C Flex"],
},
"Nord Energi Net": {
"gln": "5790000610877",
"company": "Nord Energi Net A/S",
"type": ["TA031U200"],
"note": ["Nettarif C"],
},
"Nordvestjysk Elforsyning (NOE Net)": {
"gln": "5790000395620",
"company": "NOE Net A/S",
"type": ["Net C"],
"note": ["Nettarif C"],
},
"Ikast El Net": {
"gln": "5790000682102",
"company": "Ikast El Net A/S",
"type": ["IEV-NT-01", "IEV-NT-11"],
"note": ["Nettarif C time", "Transport - Overordnet net"],
},
"FLOW Elnet": {
"gln": "5790000392551",
"company": "FLOW Elnet A/S",
"type": ["FE2 NT-01"],
"note": ["Nettarif C time"],
},
"Elinord": {
"gln": "5790001095277",
"company": "Elinord A/S",
"type": ["43300"],
"note": ["Transportbetaling, eget net C"],
},
"Hammel Elforsyning Net": {
"gln": "5790001090166",
"company": "Hammel Elforsyning Net A/S",
"type": ["50001"],
"note": ["Overliggende net"],
},
"El-net Kongerslev": {
"gln": "5790002502699",
"company": "El-net Kongerslev A/S",
"type": ["K_22100"],
"note": ["Nettarif C"],
"type": ["C-Tarif"],
},
"Ravdex": {
"gln": "5790000836727",
"company": "Ravdex A/S",
"type": ["NT-C"],
"note": ["Nettarif C time"],
},
"Tarm Elværk Net": {
"gln": "5790000706419",
"company": "Tarm Elværk Net A/S",
"type": ["TEV-NT-01"],
"note": ["Nettarif C"],
},
"Zeanet": {
"gln": "5790001089375",
"company": "Zeanet A/S",
"type": ["43110"],
"note": ["Nettarif C time"],
},
"NKE-Elnet": {
"gln": "5790001088231",
"company": "NKE-Elnet A/S",
"type": ["94TR_C_ET"],
"note": ["Nettarif C time"],
},
"L-Net": {
"gln": "5790001090111",
"company": "L-Net A/S",
"type": ["4010"],
"note": ["Nettarif C time"],
},
"Midtfyns Elforsyning": {
"gln": "5790001089023",
"company": "Midtfyns Elforsyning A.m.b.A",
"type": ["TNT15000"],
"note": ["Nettarif C Flex"],
},
"Sunds Net": {
"gln": "5790001095444",
"company": "Sunds Net A.m.b.A",
"type": ["SEF-NT-05"],
"note": ["Nettarif C Flex - time"],
},
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
async-retrying==0.2.2
CurrencyConverter==0.17.7
CurrencyConverter==0.17.8

0 comments on commit a69092a

Please sign in to comment.