forked from pavelb84/ote_rate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
134 lines (105 loc) · 3.95 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Platform for sensor integration."""
from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import (
DEVICE_CLASS_MONETARY,
SensorEntity,
SensorEntityDescription,
)
""" External Imports """
import requests
import json
import datetime
import logging
import gc
""" Constants """
NATIVE_UNIT_OF_MEASUREMENT = "EUR/MWh"
DEVICE_CLASS = "monetary"
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the sensor platform."""
add_entities([OTERateSensor()], update_before_add=True)
class OTERateSensor(SensorEntity):
"""Representation of a Sensor."""
def __init__(self):
"""Initialize the sensor."""
self._value = None
self._attr = None
@property
def name(self):
"""Return the name of the sensor."""
return 'Current OTE Energy Cost'
@property
def native_value(self):
"""Return the native value of the sensor."""
return self._value
@property
def native_unit_of_measurement(self):
"""Return the native unit of measurement."""
return NATIVE_UNIT_OF_MEASUREMENT
@property
def device_class(self):
"""Return the device class of the sensor."""
return DEVICE_CLASS
@property
def extra_state_attributes(self):
"""Return other attributes of the sensor."""
return self._attr
@property
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()
# collect method immediately free the resource of
# non-referenced object.
gc.collect()
def _get_data_from_ote_cr(self, date):
try:
cost_history = dict()
cost_string = "Cena (EUR/MWh)"
hour_string = "Hodina"
cost_data = "https://www.ote-cr.cz/cs/kratkodobe-trhy/elektrina/denni-trh/@@chart-data"
params = dict (
date = date.strftime('%Y-%m-%d')
)
response = requests.get(url=cost_data, params=params, timeout=5)
json = response.json()
cost_axis = ""
hour_axis = ""
for key in json['axis'].keys():
if json['axis'][key]['legend'] == cost_string:
cost_axis = key
if json['axis'][key]['legend'] == hour_string:
hour_axis = key
for values in json['data']['dataLine']:
if values['title'] == cost_string:
for data in values['point']:
history_index = int(data[hour_axis])-1
cost_history[history_index] = float(data[cost_axis])
return cost_history
except:
self._available = False
_LOGGER.exception("Error occured while retrieving data from ote-cr.cz.")
def _get_current_value(self):
""" Parse the data and return value in EUR/MWh
"""
try:
current_cost = 0
today_date = datetime.datetime.now()
tomorrow_date = today_date + datetime.timedelta(days=1)
today_cost_data = self._get_data_from_ote_cr(today_date)
tomorrow_cost_data = self._get_data_from_ote_cr(tomorrow_date)
#transform tomorrow's data
cost_history = today_cost_data
for key, value in tomorrow_cost_data.items():
cost_history[int(key)+24] = value
current_cost = cost_history[today_date.hour]
self._value = current_cost
self._attr = cost_history
self._available = True
except:
self._available = False
_LOGGER.exception("Error occured while retrieving data from ote-cr.cz.")