44from typing import Callable
55
66if __package__ == "" :
7- from tion_btle .tion import tion , TionException
7+ from tion_btle .tion import tion , TionException , TionExceptionGet
88else :
9- from .tion import tion , TionException
9+ from .tion import tion , TionException , TionExceptionGet
1010
1111import time
1212
@@ -108,8 +108,6 @@ async def process_mode(mode_code: int) -> str:
108108 finally :
109109 return result
110110
111- result : dict = {}
112-
113111 try :
114112 await self ._do_action (self ._connect )
115113 await self ._enable_notifications ()
@@ -131,12 +129,13 @@ async def process_mode(mode_code: int) -> str:
131129 result = await decode_response (byte_response )
132130
133131 except exc .BleakError as e :
134- _LOGGER .debug ("Got %s while waiting for notification" , str (e ))
132+ _LOGGER .error ("Got %s while waiting for notification" , str (e ))
135133 await self ._disconnect ()
134+ raise TionExceptionGet ('get' , str (e ))
136135 except TionException as e :
137136 _LOGGER .error (str (e ))
138- result = {"code" : 400 , "error" : "Got exception " + str (e )}
139137 await self ._disconnect ()
138+ raise e
140139
141140 if not keep_connection :
142141 await self ._disconnect ()
@@ -158,7 +157,11 @@ async def encode_status(status: str) -> int:
158157 except KeyError :
159158 pass
160159
161- current_settings = await self .get (True )
160+ try :
161+ current_settings = await self .get (True )
162+ except TionExceptionGet :
163+ raise TionException ('set' , 'Could not get current settings!' )
164+
162165 _LOGGER .debug ("set: got '%s' settings" % current_settings )
163166 settings = {** current_settings , ** request }
164167 new_settings = await self .create_command (self .command_SET_PARAMS )
@@ -170,7 +173,14 @@ async def encode_status(status: str) -> int:
170173 return new_settings
171174 try :
172175 await self ._do_action (self ._connect )
173- await self ._do_action (self ._try_write , request = await encode_request (request ))
176+ try :
177+ encoded_request = await encode_request (request )
178+ except (KeyError , TionException ) as e :
179+ _LOGGER .warning ("Could not create encoded settings command: %s" % str (e ))
180+ return
181+
182+ await self ._do_action (self ._try_write , request = encoded_request )
183+
174184 except TionException as e :
175185 _LOGGER .error (str (e ))
176186 finally :
0 commit comments