Skip to content

Commit d755ba6

Browse files
committed
Merge branch 'fix-status-off'
2 parents f326600 + 9b10a13 commit d755ba6

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

recuair_cli/main.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class Status(NamedTuple):
6363

6464
device: str
6565
name: str
66-
temperature_in: int
67-
humidity_in: int
68-
temperature_out: int
66+
temperature_in: Optional[int]
67+
humidity_in: Optional[int]
68+
temperature_out: Optional[int]
6969
mode: str
70-
co2_ppm: int
70+
co2_ppm: Optional[int]
7171
filter: int
7272
fan: int
7373
light: int
@@ -78,6 +78,14 @@ def _strip_unit(value: str) -> str:
7878
return value.strip().partition(" ")[0]
7979

8080

81+
def _int_or_none(value: str) -> Optional[int]:
82+
"""Convert value to int or return None."""
83+
if value == "-":
84+
return None
85+
else:
86+
return int(value)
87+
88+
8189
async def get_status(client: httpx.AsyncClient, device: str) -> Status:
8290
"""Return device status."""
8391
try:
@@ -119,11 +127,11 @@ async def get_status(client: httpx.AsyncClient, device: str) -> Status:
119127
return Status(
120128
device=device,
121129
name=cast(Tag, content.find(class_="deviceName")).text,
122-
temperature_in=int(_strip_unit(temp_in)),
123-
humidity_in=int(_strip_unit(humi_in)),
124-
temperature_out=int(_strip_unit(temp_out)),
130+
temperature_in=_int_or_none(_strip_unit(temp_in)),
131+
humidity_in=_int_or_none(_strip_unit(humi_in)),
132+
temperature_out=_int_or_none(_strip_unit(temp_out)),
125133
mode=mode_raw.strip(),
126-
co2_ppm=int(_strip_unit(co2_raw)),
134+
co2_ppm=_int_or_none(_strip_unit(co2_raw)),
127135
filter=100 - int(_strip_unit(filter_raw)),
128136
fan=100 - int(_strip_unit(fan_raw)),
129137
light=int(light_raw),

recuair_cli/tests/data/response-off.html

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

recuair_cli/tests/test_main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
class GetStatusTest(IsolatedAsyncioTestCase):
1616
async def test(self):
17+
# Test response.
1718
with respx.mock() as rsps:
1819
with open(_DATA / "response.html", "rb") as file:
1920
rsps.get("http://example/").mock(Response(200, content=file.read()))
@@ -35,6 +36,29 @@ async def test(self):
3536
)
3637
self.assertEqual(status, result)
3738

39+
async def test_off(self):
40+
# Test response from stopped device
41+
with respx.mock() as rsps:
42+
with open(_DATA / "response-off.html", "rb") as file:
43+
rsps.get("http://example/").mock(Response(200, content=file.read()))
44+
45+
async with httpx.AsyncClient() as client:
46+
status = await get_status(client, "example")
47+
48+
result = Status(
49+
device="example",
50+
name="Holly",
51+
temperature_in=None,
52+
humidity_in=None,
53+
temperature_out=None,
54+
mode="AUTO",
55+
co2_ppm=None,
56+
filter=2,
57+
fan=69,
58+
light=5,
59+
)
60+
self.assertEqual(status, result)
61+
3862
async def test_invalid(self):
3963
# Test case with '%%content%%' in the response.
4064
with respx.mock() as rsps:

0 commit comments

Comments
 (0)