diff --git a/aioesphomeapi/connection.py b/aioesphomeapi/connection.py index b1cfa8a..ad0d7b4 100644 --- a/aioesphomeapi/connection.py +++ b/aioesphomeapi/connection.py @@ -512,7 +512,7 @@ class APIConnection: does not initialize the frame helper or send the hello message. """ if self.connection_state is not ConnectionState.INITIALIZED: - raise ValueError( + raise RuntimeError( "Connection can only be used once, connection is not in init state" ) diff --git a/tests/test_connection.py b/tests/test_connection.py index 270fc5f..241341b 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -769,3 +769,18 @@ async def test_bad_protobuf_message_drops_connection( mock_data_received(protocol, message_with_bad_protobuf_data) assert "Invalid protobuf message: type=TextSensorStateResponse" in caplog.text assert connection.is_connected is False + + +@pytest.mark.asyncio +async def test_connection_cannot_be_reused( + plaintext_connect_task_with_login: tuple[ + APIConnection, asyncio.Transport, APIPlaintextFrameHelper, asyncio.Task + ], +) -> None: + """Test that we raise when trying to connect when already connected.""" + conn, transport, protocol, connect_task = plaintext_connect_task_with_login + send_plaintext_hello(protocol) + send_plaintext_connect_response(protocol, False) + await connect_task + with pytest.raises(RuntimeError): + await conn.start_connection()