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

Commit d888ee1

Browse files
authored
Handle error 404 when looping on the sites (#57)
See home-assistant/core#42526
1 parent 69025b2 commit d888ee1

6 files changed

+38
-4
lines changed

.codeclimate.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "2" # required to adjust maintainability checks
2+
checks:
3+
method-complexity:
4+
config:
5+
threshold: 10

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### 0.9.1
2+
* Handle error 404 when site has not device
13
### 0.9.0
24
* Add parent id support
35
### 0.8.0

pymfy/api/somfy_api.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from json import JSONDecodeError
12
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
23

34
from oauthlib.oauth2 import TokenExpiredError
@@ -62,10 +63,18 @@ def get_devices(
6263
devices = [] # type: List[Device]
6364
for s_id in site_ids:
6465
response = self.get("/site/" + s_id + "/device")
65-
response.raise_for_status()
66+
try:
67+
content = response.json()
68+
except JSONDecodeError:
69+
response.raise_for_status()
70+
71+
if response.status_code != 200:
72+
# Can happen when the site does not contain any device
73+
continue
74+
6675
devices += [
6776
Device(**d)
68-
for d in response.json()
77+
for d in content
6978
if category is None or category.value in Device(**d).categories
7079
]
7180
return devices

tests/get_devices_3.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"uid": "XXXXX",
3+
"message": "definition_not_found",
4+
"data": null
5+
}

tests/get_sites.json

+4
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
{
77
"id": "site-2",
88
"label": "Conexoon"
9+
},
10+
{
11+
"id": "site-3",
12+
"label": "Other"
913
}
1014
]

tests/test_somfy_api.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_get_sites(self, api):
2222
httpretty.GET, BASE_URL + "/site", body=get_sites.read()
2323
)
2424
sites = api.get_sites()
25-
assert len(sites) == 2
25+
assert len(sites) == 3
2626
assert sites[0].id == "site-1"
2727
assert sites[0].label == "TaHoma"
2828
assert sites[1].id == "site-2"
@@ -43,9 +43,12 @@ def test_devices(self, api):
4343
sites_path = os.path.join(CURRENT_DIR, "get_sites.json")
4444
devices_path_1 = os.path.join(CURRENT_DIR, "get_devices_1.json")
4545
devices_path_2 = os.path.join(CURRENT_DIR, "get_devices_2.json")
46+
devices_path_3 = os.path.join(CURRENT_DIR, "get_devices_3.json")
4647
with open(sites_path, "r") as get_sites, open(
4748
devices_path_1, "r"
48-
) as get_devices_1, open(devices_path_2, "r") as get_devices_2:
49+
) as get_devices_1, open(devices_path_2, "r") as get_devices_2, open(
50+
devices_path_3, "r"
51+
) as get_devices_3:
4952
httpretty.register_uri(
5053
httpretty.GET, BASE_URL + "/site", body=get_sites.read()
5154
)
@@ -59,6 +62,12 @@ def test_devices(self, api):
5962
BASE_URL + "/site/site-2/device",
6063
body=get_devices_2.read(),
6164
)
65+
httpretty.register_uri(
66+
httpretty.GET,
67+
BASE_URL + "/site/site-3/device",
68+
body=get_devices_3.read(),
69+
status=404,
70+
)
6271

6372
assert len(api.get_devices()) == 4
6473
assert len(api.get_devices("site-1")) == 3

0 commit comments

Comments
 (0)