Skip to content

Commit 4e5e0cb

Browse files
committed
Change debug command to return string instead of print
1 parent 98c4fef commit 4e5e0cb

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

ThermiaOnlineAPI/model/HeatPump.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
import logging
44
import sys
5-
from ..utils.utils import pretty_print_except
5+
from ..utils.utils import pretty_json_string_except
66

77
from typing import TYPE_CHECKING, Dict, List, Optional
88

@@ -940,17 +940,12 @@ def get_historical_data_for_register(
940940
# Print debug data
941941
###########################################################################
942942

943-
def debug(self):
944-
print("Creating debug file")
943+
def debug(self) -> str:
944+
debug_str = "########## DEBUG START ##########\n"
945945

946-
original_stdout = sys.stdout
947-
f = open("debug.txt", "w")
948-
sys.stdout = f
946+
debug_str += "self.__info:\n"
949947

950-
print("########## DEBUG START ##########")
951-
952-
print("self.__info:")
953-
pretty_print_except(
948+
debug_str += pretty_json_string_except(
954949
self.__info,
955950
[
956951
"address",
@@ -964,11 +959,13 @@ def debug(self):
964959
],
965960
)
966961

967-
print("self.__status:")
968-
pretty_print_except(self.__status)
962+
debug_str += "self.__status:\n"
963+
964+
debug_str += pretty_json_string_except(self.__status)
965+
966+
debug_str += "self.__device_data:\n"
969967

970-
print("self.__device_data:")
971-
pretty_print_except(
968+
debug_str += pretty_json_string_except(
972969
self.__device_data,
973970
["macAddress", "owner", "retailerAccess", "retailerId", "id", "status"],
974971
)
@@ -982,20 +979,18 @@ def debug(self):
982979
installation_profile_id
983980
)
984981
if all_available_groups is not None:
985-
print("All available groups:")
986-
pretty_print_except(all_available_groups)
982+
debug_str += "All available groups:\n"
983+
debug_str += pretty_json_string_except(all_available_groups)
987984

988985
for group in all_available_groups:
989986
group_name = group.get("name")
990987
if group_name is not None:
991-
print("Group " + group_name + ":")
988+
debug_str += "Group " + group_name + ":\n"
992989
group_data = self.__api_interface.get_register_group_json(
993990
self.__device_id, group_name
994991
)
995-
pretty_print_except(group_data)
992+
debug_str += pretty_json_string_except(group_data)
996993

997-
print("########## DEBUG END ##########")
994+
debug_str += "########## DEBUG END ##########\n"
998995

999-
sys.stdout = original_stdout
1000-
f.close()
1001-
print("Debug file created")
996+
return debug_str

ThermiaOnlineAPI/utils/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,26 @@ def get_list_value_or_default(list, idx, default: T) -> T:
3030
return default
3131

3232

33-
def pretty_print(json_object):
34-
print(json.dumps(json_object, indent=4, sort_keys=True))
35-
print("\n")
33+
def pretty_json_string(json_object) -> str:
34+
pretty_str = json.dumps(json_object, indent=4, sort_keys=True)
35+
pretty_str += "\n\n"
3636

37+
return pretty_str
3738

38-
def pretty_print_except(json_object, except_keys=[]):
39+
40+
def pretty_json_string_except(json_object, except_keys=[]) -> str:
3941
if json_object is None:
4042
return
4143

4244
json_object_copy = json_object.copy()
4345
for key in except_keys:
4446
if key in json_object_copy:
4547
del json_object_copy[key]
46-
pretty_print(json_object_copy)
48+
49+
pretty_json_str = pretty_json_string(json_object_copy)
50+
pretty_json_str += "\n"
51+
52+
return pretty_json_str
4753

4854

4955
def base64_url_encode(data):

example.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
heat_pump = thermia.heat_pumps[0]
1818

19-
heat_pump.debug()
19+
20+
print("Creating debug file")
21+
with open("debug.txt", "w") as f:
22+
f.write(heat_pump.debug())
23+
24+
print("Debug file created")
2025

2126
print("\n")
2227
print("\n")

0 commit comments

Comments
 (0)