From 9b605fe0cf9600975c3fd228e8095dea29fd826a Mon Sep 17 00:00:00 2001 From: smarthomepch Date: Sat, 30 Jan 2021 19:18:11 +0100 Subject: [PATCH] added logging support --- aristonremotethermo/ariston.py | 18 ++++++++++++++++++ aristonremotethermo/aristonaqua.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/aristonremotethermo/ariston.py b/aristonremotethermo/ariston.py index c231090..2bb5270 100644 --- a/aristonremotethermo/ariston.py +++ b/aristonremotethermo/ariston.py @@ -106,12 +106,15 @@ class AristonHandler: 'dhw_unknown_as_on' - indicates if to assume 'dhw_flame' as being True if cannot be identified. + 'logging_level' - defines level of logging - allowed values [CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET=(default)] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ _VERSION = "1.0.28" _LOGGER = logging.getLogger(__name__) + _LOGGING_LEVELS = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"] _PARAM_ACCOUNT_CH_GAS = "account_ch_gas" _PARAM_ACCOUNT_CH_ELECTRICITY = "account_ch_electricity" @@ -544,6 +547,7 @@ def __init__(self, units: str = _UNIT_METRIC, ch_and_dhw: bool = False, dhw_unknown_as_on: bool = True, + logging_level: str = "NOTSET", ) -> None: """ Initialize API. @@ -576,6 +580,9 @@ def __init__(self, if not isinstance(sensors, list): raise Exception("Invalid sensors type") + if logging_level not in self._LOGGING_LEVELS: + raise Exception("Invalid logging_level") + if sensors: for sensor in sensors: if sensor not in self._SENSOR_LIST: @@ -589,6 +596,17 @@ def __init__(self, if not os.path.isdir(self._store_folder): os.makedirs(self._store_folder) + """ + Logging settings + """ + self._logging_level = logging.getLevelName(logging_level) + self._LOGGER.setLevel(self._logging_level) + self._console_handler = logging.StreamHandler() + self._console_handler.setLevel(self._logging_level) + self._formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + self._console_handler.setFormatter(self._formatter) + self._LOGGER.addHandler(self._console_handler) + # clear read sensor values self._ariston_sensors = {} for sensor in self._SENSOR_LIST: diff --git a/aristonremotethermo/aristonaqua.py b/aristonremotethermo/aristonaqua.py index 89435ec..d10027f 100644 --- a/aristonremotethermo/aristonaqua.py +++ b/aristonremotethermo/aristonaqua.py @@ -62,12 +62,15 @@ class AquaAristonHandler: 'store_folder' - folder to store HTTP and internal data to. If empty string is used, then current working directory is used with a folder 'http_logs' within it. + 'logging_level' - defines level of logging - allowed values [CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET=(default)] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ _VERSION = "1.0.28" _LOGGER = logging.getLogger(__name__) + _LOGGING_LEVELS = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"] _VALUE = "value" _UNITS = "units" @@ -301,6 +304,7 @@ def __init__(self, polling: Union[float, int] = 1., store_file: bool = False, store_folder: str = "", + logging_level: str = "NOTSET", ) -> None: """ Initialize API. @@ -324,6 +328,9 @@ def __init__(self, if boiler_type not in self._SUPPORTED_BOILER_TYPES: raise Exception("Unknown boiler type") + if logging_level not in self._LOGGING_LEVELS: + raise Exception("Invalid logging_level") + if sensors: for sensor in sensors: if sensor not in self._SENSOR_LIST: @@ -337,6 +344,17 @@ def __init__(self, if not os.path.isdir(self._store_folder): os.makedirs(self._store_folder) + """ + Logging settings + """ + self._logging_level = logging.getLevelName(logging_level) + self._LOGGER.setLevel(self._logging_level) + self._console_handler = logging.StreamHandler() + self._console_handler.setLevel(self._logging_level) + self._formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + self._console_handler.setFormatter(self._formatter) + self._LOGGER.addHandler(self._console_handler) + self._ariston_sensors = {} for sensor_all in self._SENSOR_LIST: self._ariston_sensors[sensor_all] = {}