Skip to content

Commit

Permalink
Added cost history attributes based on request from Bojkas
Browse files Browse the repository at this point in the history
Signed-off-by: Vadim Grinco <[email protected]>
  • Loading branch information
grinco committed Oct 19, 2021
1 parent 2cd535d commit 9f0026d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"codeowners": [],
"requirements": ["requests"],
"iot_class": "cloud_polling",
"version": "0.1.0"
"version": "0.1.1"
}
32 changes: 24 additions & 8 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def state(self):
"""Return the state of the sensor."""
return self._state

@property
def extra_state_attributes(self):
"""Return other attributes of the sensor."""
return self._attr

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
Expand All @@ -44,24 +49,26 @@ def available(self):
"""Return True if entity is available."""
return self._available


def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._get_current_value()


def _get_current_value(self):
""" Parse the data and return value in EUR/kWh
"""

try:
eur_mwh = 0
current_cost = 0
cost_history = dict()
history_index = 0
cost_string = "Cena (EUR/MWh)"
hour_string = "Hodina"
cost_data = "https://www.ote-cr.cz/cs/kratkodobe-trhy/elektrina/denni-trh/@@chart-data"
# todo: add secondary validations
cost_table = "https://www.ote-cr.cz/cs/kratkodobe-trhy/elektrina/denni-trh"
cost_table2 = "https://www.ote-cr.cz/cs/kratkodobe-trhy/elektrina/multi-regional-coupling"

date = datetime.datetime.now()
params = dict (
Expand All @@ -70,16 +77,25 @@ def _get_current_value(self):

response = requests.get(url=cost_data, params=params)
json = response.json()
axis = ""
cost_axis = ""
hour_axis = ""
for key in json['axis'].keys():
if json['axis'][key]['legend'] == cost_string:
axis = key
cost_axis = key
if json['axis'][key]['legend'] == hour_string:
hour_axis = key


for values in json['data']['dataLine']:
if values['title'] == cost_string:
eur_mwh = values['point'][date.hour][axis]
for data in values['point']:
history_index = int(data[hour_axis])
cost_history[history_index] = float(data[cost_axis])
current_cost = cost_history[date.hour]


self._state = float(eur_mwh)
self._state = current_cost
self._attr = cost_history
self._available = True
except:
self._available = False
Expand Down

0 comments on commit 9f0026d

Please sign in to comment.