async def main(): """Show example of connecting to your IPP print server.""" async with IPP("ipps://EPSON761251.local:631/ipp/print") as ipp: # Get Printer Info printer = await ipp.printer() print(printer) async with IPP("ipp://hp6830.local:631/ipp/print") as ipp: # Get Printer Info printer = await ipp.printer() print(printer)
async def test_request_port(aresponses): """Test the IPP server running on non-standard port.""" aresponses.add( f"{DEFAULT_PRINTER_HOST}:{NON_STANDARD_PORT}", DEFAULT_PRINTER_PATH, "POST", aresponses.Response( status=200, headers={"Content-Type": "application/ipp"}, body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"), ), ) async with ClientSession() as session: ipp = IPP( host=DEFAULT_PRINTER_HOST, port=NON_STANDARD_PORT, base_path=DEFAULT_PRINTER_PATH, session=session, ) response = await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, ) assert response["status-code"] == 0
async def test_ipp_error_0x0503(aresponses): """Test IPP Error 0x0503 response handling.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response( status=200, headers={"Content-Type": "application/ipp"}, body=load_fixture_binary( "get-printer-attributes-error-0x0503.bin"), ), ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session) with pytest.raises(IPPVersionNotSupportedError): assert await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, )
async def test_timeout(aresponses): """Test request timeout from the IPP server.""" # Faking a timeout by sleeping async def response_handler(_): await asyncio.sleep(2) return aresponses.Response(body="Timeout!") aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", response_handler, ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session, request_timeout=1) with pytest.raises(IPPConnectionError): assert await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, )
def __init__( self, hass: HomeAssistant, *, host: str, port: int, base_path: str, tls: bool, verify_ssl: bool, ): """Initialize global IPP data updater.""" self.ipp = IPP( host=host, port=port, base_path=base_path, tls=tls, verify_ssl=verify_ssl, session=async_get_clientsession(hass, verify_ssl), ) super().__init__( hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL, )
async def test_raw(aresponses): """Test raw method is handled correctly.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response( status=200, headers={"Content-Type": "application/ipp"}, body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"), ), ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session) response = await ipp.raw( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, ) assert response assert isinstance(response, bytes)
def __init__( self, opp: OpenPeerPower, *, host: str, port: int, base_path: str, tls: bool, verify_ssl: bool, ) -> None: """Initialize global IPP data updater.""" self.ipp = IPP( host=host, port=port, base_path=base_path, tls=tls, verify_ssl=verify_ssl, session=async_get_clientsession(opp, verify_ssl), ) super().__init__( opp, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL, )
async def main(): """Show example of connecting to your IPP print server.""" async with IPP("ipps://EPSON761251.local:631/ipp/print") as ipp: response = await ipp.raw( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": [ "printer-name", "printer-type", "printer-location", "printer-info", "printer-make-and-model", "printer-state", "printer-state-message", "printer-state-reason", "printer-uri-supported", "device-uri", "printer-is-shared", ], }, }, ) with open("printer-attributes.bin", "wb") as f: f.write(response) f.close()
async def test_client_error(): """Test http client error.""" async with ClientSession() as session: ipp = IPP("#", session=session) with pytest.raises(IPPConnectionError): assert await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, )
async def validate_input(opp: OpenPeerPower, 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(opp) ipp = IPP( host=data[CONF_HOST], port=data[CONF_PORT], base_path=data[CONF_BASE_PATH], tls=data[CONF_SSL], verify_ssl=data[CONF_VERIFY_SSL], session=session, ) printer = await ipp.printer() return {CONF_SERIAL: printer.info.serial, CONF_UUID: printer.info.uuid}
async def test_printer(aresponses): """Test getting IPP printer information.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response( status=200, headers={"Content-Type": "application/ipp"}, body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"), ), ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session) printer = await ipp.printer() assert printer assert isinstance(printer, Printer)
async def test_unexpected_response(aresponses): """Test unexpected response handling.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response(text="Surprise!", status=200), ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session) with pytest.raises(IPPParseError): assert await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, )
async def test_http_error404(aresponses): """Test HTTP 404 response handling.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response(text="Not Found!", status=404), ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session) with pytest.raises(IPPError): assert await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, )
async def test_internal_session(aresponses): """Test IPP response is handled correctly.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response( status=200, headers={"Content-Type": "application/ipp"}, body=load_fixture_binary("get-printer-attributes-epsonxp6000.bin"), ), ) async with IPP(DEFAULT_PRINTER_URI) as ipp: response = await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, ) assert response["status-code"] == 0
async def test_http_error426(aresponses): """Test HTTP 426 response handling.""" aresponses.add( MATCH_DEFAULT_HOST, DEFAULT_PRINTER_PATH, "POST", aresponses.Response( text="Upgrade Required", headers={"Upgrade": "TLS/1.0, HTTP/1.1"}, status=426, ), ) async with ClientSession() as session: ipp = IPP(DEFAULT_PRINTER_URI, session=session) with pytest.raises(IPPConnectionUpgradeRequired): assert await ipp.execute( IppOperation.GET_PRINTER_ATTRIBUTES, { "operation-attributes-tag": { "requested-attributes": DEFAULT_PRINTER_ATTRIBUTES, }, }, )