Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Handle error 404 when looping on the sites (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
tetienne authored Nov 9, 2020
1 parent 69025b2 commit d888ee1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2" # required to adjust maintainability checks
checks:
method-complexity:
config:
threshold: 10
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### 0.9.1
* Handle error 404 when site has not device
### 0.9.0
* Add parent id support
### 0.8.0
Expand Down
13 changes: 11 additions & 2 deletions pymfy/api/somfy_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from json import JSONDecodeError
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from oauthlib.oauth2 import TokenExpiredError
Expand Down Expand Up @@ -62,10 +63,18 @@ def get_devices(
devices = [] # type: List[Device]
for s_id in site_ids:
response = self.get("/site/" + s_id + "/device")
response.raise_for_status()
try:
content = response.json()
except JSONDecodeError:
response.raise_for_status()

if response.status_code != 200:
# Can happen when the site does not contain any device
continue

devices += [
Device(**d)
for d in response.json()
for d in content
if category is None or category.value in Device(**d).categories
]
return devices
Expand Down
5 changes: 5 additions & 0 deletions tests/get_devices_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"uid": "XXXXX",
"message": "definition_not_found",
"data": null
}
4 changes: 4 additions & 0 deletions tests/get_sites.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
{
"id": "site-2",
"label": "Conexoon"
},
{
"id": "site-3",
"label": "Other"
}
]
13 changes: 11 additions & 2 deletions tests/test_somfy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_get_sites(self, api):
httpretty.GET, BASE_URL + "/site", body=get_sites.read()
)
sites = api.get_sites()
assert len(sites) == 2
assert len(sites) == 3
assert sites[0].id == "site-1"
assert sites[0].label == "TaHoma"
assert sites[1].id == "site-2"
Expand All @@ -43,9 +43,12 @@ def test_devices(self, api):
sites_path = os.path.join(CURRENT_DIR, "get_sites.json")
devices_path_1 = os.path.join(CURRENT_DIR, "get_devices_1.json")
devices_path_2 = os.path.join(CURRENT_DIR, "get_devices_2.json")
devices_path_3 = os.path.join(CURRENT_DIR, "get_devices_3.json")
with open(sites_path, "r") as get_sites, open(
devices_path_1, "r"
) as get_devices_1, open(devices_path_2, "r") as get_devices_2:
) as get_devices_1, open(devices_path_2, "r") as get_devices_2, open(
devices_path_3, "r"
) as get_devices_3:
httpretty.register_uri(
httpretty.GET, BASE_URL + "/site", body=get_sites.read()
)
Expand All @@ -59,6 +62,12 @@ def test_devices(self, api):
BASE_URL + "/site/site-2/device",
body=get_devices_2.read(),
)
httpretty.register_uri(
httpretty.GET,
BASE_URL + "/site/site-3/device",
body=get_devices_3.read(),
status=404,
)

assert len(api.get_devices()) == 4
assert len(api.get_devices("site-1")) == 3
Expand Down

0 comments on commit d888ee1

Please sign in to comment.