Skip to content

Commit 8e08fde

Browse files
authored
Add status method for basic state (#8)
* add status method for basic state. * Update test_interface.py
1 parent e54b206 commit 8e08fde

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

directv/directv.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ async def state(self, client: str = "0") -> State:
196196
program=program,
197197
)
198198

199+
async def status(self, client: str = "0") -> str:
200+
"""Get basic status of receiver client."""
201+
try:
202+
mode = await self._request("info/mode", params={"clientAddr": client})
203+
return "standby" if mode["mode"] == 1 else "active"
204+
except DIRECTVAccessRestricted:
205+
return "unauthorized"
206+
except DIRECTVError:
207+
return "unavailable"
208+
199209
async def tune(self, channel: str, client: str = "0") -> None:
200210
"""Change the channel on the receiver."""
201211
major, minor = parse_channel_number(channel)

tests/test_interface.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,90 @@ async def test_state_standby(aresponses):
274274
assert response.program is None
275275

276276

277+
@pytest.mark.asyncio
278+
async def test_status(aresponses):
279+
"""Test active state is handled correctly."""
280+
aresponses.add(
281+
MATCH_HOST,
282+
"/info/mode",
283+
"GET",
284+
aresponses.Response(
285+
status=200,
286+
headers={"Content-Type": "application/json"},
287+
text=load_fixture("info-mode.json"),
288+
),
289+
)
290+
291+
async with ClientSession() as session:
292+
dtv = DIRECTV(HOST, session=session)
293+
response = await dtv.status()
294+
295+
assert response == "active"
296+
297+
298+
@pytest.mark.asyncio
299+
async def test_status_access_restricted(aresponses):
300+
"""Test unauthorized state is handled correctly."""
301+
aresponses.add(
302+
MATCH_HOST,
303+
"/info/mode",
304+
"GET",
305+
aresponses.Response(
306+
status=403,
307+
headers={"Content-Type": "application/json"},
308+
text=load_fixture("info-mode-restricted.json"),
309+
),
310+
)
311+
312+
async with ClientSession() as session:
313+
dtv = DIRECTV(HOST, session=session)
314+
response = await dtv.status()
315+
316+
assert response == "unauthorized"
317+
318+
319+
@pytest.mark.asyncio
320+
async def test_status_standby(aresponses):
321+
"""Test standby status is handled correctly."""
322+
aresponses.add(
323+
MATCH_HOST,
324+
"/info/mode",
325+
"GET",
326+
aresponses.Response(
327+
status=200,
328+
headers={"Content-Type": "application/json"},
329+
text=load_fixture("info-mode-standby.json"),
330+
),
331+
)
332+
333+
async with ClientSession() as session:
334+
dtv = DIRECTV(HOST, session=session)
335+
response = await dtv.status()
336+
337+
assert response == "standby"
338+
339+
340+
@pytest.mark.asyncio
341+
async def test_status_unavailable(aresponses):
342+
"""Test unavailable status is handled correctly."""
343+
aresponses.add(
344+
MATCH_HOST,
345+
"/info/mode",
346+
"GET",
347+
aresponses.Response(
348+
status=500,
349+
headers={"Content-Type": "application/json"},
350+
text=load_fixture("info-mode-error.json"),
351+
),
352+
)
353+
354+
async with ClientSession() as session:
355+
dtv = DIRECTV(HOST, session=session)
356+
response = await dtv.status()
357+
358+
assert response == "unavailable"
359+
360+
277361
@pytest.mark.asyncio
278362
async def test_tune(aresponses):
279363
"""Test tune is handled correctly."""

0 commit comments

Comments
 (0)