async def test_state(aresponses):
    """Test active state is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/info/mode",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("info-mode.json"),
        ),
    )

    aresponses.add(
        MATCH_HOST,
        "/tv/getTuned",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("tv-get-tuned.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv.state()

        assert response
        assert isinstance(response, State)
        assert response.available
        assert not response.standby

        assert isinstance(response.program, Program)
Esempio n. 2
0
async def test_text_request(aresponses):
    """Test non JSON response is handled correctly."""
    aresponses.add(
        MATCH_HOST, "/", "GET", aresponses.Response(status=200, text="OK"),
    )
    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv._request("/")
        assert response == "OK"
Esempio n. 3
0
async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """
    session = async_get_clientsession(hass)
    directv = DIRECTV(data[CONF_HOST], session=session)
    device = await directv.update()

    return {CONF_RECEIVER_ID: device.info.receiver_id}
Esempio n. 4
0
async def test_http_error403(aresponses):
    """Test HTTP 403 response handling."""
    aresponses.add(
        MATCH_HOST,
        "/tv/getTuned",
        "GET",
        aresponses.Response(text="Forbidden", status=403),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        with pytest.raises(DIRECTVAccessRestricted):
            assert await dtv._request("/tv/getTuned")
Esempio n. 5
0
async def test_http_error404(aresponses):
    """Test HTTP 404 response handling."""
    aresponses.add(
        MATCH_HOST,
        "/info/getVersion",
        "GET",
        aresponses.Response(text="Not Found!", status=404),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        with pytest.raises(DIRECTVError):
            assert await dtv._request("/info/getVersion")
Esempio n. 6
0
async def test_http_error500(aresponses):
    """Test HTTP 500 response handling."""
    aresponses.add(
        MATCH_HOST,
        "/info/getVersion",
        "GET",
        aresponses.Response(text="Internal Server Error", status=500),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        with pytest.raises(DIRECTVError):
            assert await dtv._request("/info/getVersion")
Esempio n. 7
0
async def test_timeout(aresponses):
    """Test request timeout from the DIRECTV server."""
    # Faking a timeout by sleeping
    async def response_handler(_):
        await asyncio.sleep(2)
        return aresponses.Response(body="Timeout!")

    aresponses.add(
        MATCH_HOST, "/info/getVersion", "GET", response_handler,
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session, request_timeout=1)
        with pytest.raises(DIRECTVConnectionError):
            assert await dtv._request("/info/getVersion")
Esempio n. 8
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up DirecTV from a config entry."""
    dtv = DIRECTV(entry.data[CONF_HOST], session=async_get_clientsession(hass))

    try:
        await dtv.update()
    except DIRECTVError as err:
        raise ConfigEntryNotReady from err

    hass.data.setdefault(DOMAIN, {})
    hass.data[DOMAIN][entry.entry_id] = dtv

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Esempio n. 9
0
async def test_authenticated_request(aresponses):
    """Test authenticated JSON response is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text='{"status": "ok"}',
        ),
    )
    async with ClientSession() as session:
        dtv = DIRECTV(HOST, username="******", password="******", session=session,)
        response = await dtv._request("/")
        assert response["status"] == "ok"
Esempio n. 10
0
async def test_tune(aresponses):
    """Test tune is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/tv/tune",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("tv-tune.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        await dtv.tune("231")
Esempio n. 11
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up DirecTV from a config entry."""
    dtv = DIRECTV(entry.data[CONF_HOST], session=async_get_clientsession(hass))

    try:
        await dtv.update()
    except DIRECTVError:
        raise ConfigEntryNotReady

    hass.data[DOMAIN][entry.entry_id] = dtv

    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component))

    return True
Esempio n. 12
0
async def test_remote(aresponses):
    """Test remote is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/remote/processKey",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("remote-process-key.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        await dtv.remote("info")
Esempio n. 13
0
async def test_internal_session(aresponses):
    """Test DIRECTV response is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/info/getVersion",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text='{"status": {"code": 200, "commandResult": 0}}',
        ),
    )

    async with DIRECTV(HOST) as dtv:
        response = await dtv._request("/info/getVersion")
        assert response["status"]["code"] == 200
        assert response["status"]["commandResult"] == 0
Esempio n. 14
0
async def test_request_port(aresponses):
    """Test the DIRECTV server running on non-standard port."""
    aresponses.add(
        f"{HOST}:{NON_STANDARD_PORT}",
        "/info/getVersion",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text='{"status": {"code": 200, "commandResult": 0}}',
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(host=HOST, port=NON_STANDARD_PORT, session=session,)
        response = await dtv._request("/info/getVersion")
        assert response["status"]["code"] == 200
        assert response["status"]["commandResult"] == 0
Esempio n. 15
0
async def test_status_access_restricted(aresponses):
    """Test unauthorized state is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/info/mode",
        "GET",
        aresponses.Response(
            status=403,
            headers={"Content-Type": "application/json"},
            text=load_fixture("info-mode-restricted.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv.status()

        assert response == "unauthorized"
Esempio n. 16
0
async def test_status_unavailable(aresponses):
    """Test unavailable status is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/info/mode",
        "GET",
        aresponses.Response(
            status=500,
            headers={"Content-Type": "application/json"},
            text=load_fixture("info-mode-error.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv.status()

        assert response == "unavailable"
Esempio n. 17
0
async def test_tuned(aresponses):
    """Test tuned is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/tv/getTuned",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("tv-get-tuned.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv.tuned()

        assert response
        assert isinstance(response, Program)
Esempio n. 18
0
async def test_http_error500_json(aresponses):
    """Test HTTP 500 json response handling."""
    aresponses.add(
        MATCH_HOST,
        "/info/getVersion",
        "GET",
        aresponses.Response(
            status=500,
            headers={"Content-Type": "application/json"},
            body=load_fixture("info-get-version-error.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        with pytest.raises(DIRECTVError):
            response = await dtv._request("/info/getVersion")
            assert response
            assert response["status"]
            assert response["status"]["code"] == 500
            assert response["status"]["commandResult"] == 1
Esempio n. 19
0
async def test_update(aresponses):
    """Test update is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/info/getVersion",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("info-get-version.json"),
        ),
    )

    aresponses.add(
        MATCH_HOST,
        "/info/getLocations",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("info-get-locations.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv.update()

        assert response
        assert isinstance(response.info, Info)
        assert isinstance(response.locations, List)

        response = await dtv.update()

        assert response
        assert response.info
Esempio n. 20
0
async def test_state_restricted_mode(aresponses):
    """Test standby state is handled correctly."""
    aresponses.add(
        MATCH_HOST,
        "/info/mode",
        "GET",
        aresponses.Response(
            status=403,
            headers={"Content-Type": "application/json"},
            text=load_fixture("info-mode-restricted.json"),
        ),
    )

    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        response = await dtv.state()

        assert response
        assert isinstance(response, State)
        assert not response.available
        assert response.standby
        assert not response.authorized

        assert response.program is None
Esempio n. 21
0
async def test_client_error():
    """Test http client error."""
    async with ClientSession() as session:
        dtv = DIRECTV("#", session=session)
        with pytest.raises(DIRECTVConnectionError):
            assert await dtv._request("/info/getVersion")
Esempio n. 22
0
async def test_remote_invalid_key():
    """Test remote with invalid key is handled correctly."""
    async with ClientSession() as session:
        dtv = DIRECTV(HOST, session=session)
        with pytest.raises(DIRECTVError):
            await dtv.remote("super")