def test_that_readline_raises_timeout_error(patched_recv): """ lirc.connection.lircd_connection.LircdConnection.readline Ensure that a TimeoutError is raised from readline() if the socket times out (raises socket.timeout). """ patched_recv.side_effect = socket.timeout connection = LircdConnection() with pytest.raises(TimeoutError) as error: connection.readline() # SUT assert "could not find any data on the socket" in str(error)
def test_that_readline_raises_lircd_socket_error(patched_recv): """ lirc.connection.lircd_connection.LircdConnection.readline Ensure that a general LircdSocketError is raised from readline() if the socket raises `socket.error`. """ patched_recv.side_effect = socket.error connection = LircdConnection() with pytest.raises(LircdSocketError) as error: connection.readline() # SUT assert "An error occurred while reading from the lircd socket" in str( error)
def testReceive1AsyncLines(self): ''' Receive 1000 lines using the async interface. ''' async def get_lines(raw_conn, count): nonlocal lines async with AsyncConnection(raw_conn, loop) as conn: async for keypress in conn: lines.append(keypress) if len(lines) >= count: return lines if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"%s ./dummy-server 0"' % _EXPECT] lines = [] with subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) as child: _wait_for_socket() loop = asyncio.get_event_loop() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: loop.run_until_complete(get_lines(conn, 1000)) loop.close() self.assertEqual(len(lines), 1000) self.assertEqual(lines[0], 'foo-cmd') self.assertEqual(lines[999], 'foo-cmd')
def test_that_connection_error_is_raised_on_invalid_address(): """ lirc.connection.lircd_connection.LircdConnection.connect Ensure that an invalid address raises an LircdConnectionError when we try to connect. """ with pytest.raises(LircdConnectionError): LircdConnection(address="invalid thing").connect() # SUT
def test_that_attribute_error_is_raised_on_invalid_socket(): """ lirc.connection.lircd_connection.LircdConnection.__init__ Ensure that an error is raised when an invalid socket is passed to the LircdConnection constructor. """ with pytest.raises(AttributeError): LircdConnection(socket=5) # SUT
def test_that_custom_connections_can_be_used(mock_socket): """ lirc.Client.__init__ Ensure we can pass in custom connections using the keyword argument instead of relying on the default. """ connection = LircdConnection(socket=mock_socket) client = Client(connection=connection) # SUT assert client._Client__connection == connection
def testReceiveTimeout(self): ''' Generate a TimeoutException if there is no data ''' if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"sleep 1"'] with subprocess.Popen(cmd) as child: _wait_for_socket() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: self.assertRaises(lirc.TimeoutException, conn.readline, 0.1)
def test_that_a_socket_with_an_invalid_address_raises_error(mock_socket): """ lirc.connection.lircd_connection.LircdConnection.connect Ensure that a LircdConnectionError is raised if cannot connect to lircd at the given address (if the socket raises a FileNotFoundError). """ mock_socket.connect.side_effect = FileNotFoundError with pytest.raises(LircdConnectionError) as error: LircdConnection(socket=mock_socket).connect() # SUT assert "Could not connect to lircd" in str(error)
def testReceiveOneLine(self): ''' Receive a single, translated line OK. ''' if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"echo %s"' % _PACKET_ONE] with subprocess.Popen(cmd) as child: _wait_for_socket() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: line = conn.readline() self.assertEqual(line, 'foo-cmd')
def test_that_retrieving_the_address_property_retrieves_the_address( mock_socket): """ lirc.connection.lircd_connection.LircdConnection.address Ensure that that trying to retrieve the address property retrieves the expected address. """ expected_address = "/var/does/not/exist" conn = LircdConnection(socket=mock_socket, address=expected_address) address = conn.address # SUT assert address == expected_address
def testReceiveDisconnect(self): ''' Generate a ConnectionResetError if connection is lost ''' if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"sleep 1"'] with subprocess.Popen(cmd) as child: _wait_for_socket() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: with self.assertRaises(ConnectionResetError): with self.assertCompletedBeforeTimeout(3): conn.readline(2)
def test_that_a_socket_raises_general_error(mock_socket): """ lirc.connection.lircd_connection.LircdConnection.connect Ensure that a LircdConnectionError is raised if any general exception is raised by the connection to the socket, such as an InterruptedError if a signal occurs. """ mock_socket.connect.side_effect = InterruptedError( "interrupted by a signal") with pytest.raises(LircdConnectionError) as error: LircdConnection(socket=mock_socket).connect() # SUT assert "interrupted by a signal" in str(error)
def __init__(self, name: str, connection: LircdConnection = None): """ Initialize this Remote by connecting to the lircd socket. Args: name: Name of the remote to use. Corresponds to the name of your LIRC remote. connection: An LircdConnection specifying how to connect to LIRC on your system. By default, this will choose sensible defaults depending on the operating system it is run on. See https://pypi.org/project/lirc/ for more. """ self.__name = name self.__lirc = Client( connection=connection if connection else LircdConnection())
def testReceiveAsyncExceptionReraises(self): ''' Async readline should reraise if an exception occurs during select loop ''' async def readline(raw_conn): async with AsyncConnection(raw_conn, loop) as conn: return await conn.readline() if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"sleep 1"'] with subprocess.Popen(cmd) as child: _wait_for_socket() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: with event_loop( suppress=[ConnectionResetError, TimeoutException ]) as loop: with self.assertCompletedBeforeTimeout(2): with self.assertRaises(ConnectionResetError): loop.run_until_complete(readline(conn))
def testReceiveAsyncExceptionEndsIterator(self): ''' Async iterator should stop if an exception occurs in the select loop ''' async def get_lines(raw_conn): async with AsyncConnection(raw_conn, loop) as conn: async for keypress in conn: pass if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"sleep 1"'] with subprocess.Popen(cmd) as child: _wait_for_socket() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: with event_loop( suppress=[ConnectionResetError, TimeoutException ]) as loop: with self.assertCompletedBeforeTimeout(2): self.assertIsNone( loop.run_until_complete(get_lines(conn)))
def testReceiveAsyncDisconnectDontBlock(self): ''' Do not block the loop if connection is lost ''' async def readline(raw_conn): async with AsyncConnection(raw_conn, loop) as conn: return await conn.readline() if os.path.exists(_SOCKET): os.unlink(_SOCKET) cmd = [_SOCAT, 'UNIX-LISTEN:' + _SOCKET, 'EXEC:"sleep 1"'] with subprocess.Popen(cmd) as child: _wait_for_socket() with LircdConnection('foo', socket_path=_SOCKET, lircrc_path='lircrc.conf') as conn: with event_loop( suppress=[ConnectionResetError, TimeoutException ]) as loop: with self.assertCompletedBeforeTimeout(3): with suppress(TimeoutError, ConnectionResetError): loop.run_until_complete( asyncio.wait_for(readline(conn), 2))
import RPi.GPIO as GPIO from lirc import LircdConnection import time GPIO.setmode(GPIO.BCM) GPIO.setup(26, GPIO.OUT) ON = "on" OFF = "off" #old code #import lirc #socketid = lirc.init("ledcontrol", blocking=False) #while (True): # codeIR = lirc.nextcode() # if len(codeIR) != 0: # print(codeIR) # if codeIR[0] == ON: # GPIO.output(18, True) # elif codeIR[0] == OFF: # GPIO.output(18, False) with LircdConnection("ledcontrol") as conn: while (True): codeIR = conn.readline() if len(codeIR) != 0: print(codeIR) if codeIR == ON: GPIO.output(26, True) elif codeIR == OFF: GPIO.output(26, False)
def mock_connection(mock_socket): return LircdConnection(socket=mock_socket)