Skip to content

Commit

Permalink
Added ability to fetch historical data
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Feb 13, 2022
1 parent dbe38a4 commit 9cd8202
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ See [example.py](https://github.com/klejejs/python-thermia-online-api/blob/main/
| Hot Water data | |
| `is_operation_mode_read_only` | Boolean value indicating if the Heat Pump operation mode is read-only |
| `is_hot_water_switch_available` | Boolean value indicating if the Heat Pump has a hot water switch |
| --- | --- |
| Historical data | |
| `historical_data_registers` | List of available registers to use for historical data fetching |

## Available functions within ThermiaHeatPump class:
| Function | Description |
| --- | --- |
| `update_data` | Refetch all data from Thermia for Heat Pump |
| --- | --- |
| Change heat pump state | |
| `set_temperature` | Set the target temperature for the Heat Pump |
| `set_operation_mode` | Set the operation mode for the Heat Pump |
| `set_hot_water_switch_state` | Set the hot water switch state to 0 (off) or 1 (on) for the Heat Pump |
| --- | --- |
| Fetch historical data | |
| `get_historical_data_for_register` | Fetch historical data by using register name from `historical_data_registers` together with start_time and end_time of the data in Python datatime format. Returns list of dictionaries which contains data in format `{ "time": datetime, "value": int }` |
44 changes: 44 additions & 0 deletions ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,50 @@ def get_all_alarms(self, device_id: str):

return request.json()

def get_historical_data_registers(self, device_id: str):
self.__check_token_validity()

url = (
self.configuration["apiBaseUrl"]
+ "/api/v1/DataHistory/installation/"
+ str(device_id)
)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code

if status != 200:
_LOGGER.error("Error in historical data registers. " + str(status))
return None

return request.json()

def get_historical_data(
self, device_id: str, register_id, start_date_str, end_date_str
):
self.__check_token_validity()

url = (
self.configuration["apiBaseUrl"]
+ "/api/v1/datahistory/installation/"
+ str(device_id)
+ "/register/"
+ str(register_id)
+ "/minute?periodStart="
+ start_date_str
+ "&periodEnd="
+ end_date_str
)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code

if status != 200:
_LOGGER.error(
"Error in historical data for specific register. " + str(status)
)
return None

return request.json()

def get__group_temperatures(self, device_id: str):
return self.__get_register_group(device_id, REG_GROUP_TEMPERATURES)

Expand Down
6 changes: 6 additions & 0 deletions ThermiaOnlineAPI/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@
REG_OPER_TIME_COMPRESSOR,
REG_OPER_TIME_HOT_WATER,
]

###############################################################################
# Other
###############################################################################

DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
61 changes: 61 additions & 0 deletions ThermiaOnlineAPI/model/HeatPump.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import json
import logging

Expand All @@ -22,6 +23,7 @@
REG_RETURN_LINE,
REG_SUPPLY_LINE,
TEMPERATURE_REGISTERS,
DATETIME_FORMAT,
)

from ..utils.utils import get_dict_value_safe
Expand Down Expand Up @@ -55,6 +57,7 @@ def __init__(self, device_data: json, api_interface: "ThermiaAPI"):
self.__group_hot_water = None

self.__alarms = None
self.__historical_data_registers_map = None

self.__register_indexes = DEFAULT_REGISTER_INDEXES

Expand Down Expand Up @@ -193,6 +196,19 @@ def __get_active_alarms(self):
)
return list(active_alarms)

def __set_historical_data_registers(self):
data = self.__api_interface.get_historical_data_registers(self.__device_id)

data_map = {}

if data is not None and data.get("registers") is not None:
registers = data["registers"]

for register in registers:
data_map[register["registerName"]] = register["registerId"]

self.__historical_data_registers_map = data_map

@property
def name(self):
return self.__info.get("name")
Expand Down Expand Up @@ -438,3 +454,48 @@ def active_alarms(self):
active_alarms = self.__get_active_alarms()
active_alarm_texts = map(lambda alarm: alarm.get("eventTitle"), active_alarms)
return list(active_alarm_texts)

###########################################################################
# Historical data
###########################################################################

@property
def historical_data_registers(self):
if self.__historical_data_registers_map is None:
self.__set_historical_data_registers()

return list(self.__historical_data_registers_map.keys())

def get_historical_data_for_register(
self, register_name, start_date: datetime, end_date: datetime
):
if self.__historical_data_registers_map is None:
self.__set_historical_data_registers()

register_id = self.__historical_data_registers_map.get(register_name)

if register_id is None:
_LOGGER.error("Register name is not supported: " + str(register_name))
return None

historical_data = self.__api_interface.get_historical_data(
self.__device_id,
register_id,
start_date.strftime(DATETIME_FORMAT),
end_date.strftime(DATETIME_FORMAT),
)

if historical_data is None or historical_data.get("data") is None:
return []

return list(
map(
lambda entry: {
"time": datetime.strptime(
entry["at"].split(".")[0], DATETIME_FORMAT
),
"value": int(entry["val"]),
},
historical_data["data"],
)
)
17 changes: 17 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timedelta
from ThermiaOnlineAPI import Thermia

USERNAME = "demo"
Expand Down Expand Up @@ -97,6 +98,22 @@

print("\n")

print(
"Available historical data registers: " + str(heat_pump.historical_data_registers)
)
print(
"Historical data for outdoor temperature during past 24h: "
+ str(
heat_pump.get_historical_data_for_register(
"REG_OUTDOOR_TEMPERATURE",
datetime.now() - timedelta(days=1),
datetime.now(),
)
)
)

print("\n")

thermia.update_data()

heat_pump.set_temperature(19)
Expand Down

0 comments on commit 9cd8202

Please sign in to comment.