File tree 3 files changed +32
-2
lines changed
3 files changed +32
-2
lines changed Original file line number Diff line number Diff line change 15
15
CodeError ,
16
16
CommandError ,
17
17
CredentialError ,
18
+ DeviceDisconnectedError ,
18
19
InvalidToken ,
19
20
LockError ,
20
21
ParseError ,
@@ -622,8 +623,14 @@ def query(self, query):
622
623
# Bail-out if the query is not recognized
623
624
raise QueryNotValid ()
624
625
625
- response = self ._session .post (endpoint , data = {"sessionId" : self ._session_id })
626
- response .raise_for_status ()
626
+ try :
627
+ response = self ._session .post (endpoint , data = {"sessionId" : self ._session_id })
628
+ response .raise_for_status ()
629
+ except HTTPError as err :
630
+ # Handle the case when the device is disconnected
631
+ if err .response .status_code == 403 and "Centrale non connessa" in err .response .text :
632
+ raise DeviceDisconnectedError
633
+ raise err
627
634
628
635
if query in [q .SECTORS , q .INPUTS , q .OUTPUTS ]:
629
636
# Retrieve description or use the cache
Original file line number Diff line number Diff line change @@ -74,3 +74,9 @@ class CommandError(APIException):
74
74
"""Exception raised when the API returns an error response after issuing a command."""
75
75
76
76
default_message = "An error occurred while executing the command."
77
+
78
+
79
+ class DeviceDisconnectedError (APIException ):
80
+ """Exception raised when the device is disconnected."""
81
+
82
+ default_message = "Unable to execute commands. Device is disconnected."
Original file line number Diff line number Diff line change 10
10
CodeError ,
11
11
CommandError ,
12
12
CredentialError ,
13
+ DeviceDisconnectedError ,
13
14
InvalidToken ,
14
15
LockError ,
15
16
LockNotAcquired ,
@@ -2414,6 +2415,22 @@ def test_client_query_invalid_response(server, mocker):
2414
2415
client .query (query .SECTORS )
2415
2416
2416
2417
2418
+ def test_client_query_unit_disconnected (server , mocker ):
2419
+ # Ensure that the client catches and raises an exception when the unit is disconnected
2420
+ server .add (
2421
+ responses .POST ,
2422
+ "https://example.com/api/areas" ,
2423
+ body = '"Centrale non connessa"' ,
2424
+ status = 403 ,
2425
+ )
2426
+ client = ElmoClient (base_url = "https://example.com" , domain = "domain" )
2427
+ client ._session_id = "test"
2428
+ mocker .patch .object (client , "_get_descriptions" )
2429
+ # Test
2430
+ with pytest .raises (DeviceDisconnectedError ):
2431
+ client .query (query .SECTORS )
2432
+
2433
+
2417
2434
def test_client_get_alerts_status (server ):
2418
2435
"""Should query a Elmo system to retrieve alerts status."""
2419
2436
html = """
You can’t perform that action at this time.
0 commit comments