@@ -44,9 +44,9 @@ def run(self, once: bool = True) -> bool:
4444 self ._connect ()
4545 try :
4646 self ._run (once )
47- except Exception as e :
48- _LOGGER .warning ("Exception in run loop" , e )
49- sleep (RECONNECT_DELAY )
47+ except :
48+ _LOGGER .warning ("Exception in run loop" )
49+ sleep (RECONNECT_DELAY )
5050 return not self ._loop and self ._measurements_counter > 0
5151
5252 def _connect (self ) -> bool :
@@ -105,19 +105,23 @@ def _run(self, once: bool = True) -> bool:
105105
106106 if once and len (self ._measurements ) > 0 :
107107 self ._loop = False
108-
108+
109109 return (once and len (self ._measurements ) > 0 ) or not once
110110
111111 def _handle_response (self , data ):
112112 if data [0 ] != 0 and data [1 ] != 0 and data [2 ] != 0 and data [2 ] != 3 :
113- _LOGGER .warning ("Ignore data package because invalid prefix: %s" % data [0 :3 ])
113+ _LOGGER .warning (
114+ "Ignore data package because invalid prefix: %s" % data [0 :3 ]
115+ )
114116 return
115117 data_length = data [4 ]
116118 if len (data ) != data_length + 5 :
117119 if len (data ) > data_length :
118120 additional_data = data [data_length : len (data )]
119121 data = data [0 :data_length ]
120- _LOGGER .debug ("Split into two data packages because additional data detected." )
122+ _LOGGER .debug (
123+ "Split into two data packages because additional data detected."
124+ )
121125 self ._handle_response (additional_data )
122126 else :
123127 _LOGGER .warning (
@@ -148,7 +152,7 @@ def _handle_passcode_response(self, data):
148152 pass
149153 _LOGGER .warning ("Passcode resonse ignored" )
150154
151- def _handle_login_response (self , data ):
155+ def _handle_login_response (self , data ):
152156 pass
153157 _LOGGER .warning ("Login resonse ignored" )
154158
@@ -160,7 +164,7 @@ def _handle_data_response(self, data):
160164 else :
161165 pass
162166 _LOGGER .debug (meas )
163-
167+
164168 def _handle_data_extended_response (self , data ):
165169 pass
166170 _LOGGER .warning ("Extended data ignored" )
@@ -193,8 +197,12 @@ def abort(self):
193197 def close (self ):
194198 self ._socket .close ()
195199
196- def get_result (self ):
197- return str (self .result )
200+ def get_latest_measurement_and_empty (self ) -> Measurement :
201+ if len (self ._measurements ) > 0 :
202+ m = self ._measurements .pop ()
203+ self ._measurements .clear ()
204+ return m
205+ return None
198206
199207 def __enter__ (self ):
200208 self .run ()
@@ -203,6 +211,7 @@ def __enter__(self):
203211 def __exit__ (self , type , value , traceback ):
204212 self ._socket .close ()
205213
214+
206215class Measurement :
207216 def __init__ (self , data ) -> None :
208217 flag1 = data [8 ]
@@ -220,7 +229,10 @@ def __init__(self, data) -> None:
220229 self .unknown2 = int .from_bytes (unknown2_raw , "big" )
221230
222231 def __str__ (self ) -> str :
223- return (
224- "pH: %s, Redox: %s, In-water: %s, pH-on: %s, Orp-on: %s"
225- % (self .ph , self .redox , self .in_water , self .ph_on , self .orp_on )
232+ return "pH: %s, Redox: %s, In-water: %s, pH-on: %s, Orp-on: %s" % (
233+ self .ph ,
234+ self .redox ,
235+ self .in_water ,
236+ self .ph_on ,
237+ self .orp_on ,
226238 )
0 commit comments