Skip to content

Commit 5afd849

Browse files
authored
feat(api): handle main unit disconnections (#147)
1 parent c1807d1 commit 5afd849

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/elmo/api/client.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
CodeError,
1616
CommandError,
1717
CredentialError,
18+
DeviceDisconnectedError,
1819
InvalidToken,
1920
LockError,
2021
ParseError,
@@ -622,8 +623,14 @@ def query(self, query):
622623
# Bail-out if the query is not recognized
623624
raise QueryNotValid()
624625

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
627634

628635
if query in [q.SECTORS, q.INPUTS, q.OUTPUTS]:
629636
# Retrieve description or use the cache

src/elmo/api/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ class CommandError(APIException):
7474
"""Exception raised when the API returns an error response after issuing a command."""
7575

7676
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."

tests/test_client.py

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
CodeError,
1111
CommandError,
1212
CredentialError,
13+
DeviceDisconnectedError,
1314
InvalidToken,
1415
LockError,
1516
LockNotAcquired,
@@ -2414,6 +2415,22 @@ def test_client_query_invalid_response(server, mocker):
24142415
client.query(query.SECTORS)
24152416

24162417

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+
24172434
def test_client_get_alerts_status(server):
24182435
"""Should query a Elmo system to retrieve alerts status."""
24192436
html = """

0 commit comments

Comments
 (0)