11"""Winet-Control API"""
22from __future__ import annotations
3- import logging as _log
3+ import logging
4+
45from json import JSONDecodeError
56
67import aiohttp
1718 WinetRegisterCategory ,
1819)
1920
21+ LOGGER = logging .getLogger (__package__ )
22+
2023
2124class WinetAPILocal :
2225 """Bottom level API. handle http communication with the local winet module"""
@@ -51,35 +54,35 @@ async def get_registers(
5154 "Origin" : f"http://{ self ._stove_ip } " ,
5255 "Referer" : f"http://{ self ._stove_ip } /management.html" ,
5356 }
54- _log . warning (f"Querying { url } with data={ data } " )
57+ LOGGER . debug (f"Querying { url } with data={ data } " )
5558 try :
5659 async with session .post (url , data = data , headers = headers ) as response :
5760 try :
5861 if response .status != 200 :
59- _log .warning (f"Error accessing { url } - { response .status } " )
62+ LOGGER .warning (f"Error accessing { url } - { response .status } " )
6063 raise ConnectionError (
6164 f"Communication error - Response status { response .status } "
6265 )
6366 try :
6467 json_data = await response .json (content_type = None )
65- _log . warning ("Received: %s" , json_data )
68+ LOGGER . debug ("Received: %s" , json_data )
6669
6770 if "result" in json_data :
6871 # handle an action's result
6972 if json_data ["result" ] is False :
70- _log .warning ("Api result is False" )
73+ LOGGER .warning ("Api result is False" )
7174 else :
7275 try :
7376 return WinetGetRegisterResult (** json_data )
7477 except Exception :
75- _log .warning ("Error parsing poll data" )
76- _log . warning (f"Received: { json_data } " )
78+ LOGGER .warning ("Error parsing poll data" )
79+ LOGGER . debug (f"Received: { json_data } " )
7780 # TODO: what about model check exceptions ?
7881 except JSONDecodeError :
79- _log .warning ("Error decoding JSON: [%s]" , response .text )
82+ LOGGER .warning ("Error decoding JSON: [%s]" , response .text )
8083
8184 except ConnectionError as exc :
82- _log .warning (f"Connection Error accessing { url } " )
85+ LOGGER .warning (f"Connection Error accessing { url } " )
8386 raise ConnectionError (
8487 "ConnectionError - host not found"
8588 ) from exc
@@ -92,7 +95,7 @@ async def get_registers(
9295 ):
9396 raise ConnectionError ()
9497 except Exception as unknown_error :
95- _log .error ("Unhandled Exception %s" , type (unknown_error ))
98+ LOGGER .error ("Unhandled Exception %s" , type (unknown_error ))
9699
97100 async def set_register (
98101 self , registerid : WinetRegister , value : int , key = "002" , memory = 1
@@ -118,27 +121,27 @@ async def set_register(
118121 "Origin" : f"http://{ self ._stove_ip } " ,
119122 "Referer" : f"http://{ self ._stove_ip } /management.html" ,
120123 }
121- _log .debug (f"Posting to { url } , data={ data } " )
124+ LOGGER .debug (f"Posting to { url } , data={ data } " )
122125 try :
123126 async with session .post (url , data = data , headers = headers ) as response :
124127 try :
125128 # TODO: log others error responses codes
126129 if response .status != 200 :
127130 # Valid address - but poll endpoint not found
128- _log .warning (f"Error accessing { url } - { response .status } " )
131+ LOGGER .warning (f"Error accessing { url } - { response .status } " )
129132 raise ConnectionError (
130133 f"Error accessing { url } - { response .status } "
131134 )
132135 try :
133136 # returns {'result': False} if failed (or True if success)
134137 json_data = await response .json (content_type = None )
135138 if json_data ["result" ] is not True :
136- _log . warning ("Received: %s" , json_data )
139+ LOGGER . debug ("Received: %s" , json_data )
137140
138141 except JSONDecodeError :
139- _log .warning ("Error decoding JSON: [%s]" , response .text )
142+ LOGGER .warning ("Error decoding JSON: [%s]" , response .text )
140143 except ConnectionError as exc :
141- _log .warning (f"Connection Error accessing { url } " )
144+ LOGGER .warning (f"Connection Error accessing { url } " )
142145 raise ConnectionError (
143146 "ConnectionError - host not found"
144147 ) from exc
@@ -152,4 +155,4 @@ async def set_register(
152155 ) as exc :
153156 raise ConnectionError () from exc
154157 except Exception as unknown_error :
155- _log .error ("Unhandled Exception %s" , type (unknown_error ))
158+ LOGGER .error ("Unhandled Exception %s" , type (unknown_error ))
0 commit comments