@@ -43,14 +43,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
43
43
client = NefitEasy (hass , credentials )
44
44
45
45
await client .connect ()
46
- _LOGGER .debug ("Is connected state? %s" , client .connected_state )
47
-
48
46
if client .connected_state == STATE_CONNECTION_VERIFIED :
49
47
hass .data [DOMAIN ][entry .entry_id ]["client" ] = client
50
- _LOGGER .info (
51
- "Successfully connected %s to Nefit device!" ,
52
- credentials .get (CONF_SERIAL ),
53
- )
54
48
else :
55
49
raise ConfigEntryNotReady
56
50
@@ -66,23 +60,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
66
60
67
61
async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
68
62
"""Unload nefit easy component."""
69
- if not all (
70
- await asyncio .gather (
71
- * (
72
- hass .config_entries .async_forward_entry_unload (entry , component )
73
- for component in DOMAINS
74
- )
75
- )
76
- ):
77
- return False
63
+ unload_ok = await hass .config_entries .async_unload_platforms (entry , DOMAINS )
78
64
79
- client = hass .data [DOMAIN ][entry .entry_id ]["client" ]
65
+ if unload_ok :
66
+ client = hass .data [DOMAIN ][entry .entry_id ]["client" ]
80
67
81
- await client .shutdown ("Unload entry" )
68
+ await client .shutdown ("Unload entry" )
82
69
83
- hass .data [DOMAIN ].pop (entry .entry_id )
70
+ hass .data [DOMAIN ].pop (entry .entry_id )
84
71
85
- return True
72
+ return unload_ok
86
73
87
74
88
75
class NefitEasy (DataUpdateCoordinator ):
@@ -139,85 +126,49 @@ async def add_key(self, entity_description: NefitEntityDescription) -> None:
139
126
140
127
async def connect (self ) -> None :
141
128
"""Connect to nefit easy."""
142
- _LOGGER .debug ("Starting connecting. ." )
129
+ _LOGGER .debug ("Start connecting." )
143
130
if not self .is_connecting :
144
131
self .is_connecting = True
145
- retries_connection = 0
146
132
147
- while self .connected_state != STATE_CONNECTED and retries_connection < 3 :
148
- await self .nefit .connect ()
149
- _LOGGER .debug ("Waiting for connected event" )
133
+ await self .nefit .connect ()
134
+ _LOGGER .debug ("Waiting for connected event." )
135
+ try :
136
+ await asyncio .wait_for (
137
+ self .nefit .xmppclient .connected_event .wait (), timeout = 29.0
138
+ )
139
+ except asyncio .TimeoutError :
140
+ _LOGGER .debug ("TimeoutError on waiting for connected event." )
141
+ except : # noqa: E722 pylint: disable=bare-except
142
+ _LOGGER .debug ("Unknown error." )
143
+ else :
144
+ _LOGGER .debug ("Connected successfully." )
145
+ self .connected_state = STATE_CONNECTED
146
+
147
+ if self .connected_state == STATE_CONNECTED :
148
+ self .nefit .get ("/gateway/brandID" )
150
149
try :
151
150
await asyncio .wait_for (
152
- self .nefit .xmppclient .connected_event .wait (), timeout = 29.0
153
- )
154
- self .connected_state = STATE_CONNECTED
155
- _LOGGER .debug ("adding stop listener" )
156
- self .hass .bus .async_listen_once (
157
- EVENT_HOMEASSISTANT_STOP , self .shutdown
151
+ self .nefit .xmppclient .message_event .wait (), timeout = 29.0
158
152
)
159
153
except asyncio .TimeoutError :
160
154
_LOGGER .debug (
161
- "TimeoutError on waiting for connected event (connection retries=%d)" ,
162
- retries_connection ,
155
+ "Did not get a response in time for testing connection."
163
156
)
164
- retries_connection = retries_connection + 1
165
157
except : # noqa: E722 pylint: disable=bare-except
166
- _LOGGER .debug ("Unknown error" )
167
-
168
- # test password for decrypting messages if connected
169
- if self .connected_state == STATE_CONNECTED :
170
- _LOGGER .info (
171
- "Testing connection (connect retries=%d)" , retries_connection
172
- )
173
- retries_validation = 0
174
- while (
175
- self .connected_state != STATE_CONNECTION_VERIFIED
176
- and retries_validation < 3
177
- ):
178
- try :
179
- self .nefit .get ("/gateway/brandID" )
180
- await asyncio .wait_for (
181
- self .nefit .xmppclient .message_event .wait (), timeout = 29.0
182
- )
183
- self .nefit .xmppclient .message_event .clear ()
184
-
185
- if self .connected_state == STATE_ERROR_AUTH :
186
- self .is_connecting = False
187
- return
158
+ _LOGGER .debug ("No connection while testing connection." )
159
+ else :
160
+ self .nefit .xmppclient .message_event .clear ()
188
161
162
+ # No exception and no auth error
163
+ if self .connected_state == STATE_CONNECTED :
189
164
self .connected_state = STATE_CONNECTION_VERIFIED
190
- _LOGGER .info (
191
- "Connected %s with %d retries and %d test retries." ,
192
- self .serial ,
193
- retries_connection ,
194
- retries_validation ,
195
- )
196
- except asyncio .TimeoutError :
197
- _LOGGER .error (
198
- "Did not get a response in time for testing connection (validation retries=%d)." ,
199
- retries_validation ,
200
- )
201
- retries_validation = retries_validation + 1
202
- except : # noqa: E722 pylint: disable=bare-except
203
- _LOGGER .error ("No connection while testing connection." )
204
- break
205
165
206
166
if self .connected_state != STATE_CONNECTION_VERIFIED :
207
- self .hass .components .persistent_notification .create (
208
- f"Did not succeed in connecting { self .serial } to Bosch cloud after retrying 3 times. Retry in 30 seconds." ,
209
- title = "Nefit connect error" ,
210
- notification_id = "nefit_connect_error" ,
211
- )
212
- self .is_connecting = False
213
-
214
- # wait 30 seconds to retry
215
- await asyncio .sleep (30 )
216
- await self .connect ()
167
+ _LOGGER .debug ("Successfully verified connection." )
217
168
218
169
self .is_connecting = False
219
170
else :
220
- _LOGGER .debug ("Connection procedure was already running. ." )
171
+ _LOGGER .debug ("Connection procedure is already running." )
221
172
222
173
async def shutdown (self , event : str ) -> None :
223
174
"""Shutdown."""
@@ -250,23 +201,20 @@ async def failed_auth_handler(self, event: str) -> None:
250
201
notification_id = "nefit_logon_error" ,
251
202
)
252
203
204
+ if self .config_entry :
205
+ self .config_entry .async_start_reauth (self .hass )
206
+
253
207
async def session_end_callback (self ) -> None :
254
208
"""If connection is closed unexpectedly, try to reconnect."""
255
209
if not self .expected_end :
256
- self .hass .components .persistent_notification .create (
257
- f"Unexpected disconnect of { self .serial } with Bosch server. Try to reconnect.." ,
258
- title = "Nefit disconnect" ,
259
- notification_id = "nefit_disconnect" ,
210
+ _LOGGER .debug (
211
+ f"Unexpected disconnect of { self .serial } with Bosch server. Try to reconnect.."
260
212
)
261
213
262
- _LOGGER .info ("Starting reconnect procedure." )
263
214
# Reset values
264
215
self .connected_state = STATE_INIT
265
216
self .expected_end = False
266
217
267
- # Retry connection
268
- await self .connect ()
269
-
270
218
async def parse_message (self , data : dict [str , Any ]) -> None :
271
219
"""Message received callback function for the XMPP client."""
272
220
if (
@@ -310,7 +258,10 @@ async def parse_message(self, data: dict[str, Any]) -> None:
310
258
async def _async_update_data (self ) -> dict [str , Any ]:
311
259
"""Update data via library."""
312
260
if self .connected_state != STATE_CONNECTION_VERIFIED :
313
- raise UpdateFailed ("Nefit easy not connected!" )
261
+ _LOGGER .debug ("Starting reconnect procedure." )
262
+ await self .connect ()
263
+ if self .connected_state != STATE_CONNECTION_VERIFIED :
264
+ raise UpdateFailed ("Nefit easy not connected!" )
314
265
315
266
async with self ._lock :
316
267
_url = "/ecus/rrc/uiStatus"
0 commit comments