Skip to content

Commit

Permalink
Logger changes
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Jan 13, 2022
1 parent 6678dc3 commit d74ae1e
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..exceptions.NetworkException import NetworkException
from ..model.HeatPump import ThermiaHeatPump

LOGGER = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)

THERMIA_API_CONFIG_URL = "https://online.thermia.se/api/configuration"
THERMIA_INSTALLATION_PATH = "/api/v1/Registers/Installations/"
Expand All @@ -34,10 +34,9 @@ def get_devices(self):
url = self.configuration["apiBaseUrl"] + "/api/v1/InstallationsInfo/own"
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code
LOGGER.info("Fetching devices. " + str(status))

if status != 200:
LOGGER.error("Error fetching devices. " + str(status))
_LOGGER.error("Error fetching devices. " + str(status))
return []

return request.json()
Expand All @@ -50,7 +49,7 @@ def get_device_by_id(self, device_id: str):
device = [d for d in devices if str(d["id"]) == device_id]

if len(device) != 1:
LOGGER.error("Error getting device by id: " + str(device_id))
_LOGGER.error("Error getting device by id: " + str(device_id))
return None

return device[0]
Expand All @@ -63,7 +62,7 @@ def get_device_info(self, device_id: str):
status = request.status_code

if status != 200:
LOGGER.error("Error fetching device info. " + str(status))
_LOGGER.error("Error fetching device info. " + str(status))
return None

return request.json()
Expand All @@ -81,7 +80,7 @@ def get_device_status(self, device_id: str):
status = request.status_code

if status != 200:
LOGGER.error("Error fetching device status. " + str(status))
_LOGGER.error("Error fetching device status. " + str(status))
return None

return request.json()
Expand All @@ -99,12 +98,14 @@ def get_temperature_status(self, device: ThermiaHeatPump):
status = request.status_code

if status != 200:
LOGGER.error("Error in getting device's temperature status. " + str(status))
_LOGGER.error(
"Error in getting device's temperature status. " + str(status)
)
return None

device_temperature_register_index = device.get_register_indexes()["temperature"]
if device_temperature_register_index is None:
LOGGER.error(
_LOGGER.error(
"Error in getting device's temperature status. No temperature register index."
)
return None
Expand Down Expand Up @@ -140,7 +141,7 @@ def get_operation_mode(self, device: ThermiaHeatPump):
status = request.status_code

if status != 200:
LOGGER.error("Error in getting device's operation mode. " + str(status))
_LOGGER.error("Error in getting device's operation mode. " + str(status))
return None

data = [d for d in request.json() if d["registerName"] == "REG_OPERATIONMODE"]
Expand Down Expand Up @@ -185,7 +186,7 @@ def get_hot_water_switch_state(self, device: ThermiaHeatPump):
status = request.status_code

if status != 200:
LOGGER.error("Error in getting device's operation mode. " + str(status))
_LOGGER.error("Error in getting device's operation mode. " + str(status))
return None

data = [
Expand All @@ -211,7 +212,7 @@ def get_hot_water_switch_state(self, device: ThermiaHeatPump):
def set_temperature(self, device: ThermiaHeatPump, temperature):
device_temperature_register_index = device.get_register_indexes()["temperature"]
if device_temperature_register_index is None:
LOGGER.error(
_LOGGER.error(
"Error setting device's temperature. No temperature register index."
)
return
Expand All @@ -227,7 +228,7 @@ def set_operation_mode(self, device: ThermiaHeatPump, mode):
"operation_mode"
]
if device_operation_mode_register_index is None:
LOGGER.error(
_LOGGER.error(
"Error setting device's operation mode. No operation mode register index."
)
return
Expand All @@ -243,7 +244,7 @@ def set_hot_water_switch_state(
"hot_water_switch"
]
if device_hot_water_switch_state_register_index is None:
LOGGER.error(
_LOGGER.error(
"Error setting device's hot water switch state. No hot water switch register index."
)
return
Expand Down Expand Up @@ -273,7 +274,7 @@ def __set_register_value(

status = request.status_code
if status != 200:
LOGGER.error(
_LOGGER.error(
"Error setting register "
+ str(register_index)
+ " value. "
Expand All @@ -285,7 +286,7 @@ def __fetch_configuration(self):
status = request.status_code

if status != 200:
LOGGER.error("Error fetching API configuration. " + str(status))
_LOGGER.error("Error fetching API configuration. " + str(status))
raise NetworkException("Error fetching API configuration.", status)

return request.json()
Expand All @@ -302,7 +303,7 @@ def __authenticate(self):
status = request_auth.status_code

if status != 200:
LOGGER.error(
_LOGGER.error(
"Authentication request failed, please check credentials. "
+ str(status)
)
Expand All @@ -311,7 +312,7 @@ def __authenticate(self):
)

auth_data = request_auth.json()
LOGGER.debug(str(auth_data))
_LOGGER.debug(str(auth_data))

token_valid_to = auth_data.get("tokenValidToUtc").split(".")[0]
datetime_object = datetime.strptime(token_valid_to, "%Y-%m-%dT%H:%M:%S")
Expand All @@ -325,13 +326,13 @@ def __authenticate(self):
"Content-Type": "application/json",
}

LOGGER.info("Authentication was successful, token set.")
_LOGGER.info("Authentication was successful, token set.")
return True

def __check_token_validity(self):
if (
self.__token_valid_to is None
or self.__token_valid_to < datetime.now().timestamp()
):
LOGGER.info("Token expired, reauthenticating.")
_LOGGER.info("Token expired, reauthenticating.")
self.authenticated = self.__authenticate()

0 comments on commit d74ae1e

Please sign in to comment.